Something went wrong. Try again.
minimal agent cli memoria.tools
Something went wrong. Try again.
123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156package input
// Rendering the prompt (RENDERING.md): `┃ ` + content, one marker row per// visual row — hard newlines and soft wraps alike. Pure: state by value,// geometry returned via Layout, never stored in the state.//// Each row is prefixed with term.clear_line (\r + erase line): a repaint// must erase the previous frame's content, which may be longer than the// new one (RENDERING.md). The marker paints in the theme's marker color;// every escape comes from a term proc — none is written here.
import "../term"import "../theme"import "core:strings"import "core:unicode"import "core:unicode/utf8"
MARKER :: "┃"MARKER_WIDTH :: 2 // marker cell + one space
Row :: struct { start: int, // byte offset of the first rune of the row end: int, // byte offset after the last rune of the row width: int, // display cells used by the row's content}
Layout :: struct { rows: int, // visual rows the prompt occupies, always >= 1 cursor_row: int, // visual row (0-based) holding the edit cursor cursor_column: int, // 0-based terminal column of the edit cursor}
// Compute the visual rows of the prompt at the given terminal width.// The returned slice uses context.temp_allocator.row_layout :: proc(state: State, width: int) -> []Row { content_width := max(1, width - MARKER_WIDTH)
rows := make([dynamic]Row, 0, 8, context.temp_allocator)
start := 0 row_width := 0 end := 0 for end < len(state.bytes) { r, size := utf8.decode_rune(state.bytes[end:])
if r == '\n' { // The newline consumes its byte and starts a new row; a // trailing newline therefore yields one final empty row. append(&rows, Row{start = start, end = end, width = row_width}) end += size start = end row_width = 0 continue }
w := term.display_width(r) if row_width > 0 && row_width + w > content_width { // This rune starts the next row; don't consume it here. // A rune wider than content_width still gets its own row. append(&rows, Row{start = start, end = end, width = row_width}) start = end row_width = 0 continue }
row_width += w end += size }
append(&rows, Row{start = start, end = end, width = row_width}) return rows[:]}
// The edit cursor's visual position (RENDERING.md: geometry is computed,// never stored): the row containing state.position, and the column just// after the content preceding it on that row. At a soft-wrap boundary the// cursor sits at the end of the earlier row.cursor_layout :: proc(state: State, rows: []Row) -> (row: int, column: int) { for r, i in rows { if r.start <= state.position && state.position <= r.end { column := MARKER_WIDTH for index := r.start; index < state.position; { ch, size := utf8.decode_rune(state.bytes[index:]) column += term.display_width(ch) index += size } return i, column } } // Defensive: position outside every row (the invariants say this // cannot happen) — the end of the last row. last := rows[len(rows) - 1] return len(rows) - 1, MARKER_WIDTH + last.width}
// Render the prompt into sb. Pure: never mutates the state it renders.// mask paints every non-control rune as '*' (API-key entry); display widths// are preserved so the row layout still matches the cursor.render :: proc( state: State, sb: ^strings.Builder, width: int, t: theme.Theme, mask := false,) -> Layout { rows := row_layout(state, width)
for row, i in rows { // Odin slice iteration binds value first, index second if i > 0 { strings.write_string(sb, "\r\n") } // Erase whatever the previous frame left on this row before // painting it — the old content may be longer than the new. term.clear_line(sb) term.set_foreground_rgb(sb, t.marker) strings.write_string(sb, MARKER) strings.write_string(sb, " ") term.reset_style(sb)
for index := row.start; index < row.end; { r, size := utf8.decode_rune(state.bytes[index:]) if mask && r != '\t' && !unicode.is_control(r) { for _ in 0 ..< max(term.display_width(r), 1) { strings.write_byte(sb, '*') } index += size continue } switch { case r == '\t': for _ in 0 ..< term.TAB_WIDTH { strings.write_string(sb, " ") } case r == utf8.RUNE_ERROR && size == 1: // Defensive: the State invariants say this cannot happen. strings.write_string(sb, " ") case unicode.is_control(r): // A paste can carry control bytes; painting them raw would // drive the terminal. One space — the cell // term.display_width already counted. strings.write_string(sb, " ") case: strings.write_string(sb, string(state.bytes[index:index + size])) } index += size } }
cursor_row, cursor_column := cursor_layout(state, rows) return Layout{ rows = len(rows), cursor_row = cursor_row, cursor_column = cursor_column, }}