mirror of
https://github.com/sbrow/envr.git
synced 2026-06-27 10:38:33 -04:00
refactor: Used ansi project constants instead of inlines.
This commit is contained in:
@@ -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
|
||||
}
|
||||
|
||||
|
||||
@@ -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)
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
17
prompt.odin
17
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])
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
Reference in New Issue
Block a user