tty.bend
bend-kit-tty@0.1.0.0 · 35 lines · 0 definitions
1# Terminal detection, dimensions, ANSI color and Unicode display-width text. Source: https://github.com/paymog/bend-kit/tree/main/tty
2import Base
3import bend-kit-tty@0.1.0.0/text.bend as Text
4
5# Standard output is a terminal.
6def stdout_tty() -> IO(Bool):
7 import "./effs/tty.c"
8 import "./effs/tty.js"
9
10# Terminal columns and rows; None when stdout is not a terminal or its size is unavailable.
11def size() -> IO(Maybe<&1, U32 & U32>):
12 import "./effs/tty.c"
13 import "./effs/tty.js"
14
15# Automatic styling is enabled only for a terminal without NO_COLOR.
16def color_enabled() -> IO(Bool):
17 import "./effs/tty.c"
18 import "./effs/tty.js"
19
20# Apply one numeric ANSI SGR code and reset it, or leave the text unchanged.
21def style(enabled: Bool, code: U32, text: String) -> String:
22 Text.style(enabled, code, text)
23
24def auto_style(code: U32, text: String) -> IO(String):
25 do IO<String>:
26 enabled : Bool <- color_enabled()
27 return style(enabled, code, text)
28
29# Display columns for Unicode text. Control characters are not terminal cursor movements.
30def width(text: String) -> U32:
31 Text.width(text)
32
33# Append spaces until text occupies at least cols display columns.
34def pad_right(text: String, cols: U32) -> String:
35 Text.pad_right(text, cols)