All Projects → crystallabs → tput.cr

crystallabs / tput.cr

Licence: other
Low-level component for building term/console applications in Crystal

Programming Languages

crystal
512 projects

Projects that are alternatives of or similar to tput.cr

croatoan
Common Lisp bindings for the ncurses terminal library.
Stars: ✭ 111 (+404.55%)
Mutual labels:  console, terminfo
px
ps and top for human beings
Stars: ✭ 151 (+586.36%)
Mutual labels:  console
go-color
A lightweight, simple and cross-platform package to colorize text in terminals
Stars: ✭ 65 (+195.45%)
Mutual labels:  console
douban.fm
简洁的豆瓣电台命令行版。
Stars: ✭ 13 (-40.91%)
Mutual labels:  console
leeks.js
Simple ANSI styling for your terminal
Stars: ✭ 12 (-45.45%)
Mutual labels:  console
yachalk
🖍️ Terminal string styling done right
Stars: ✭ 131 (+495.45%)
Mutual labels:  console
multi auth
Standardized multi-provider OAuth authentication
Stars: ✭ 102 (+363.64%)
Mutual labels:  crystal-lang
stylish-log
🎉 Stylish-log "A beautiful way to see your web console logs"
Stars: ✭ 12 (-45.45%)
Mutual labels:  console
log-utils
Basic logging utils: colors, symbols and timestamp.
Stars: ✭ 24 (+9.09%)
Mutual labels:  console
Inquirer.cs
A collection of common interactive command line user interfaces. Port of Inquirer.js
Stars: ✭ 26 (+18.18%)
Mutual labels:  console
mini-me
Inline multiline text-editor/prompt written in Rust.
Stars: ✭ 23 (+4.55%)
Mutual labels:  console
barecolor
A tiny JavaScript utility for printing colorful console messages.
Stars: ✭ 20 (-9.09%)
Mutual labels:  console
CLI-Autocomplete
Cross-platform flexible autocomplete library for your CLI applications.
Stars: ✭ 21 (-4.55%)
Mutual labels:  console
multitextor
Multiplatform command line text editor.
Stars: ✭ 27 (+22.73%)
Mutual labels:  console
laravel-console-spinner
Customized loading ⌛ spinner for Laravel Artisan Console.
Stars: ✭ 70 (+218.18%)
Mutual labels:  console
laravel-make-me
Extendable Interactive Make Command for Laravel
Stars: ✭ 36 (+63.64%)
Mutual labels:  console
congif
convert script(1) output to GIF
Stars: ✭ 52 (+136.36%)
Mutual labels:  console
oof
Convenient, high-performance RGB color and position control for console output
Stars: ✭ 764 (+3372.73%)
Mutual labels:  console
colorize
pub.dartlang.org/packages/colorize
Stars: ✭ 20 (-9.09%)
Mutual labels:  console
console
a debugger for async rust!
Stars: ✭ 2,101 (+9450%)
Mutual labels:  console

Tput.cr

Tput (akin to tput(1)) is a complete term/console output library for Crystal.

In general, when writing console apps, 4 layers can be identified:

  1. Low-level (terminal emulator & terminfo)
  2. Mid-level (console interface, memory state, cursor position, etc.)
  3. High-level (a framework or toolkit)
  4. End-user apps

Tput implements levels (1) and (2).

Is it any good? Yes, there are many, many functions supported. Check the API docs for a list.

It is a Crystal-native implementation (except for binding to a C Terminfo library called unibilium). Not even ncurses bindings are used; Tput is a standalone library that implements all the needed functions itself and provides a much nicer API.

Installation

  1. Add the dependency to your shard.yml:
dependencies:
  tput:
    github: crystallabs/tput.cr
    version: ~> 1.0
  1. Run shards install

Overview

If/when initialized with term name, terminfo data will be looked up. If no terminfo data is found (or one wishes not to use terminfo), Tput's built-in replacements will be used.

As part of initialization, Tput will also detect terminal features and the terminal emulator program in use.

There is zero configuration or considerations to have in mind when using this library. Everything is set up automatically.

For example:

require "unibilium"
require "tput"

terminfo = Unibilium::Terminfo.from_env
tput = Tput.new terminfo

# Print detected features and environment
p tput.features.to_json
p tput.emulator.to_json

# Set terminal emulator's title, if possible
tput.title = "Test 123"

# Set cursor to red block:
tput.cursor_shape Tput::CursorShape::Block, blink: false
tput.cursor_color Tput::Color::Red

# Switch to "alternate buffer", print some text
tput.alternate
tput.cursor_pos 10, 20
tput.echo "Text at position y=10, x=20"
tput.bell
tput.cr
tput.lf

tput.echo "Now displaying ACS chars:"
tput.cr
tput.lf
tput.smacs
tput.echo "``aaffggiijjkkllmmnnooppqqrrssttuuvvwwxxyyzz{{||}}~~"
tput.rmacs

tput.cr
tput.lf
tput.echo "Press any keys; q to exit."

# Listen for keypresses:
tput.listen do |char, key, sequence|
  # Char is a single typed character, or the first character
  # of a sequence which led to a particular key.

  # Key is a keyboard key, if any. Ordinary characters like
  # 'a' or '1' don't have a representation as Key. Special
  # keys like Enter, F1, Esc etc. do.

  # Sequence is the complete sequence of characters which
  # were consumed as part of identifying the key that was
  # pressed.
  if char == 'q'
    exit
  else
    tput.cr
    tput.lf
    tput.echo "Char=#{char.inspect}, Key=#{key.inspect}, Sequence=#{sequence.inspect}"
  end
end

tput.clear

Why no ncurses? Ncurses is an implementation sort-of specific to C. When not working in C, many of the ncurses methods make no sense. Also in general, the ncurses API is arcane.

API documentation

Run crystal docs as usual, then open file docs/index.html.

Testing

Run crystal spec as usual.

Thanks

  • All the fine folks on Libera.Chat IRC channel #crystal-lang and on Crystal's Gitter channel https://gitter.im/crystal-lang/crystal

  • Blacksmoke16, Asterite, HertzDevil, Raz, Oprypin, Straight-shoota, Watzon, Naqvis, and others!

Note that the project description data, including the texts, logos, images, and/or trademarks, for each open source project belongs to its rightful owner. If you wish to add or remove any projects, please contact us at [email protected].