1 # Terminal detection, dimensions, ANSI color and Unicode display-width text. Source: https://github.com/paymog/bend-kit/tree/main/tty 2 import Base 3 import ./text.bend as Text 4 5 # Standard output is a terminal. 6 def 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. 11 def 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. 16 def 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. 21 def style(enabled: Bool, code: U32, text: String) -> String: 22 Text.style(enabled, code, text) 23 24 def auto_style(code: U32, text: String) -> IO(String): 25 do IO: 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. 30 def width(text: String) -> U32: 31 Text.width(text) 32 33 # Append spaces until text occupies at least cols display columns. 34 def pad_right(text: String, cols: U32) -> String: 35 Text.pad_right(text, cols)