From f2da8b9f22f1cdde2c9f924affa3afe8eb554b67 Mon Sep 17 00:00:00 2001 From: Spencer Brower Date: Fri, 19 Jun 2026 18:12:51 -0400 Subject: [PATCH] refactor: Used `ansi` project constants instead of inlines. --- cmd_init.odin | 3 ++- cmd_scan.odin | 7 ++++--- prompt.odin | 17 +++++++++-------- 3 files changed, 15 insertions(+), 12 deletions(-) diff --git a/cmd_init.odin b/cmd_init.odin index 305af6e..c9e1d99 100644 --- a/cmd_init.odin +++ b/cmd_init.odin @@ -1,6 +1,7 @@ package main import "core:fmt" +import "core:terminal/ansi" cmd_init :: proc(cmd: ^Command) { force := has_flag(cmd, "force") || has_flag(cmd, "f") @@ -32,7 +33,7 @@ Generate one with: ssh-keygen -t ed25519`, flush = false) selected, result := multi_select("Select SSH private keys:", keys[:]) defer delete(selected) if result == .Cancel { - fmt.wprintln(cmd.out, "\x1b[2mCancelled.\x1b[0m", flush = false) + fmt.wprintln(cmd.out, ansi.CSI + ansi.FAINT + ansi.SGR + "Cancelled." + ANSI_RESET, flush = false) return } diff --git a/cmd_scan.odin b/cmd_scan.odin index 3a4d4c7..c7ca903 100644 --- a/cmd_scan.odin +++ b/cmd_scan.odin @@ -4,6 +4,7 @@ import "core:encoding/json" import "core:fmt" import "core:os" import "core:terminal" +import "core:terminal/ansi" cmd_scan :: proc(cmd: ^Command) { db, db_ok := db_open(cmd.config_path) @@ -71,7 +72,7 @@ cmd_scan :: proc(cmd: ^Command) { selected, result := multi_select("Select .env files to backup:", files[:]) defer delete(selected) if result == .Cancel { - fmt.wprintln(cmd.out, "\x1b[2mCancelled.\x1b[0m", flush = false) + fmt.wprintln(cmd.out, ansi.CSI + ansi.FAINT + ansi.SGR + "Cancelled." + ANSI_RESET, flush = false) return } @@ -95,12 +96,12 @@ cmd_scan :: proc(cmd: ^Command) { if added_count > 0 { fmt.wprintf( cmd.out, - "\x1b[1;32mSuccessfully added %d file(s) to backup.\x1b[0m\n", + ansi.CSI + ansi.BOLD + ";" + ansi.FG_GREEN + ansi.SGR + "Successfully added %d file(s) to backup." + ANSI_RESET + "\n", added_count, flush = false, ) } else { - fmt.wprintln(cmd.out, "\x1b[2mNo files were added.\x1b[0m", flush = false) + fmt.wprintln(cmd.out, ansi.CSI + ansi.FAINT + ansi.SGR + "No files were added." + ANSI_RESET, flush = false) } } diff --git a/prompt.odin b/prompt.odin index 0ccd525..e31b2a9 100644 --- a/prompt.odin +++ b/prompt.odin @@ -2,6 +2,7 @@ package main import "core:fmt" import "core:sys/posix" +import "core:terminal/ansi" MultiSelect_Result :: enum { Confirm, @@ -40,12 +41,12 @@ multi_select :: proc( cursor: int = 0 scroll_offset: int = 0 - fmt.printf("\x1b[?25l") + fmt.printf(ansi.CSI + ansi.DECTCEM_HIDE) visible := render_options(prompt, options, selected[:], cursor, scroll_offset) raw, ok := enable_raw_mode(posix.STDIN_FILENO) if !ok { - fmt.printf("\x1b[?25h") + fmt.printf(ansi.CSI + ansi.DECTCEM_SHOW) return } defer disable_raw_mode(&raw) @@ -65,18 +66,18 @@ multi_select :: proc( case .Space: selected[cursor] = !selected[cursor] case .Enter: - fmt.printf("\x1b[%dA\x1b[J\x1b[?25h", visible + 1) + fmt.printf(ansi.CSI + "%d" + ansi.CUU + ansi.CSI + ansi.ED + ansi.CSI + ansi.DECTCEM_SHOW, visible + 1) result = .Confirm return case .Escape: - fmt.printf("\x1b[%dA\x1b[J\x1b[?25h", visible + 1) + fmt.printf(ansi.CSI + "%d" + ansi.CUU + ansi.CSI + ansi.ED + ansi.CSI + ansi.DECTCEM_SHOW, visible + 1) result = .Cancel return case .Unknown: } scroll_offset = max(0, min(cursor - MAX_VISIBLE / 2, len(options) - MAX_VISIBLE)) - fmt.printf("\x1b[%dA\x1b[0J", visible + 1) + fmt.printf(ansi.CSI + "%d" + ansi.CUU + ansi.CSI + ansi.RESET + ansi.ED, visible + 1) visible = render_options(prompt, options, selected[:], cursor, scroll_offset) } } @@ -88,7 +89,7 @@ render_options :: proc( cursor: int, scroll_offset: int, ) -> int { - fmt.printf("\x1b[1;36m%s\x1b[0m (↑/↓ move, space select, enter confirm)\r\n", prompt) + fmt.printf(ansi.CSI + ansi.BOLD + ";" + ansi.FG_CYAN + ansi.SGR + "%s" + ANSI_RESET + " (↑/↓ move, space select, enter confirm)\r\n", prompt) end := scroll_offset + MAX_VISIBLE if end > len(options) { @@ -101,9 +102,9 @@ render_options :: proc( checkbox = "x" } if i == cursor { - fmt.printf("\x1b[1;32m> \x1b[0m[\x1b[32m%s\x1b[0m] %s\r\n", checkbox, options[i]) + fmt.printf(ansi.CSI + ansi.BOLD + ";" + ansi.FG_GREEN + ansi.SGR + "> " + ANSI_RESET + "[" + ansi.CSI + ansi.FG_GREEN + ansi.SGR + "%s" + ANSI_RESET + "] %s\r\n", checkbox, options[i]) } else { - fmt.printf(" [\x1b[2m%s\x1b[0m] %s\r\n", checkbox, options[i]) + fmt.printf(" [" + ansi.CSI + ansi.FAINT + ansi.SGR + "%s" + ANSI_RESET + "] %s\r\n", checkbox, options[i]) } }