nx_render_cli.nx source
↩ module page · 155 lines · 4942 B
1// nx_render_cli.nx -- native ASCII visualization engine.
2//
3// Per user 2026-05-15: "build the visual in cli, browser, etc it
4// should be native".
5//
6// CLI side: ASCII bar chart, ASCII line plot, ASCII matrix display.
7// Pure stdout writes via println(). No ncurses, no termcap, no
8// external lib -- works in any 80-col terminal.
9
10// nx_safety_envelope:
11// intended_use: AUTO_APPLIED -- primitive-specific tuning queued
12// sil_target: SIL1
13// evidence: [bulk_applied_2026-05-16, see-file-comment-for-detail]
14// verdict: NOT_YET_EVALUATED
15
16import "nx_kernel_v2.nx"
17
18const NX_RENDER_BAR_MAX: nx_int = 60
19const NX_RENDER_PLOT_W: nx_int = 60
20const NX_RENDER_PLOT_H: nx_int = 20
21
22// Find max value in an array.
23func nx_render_max(values: *nx_int, n: nx_int) -> nx_int {
24 if n <= 0 { return 0 }
25 var p0: *nx_int = values
26 var m: nx_int = p0[0]
27 var i: nx_int = 1
28 while i < n {
29 let p: *nx_int = ((values as nx_int) + (i * 8)) as *nx_int
30 if p[0] > m { m = p[0] }
31 i = i + 1
32 }
33 return m
34}
35
36func nx_render_min(values: *nx_int, n: nx_int) -> nx_int {
37 if n <= 0 { return 0 }
38 var p0: *nx_int = values
39 var m: nx_int = p0[0]
40 var i: nx_int = 1
41 while i < n {
42 let p: *nx_int = ((values as nx_int) + (i * 8)) as *nx_int
43 if p[0] < m { m = p[0] }
44 i = i + 1
45 }
46 return m
47}
48
49// ===== Bar chart =====================================================
50// Each row: "[label: NN] |######" where # count = (value / max) * 60.
51func nx_render_bar_row(label: nx_int, value: nx_int, max_v: nx_int) -> nx_int {
52 print("[" as *u8); print_i64(label); print(": " as *u8); print_i64(value); print("] |" as *u8)
53 if max_v <= 0 {
54 println("" as *u8); return 0
55 }
56 let bars: nx_int = (value * NX_RENDER_BAR_MAX) / max_v
57 var i: nx_int = 0
58 while i < bars {
59 print("#" as *u8)
60 i = i + 1
61 }
62 println("" as *u8)
63 return bars
64}
65
66func nx_render_bar(values: *nx_int, n: nx_int) -> nx_int {
67 let max_v: nx_int = nx_render_max(values, n)
68 println("---- bar chart ----" as *u8)
69 var i: nx_int = 0
70 while i < n {
71 let p: *nx_int = ((values as nx_int) + (i * 8)) as *nx_int
72 let _r: nx_int = nx_render_bar_row(i, p[0], max_v)
73 i = i + 1
74 }
75 return n
76}
77
78// ===== Line plot (ASCII) ============================================
79// Build a (PLOT_H x PLOT_W) char grid, plot points by mapping
80// y -> (PLOT_H - 1) - (y - min) * (PLOT_H - 1) / range,
81// then print top-down.
82//
83// Stores grid as flat *u8 of size PLOT_H * PLOT_W. Initial fill is
84// '.' ; points become '*'.
85func nx_render_line(values: *nx_int, n: nx_int) -> nx_int {
86 if n <= 0 { return 0 }
87 let max_v: nx_int = nx_render_max(values, n)
88 let min_v: nx_int = nx_render_min(values, n)
89 var range: nx_int = max_v - min_v
90 if range <= 0 { range = 1 }
91 let grid: *u8 = (sys_mmap((NX_RENDER_PLOT_H * NX_RENDER_PLOT_W) as i64)) as *u8
92
93 // Fill with '.'.
94 var i: nx_int = 0
95 while i < (NX_RENDER_PLOT_H * NX_RENDER_PLOT_W) {
96 let p: *u8 = ((grid as nx_int) + i) as *u8
97 p[0] = 46 as u8 // '.'
98 i = i + 1
99 }
100
101 // Plot. x mapped 0..n-1 -> 0..PLOT_W-1.
102 var k: nx_int = 0
103 while k < n {
104 let pv: *nx_int = ((values as nx_int) + (k * 8)) as *nx_int
105 let v: nx_int = pv[0]
106 let x: nx_int = (k * (NX_RENDER_PLOT_W - 1)) / (n - 1 + 1)
107 let y: nx_int = (NX_RENDER_PLOT_H - 1) - ((v - min_v) * (NX_RENDER_PLOT_H - 1)) / range
108 if y >= 0 {
109 if y < NX_RENDER_PLOT_H {
110 if x >= 0 {
111 if x < NX_RENDER_PLOT_W {
112 let cell: *u8 = ((grid as nx_int) + (y * NX_RENDER_PLOT_W + x)) as *u8
113 cell[0] = 42 as u8 // '*'
114 }
115 }
116 }
117 }
118 k = k + 1
119 }
120
121 // Print row by row.
122 println("---- line plot ----" as *u8)
123 var r: nx_int = 0
124 while r < NX_RENDER_PLOT_H {
125 var c: nx_int = 0
126 while c < NX_RENDER_PLOT_W {
127 let cell: *u8 = ((grid as nx_int) + (r * NX_RENDER_PLOT_W + c)) as *u8
128 sys_write(1, cell, 1)
129 c = c + 1
130 }
131 println("" as *u8)
132 r = r + 1
133 }
134 return n
135}
136
137// ===== Matrix display ===============================================
138// Prints a (rows x cols) matrix with right-aligned cells.
139func nx_render_matrix(data: *nx_int, rows: nx_int, cols: nx_int) -> nx_int {
140 println("---- matrix ----" as *u8)
141 var i: nx_int = 0
142 while i < rows {
143 print("[" as *u8)
144 var j: nx_int = 0
145 while j < cols {
146 if j > 0 { print(", " as *u8) }
147 let p: *nx_int = ((data as nx_int) + ((i * cols + j) * 8)) as *nx_int
148 print_i64(p[0])
149 j = j + 1
150 }
151 println("]" as *u8)
152 i = i + 1
153 }
154 return rows
155}