code wiki / (root) / nx_tty.nx

nx_tty.nx source

↩ module page · 167 lines · 4818 B

1// nx_tty.nx -- terminal output primitives (ANSI escape sequences). 2// 3// The user-interaction layer's OUTPUT side. Writes ANSI escape 4// sequences to stdout (fd=1) for cursor positioning, color, screen 5// clearing, attribute changes. Pure i64 + sys_write; no libc. 6// 7// Why this exists (per user 2026-05-13): 8// "trace all the way from the hardware bits electrical impulses up 9// through the math physics function objects programs to the user 10// interaction" 11// 12// Closes the substrate-to-user gap that the generational audit 13// surfaced (Gen 0-8 covered code; Gen "UI" was absent). 14// 15// Compatible with: xterm, gnome-terminal, kitty, alacritty, VS Code 16// terminal, Windows Terminal, every VT100-derived TTY. 17// 18// genealogy_id: ecma_48_1976_ansi_escape + vt100_1978_terminal 19// + xterm_extensions 20// lineage_id: escape_sequence_emission_to_fd_stdout 21// axioms: NX_AX_LOGIC_IDENTITY (each escape sequence is a 22// well-defined glyph -> behavior mapping) 23 24// nx_safety_envelope: 25// intended_use: AUTO_APPLIED -- primitive-specific tuning queued 26// sil_target: SIL1 27// evidence: [bulk_applied_2026-05-16, see-file-comment-for-detail] 28// verdict: NOT_YET_EVALUATED 29 30import "syscalls.nx" 31import "runtime.nx" 32import "nx_axioms.nx" 33 34// ===== Foreground color codes (ANSI 30-37 + bright 90-97) ============== 35 36const NX_TTY_FG_BLACK: i64 = 30 37const NX_TTY_FG_RED: i64 = 31 38const NX_TTY_FG_GREEN: i64 = 32 39const NX_TTY_FG_YELLOW: i64 = 33 40const NX_TTY_FG_BLUE: i64 = 34 41const NX_TTY_FG_MAGENTA: i64 = 35 42const NX_TTY_FG_CYAN: i64 = 36 43const NX_TTY_FG_WHITE: i64 = 37 44const NX_TTY_FG_DEFAULT: i64 = 39 45 46// Background colors: add 10 (40-47 + 49 default). 47const NX_TTY_BG_OFFSET: i64 = 10 48 49// ===== screen clear / cursor ========================================== 50 51// Clear entire screen + home cursor. 52func nx_tty_clear() -> i64 { 53 return sys_write(1, "\x1b[2J\x1b[H" as *u8, 7) 54} 55 56// Clear from cursor to end of line. 57func nx_tty_clear_line() -> i64 { 58 return sys_write(1, "\x1b[K" as *u8, 3) 59} 60 61// Move cursor to (row, col), both 1-indexed (ANSI convention). 62func nx_tty_cursor_at(row: i64, col: i64) -> i64 { 63 // emit "\x1b[<row>;<col>H" 64 let buf: *u8 = sys_mmap(32) 65 buf[0] = 0x1b 66 buf[1] = 91 // '[' 67 let row_str: *u8 = sys_mmap(16) 68 let n_row: i64 = itoa(row, row_str) 69 var i: i64 = 0 70 var pos: i64 = 2 71 while i < n_row { 72 buf[pos] = row_str[i] 73 pos = pos + 1 74 i = i + 1 75 } 76 buf[pos] = 59 // ';' 77 pos = pos + 1 78 let col_str: *u8 = sys_mmap(16) 79 let n_col: i64 = itoa(col, col_str) 80 var j: i64 = 0 81 while j < n_col { 82 buf[pos] = col_str[j] 83 pos = pos + 1 84 j = j + 1 85 } 86 buf[pos] = 72 // 'H' 87 pos = pos + 1 88 return sys_write(1, buf, pos) 89} 90 91func nx_tty_cursor_home() -> i64 { 92 return sys_write(1, "\x1b[H" as *u8, 3) 93} 94 95func nx_tty_cursor_hide() -> i64 { 96 return sys_write(1, "\x1b[?25l" as *u8, 6) 97} 98 99func nx_tty_cursor_show() -> i64 { 100 return sys_write(1, "\x1b[?25h" as *u8, 6) 101} 102 103// ===== attribute changes ============================================== 104 105// Reset all attributes. 106func nx_tty_reset() -> i64 { 107 return sys_write(1, "\x1b[0m" as *u8, 4) 108} 109 110// Bold on / off. 111func nx_tty_bold() -> i64 { 112 return sys_write(1, "\x1b[1m" as *u8, 4) 113} 114 115// Underline on / off. 116func nx_tty_underline() -> i64 { 117 return sys_write(1, "\x1b[4m" as *u8, 4) 118} 119 120// Inverse video. 121func nx_tty_inverse() -> i64 { 122 return sys_write(1, "\x1b[7m" as *u8, 4) 123} 124 125// Set foreground color. Use NX_TTY_FG_* constants. 126func nx_tty_fg(color: i64) -> i64 { 127 let buf: *u8 = sys_mmap(16) 128 buf[0] = 0x1b 129 buf[1] = 91 // '[' 130 let s: *u8 = sys_mmap(8) 131 let n: i64 = itoa(color, s) 132 var i: i64 = 0 133 var pos: i64 = 2 134 while i < n { 135 buf[pos] = s[i] 136 pos = pos + 1 137 i = i + 1 138 } 139 buf[pos] = 109 // 'm' 140 pos = pos + 1 141 return sys_write(1, buf, pos) 142} 143 144// Set background color (color = FG_* + BG_OFFSET). 145func nx_tty_bg(color: i64) -> i64 { 146 return nx_tty_fg(color + NX_TTY_BG_OFFSET) 147} 148 149// ===== high-level helpers =============================================== 150 151// Print a string with a specific foreground color, then reset. 152func nx_tty_say(color: i64, s: *u8) -> i64 { 153 nx_tty_fg(color) 154 print(s) 155 nx_tty_reset() 156 return 0 157} 158 159// Print a string in bold + color, then newline. 160func nx_tty_announce(color: i64, s: *u8) -> i64 { 161 nx_tty_bold() 162 nx_tty_fg(color) 163 print(s) 164 nx_tty_reset() 165 print("\n" as *u8) 166 return 0 167}