export Prim.( print, println ), * use Str (Str(*)) use Mem (cast) use Math (rshift) use Iter (for-each) use Prim (print, println) # basic stuff # CPrintWriter # CPrintWriter # inst Writer CPrintWriter # write (wr _, ptr Ptr item, count Size) # Cnile.printf2('%.*s', count Mem.size-i32(), ptr) # write-cformat1 (wr _, cs ConstStr, x a) # Cnile.printf1(cs, x) ## term stuff fn is-terminal(): Cnile.isatty(Cnile.stdout-fileno()) > 0 fn beep() c = 7 Mem.cast() as Char print(c) # C's wcwidth. Cnile.setlocale(Cnile.lc-ctype(), '') # <- set this for wcwidth to work properly wtf (GNUfail) fn wcwidth (c Char): c Char.to-c-wchar() Cnile.wcwidth() ## color term stuff. ## SHOULD THIS BE IN THE SAME FILE??? fn show-cursor() print('\x1b[?25h') fn hide-cursor() print('\x1b[?25l') fn move-cursor-to (x U32, y U32) # 1.. x = x + 1 y = y + 1 print('\x1b[\(y);\(x)H') fn reset-cursor-position() print('\x1b[H') fn move-cursor-to-line-start () print('\r') # :) fn get-dimensions() -> (U32, U32) ws =& Mem.zeroed() as Cnile.Winsize if Cnile.ioctl(Cnile.stdout-fileno(), Cnile.tiocgwinsz(), ws) == -1 or ws&.ws-col == 0 return (80, 24) return (Mem.u16-u32(ws&.ws-col), Mem.u16-u32(ws&.ws-row)) ## Alt Screen fn enable-alternative-screen-buffer () print('\x1b[?1049h') fn disable-alternative-screen-buffer () print('\x1b[?1049l') ## Colors fn set-default-fg() print('\x1b[39m') fn set-default-bg() print('\x1b[49m') ColorPalette Palette8 Palette16 Palette256 PaletteRGB inst Str ColorPalette # print-str (p) # case p # Palette8 # print-str('Palette8') # Palette16 # print-str('Palette16') # Palette256 # print-str('Palette256') # PaletteRGB # print-str('PaletteRGB') chars (p) case p Palette8 return 'Palette8' chars() Palette16 return 'Palette16' chars() Palette256 return 'Palette256' chars() PaletteRGB return 'PaletteRGB' chars() fn query-palette() -> ColorPalette colorterm = Env.get('COLORTERM') case colorterm Just(def) if def == 'truecolor' or def == '24bit' return PaletteRGB None pass case Env.get('TERM') Just(term) if term == 'linux' return Palette8 None pass return Palette16 # default Color8 Black8 Red8 Green8 Yellow8 Blue8 Magenta8 Cyan8 White8 inst Eq Color8 eq (l, r): (Mem.cast-on-zeroed(l) as I32) == Mem.cast-on-zeroed(r) # :) Color16 Black16 Red16 Green16 Yellow16 Blue16 Magenta16 Cyan16 White16 BrightBlack16 BrightRed16 BrightGreen16 BrightYellow16 BrightBlue16 BrightMagenta16 BrightCyan16 BrightWhite16 inst Eq Color16 eq (l, r): (Mem.cast-on-zeroed(l) as I32) == Mem.cast-on-zeroed(r) # :) fn set-fg8(color Color8) case color Black8 print('\x1b[30m') Red8 print('\x1b[31m') Green8 print('\x1b[32m') Yellow8 print('\x1b[33m') Blue8 print('\x1b[34m') Magenta8 print('\x1b[35m') Cyan8 print('\x1b[36m') White8 print('\x1b[37m') fn set-fg16(color Color16) case color Black16 print('\x1b[30m') Red16 print('\x1b[31m') Green16 print('\x1b[32m') Yellow16 print('\x1b[33m') Blue16 print('\x1b[34m') Magenta16 print('\x1b[35m') Cyan16 print('\x1b[36m') White16 print('\x1b[37m') BrightBlack16 print('\x1b[90m') BrightRed16 print('\x1b[91m') BrightGreen16 print('\x1b[92m') BrightYellow16 print('\x1b[93m') BrightBlue16 print('\x1b[94m') BrightMagenta16 print('\x1b[95m') BrightCyan16 print('\x1b[96m') BrightWhite16 print('\x1b[97m') fn set-bg8(color Color8) case color Black8 print('\x1b[40m') Red8 print('\x1b[41m') Green8 print('\x1b[42m') Yellow8 print('\x1b[43m') Blue8 print('\x1b[44m') Magenta8 print('\x1b[45m') Cyan8 print('\x1b[46m') White8 print('\x1b[47m') fn set-bg16(color Color16) case color Black16 print('\x1b[40m') Red16 print('\x1b[41m') Green16 print('\x1b[42m') Yellow16 print('\x1b[43m') Blue16 print('\x1b[44m') Magenta16 print('\x1b[45m') Cyan16 print('\x1b[46m') White16 print('\x1b[47m') BrightBlack16 print('\x1b[100m') BrightRed16 print('\x1b[101m') BrightGreen16 print('\x1b[102m') BrightYellow16 print('\x1b[103m') BrightBlue16 print('\x1b[104m') BrightMagenta16 print('\x1b[105m') BrightCyan16 print('\x1b[106m') BrightWhite16 print('\x1b[107m') fn reset-colors() print('\x1b[0m') fn clear-screen() print('\x1b[2J') fn set-fg256(color U8) print('\x1b[38;5;\(color)m') fn set-bg256(color U8) print('\x1b[48;5;\(color)m') RGB r U8 g U8 b U8 inst Eq RGB eq (l, r): l.r == r.r and l.g == r.g and l.b == r.b fn rgb(r, g, b): RGB { r, g, b } fn rgb-from-hex(n) -> RGB return { r: (n rshift(16)) Mem.u32-u8(), g: n rshift(8) Math.mod(256) Mem.u32-u8(), b: n Math.mod(256) Mem.u32-u8() } fn set-fg-rgb(c RGB) print('\x1b[38;2;\(c.r);\(c.g);\(c.b)m') fn set-bg-rgb(c RGB) print('\x1b[48;2;\(c.r);\(c.g);\(c.b)m') # Unifying color thing Color ColorDefault Color8 Color8 Color16 Color16 Color256 U8 ColorRGB RGB inst Eq Color eq (l, r) return (case (l, r)) (ColorDefault, ColorDefault): True (Color8(lc), Color8(rc)): lc == rc (Color16(lc), Color16(rc)): lc == rc (Color256(lc), Color256(rc)): lc == rc (ColorRGB(lc), ColorRGB(rc)): lc == rc _: False fn set-fg (c Color) case c ColorDefault set-default-fg() Color8(c8) set-fg8(c8) Color16(c16) set-fg16(c16) Color256(c256) set-fg256(c256) ColorRGB(crgb) set-fg-rgb(crgb) fn set-bg (c Color) case c ColorDefault set-default-bg() Color8(c8) set-bg8(c8) Color16(c16) set-bg16(c16) Color256(c256) set-bg256(c256) ColorRGB(crgb) set-bg-rgb(crgb) fn cprintln-rgb(s, fg RGB, bg RGB) set-fg-rgb(fg) set-bg-rgb(bg) print(s) reset-colors() print('\n') fn cprintln256(s, fg U8, bg U8) set-fg256(fg) set-bg256(bg) print(s) reset-colors() print('\n') fn cprintln8(s, fg Color8, bg Color8) set-fg8(fg) set-bg8(bg) print(s) reset-colors() print('\n') fn cprintln16(s, fg Color16, bg Color16) set-fg16(fg) set-bg16(bg) print(s) reset-colors() print('\n') fn cprintln(s, fg Color, bg Color) set-fg(fg) set-bg(bg) print(s) reset-colors() print('\n')