From: jxnshi Date: Tue, 31 Dec 2024 23:35:21 +0000 (+0100) Subject: Trying to understand how ncurses work X-Git-Url: https://jxnshi.xyz/repos?a=commitdiff_plain;h=1010820f2575d1b68b36898373d9b561d2865038;p=mesange.git Trying to understand how ncurses work --- diff --git a/client-cli/client-cli b/client-cli/client-cli index 3c95ce5..984658c 100755 Binary files a/client-cli/client-cli and b/client-cli/client-cli differ diff --git a/client-cli/main.odin b/client-cli/main.odin index e06d47d..3acd4e5 100644 --- a/client-cli/main.odin +++ b/client-cli/main.odin @@ -15,6 +15,10 @@ import nc "ncurses" import "../common" +COLOR_FIRST :: 16 +COLOR_SECOND :: 17 +COLOR_THIRD :: 18 + Config :: struct { selected_profile: Maybe(string), } @@ -66,6 +70,7 @@ App :: struct { box_message: Maybe(^nc.Window), info_bar: ^nc.Window, + input_window: ^nc.Window, config: Config, profile_password: Maybe(string), @@ -78,12 +83,21 @@ app_init :: proc() -> App { nc.keypad(screen, true) nc.curs_set(0) + nc.use_default_colors() + nc.start_color() + + nc.init_color(COLOR_FIRST, 800, 800, 800) + nc.init_color(COLOR_SECOND, 500, 500, 500) + nc.init_color(COLOR_THIRD, 100, 100, 100) + + nc.init_pair(1, nc.COLOR_WHITE, COLOR_THIRD) + screen_height, screen_width := nc.getmaxyx(screen) - info_bar := nc.newwin(0, 0, 3, screen_width) + info_bar := nc.newwin(1, screen_width, screen_height - 2, 0) + input_window := nc.newwin(1, screen_width, screen_height - 1, 0) - nc.box(info_bar, 0, 0) - nc.wrefresh(info_bar) + nc.refresh() home_path := os.get_env("HOME") storage_path := fpath.join({ home_path, "mesange" }) @@ -97,15 +111,23 @@ app_init :: proc() -> App { } } - return { + app := App{ screen = screen, storage_path = storage_path, + + info_bar = info_bar, + input_window = input_window, } + + app_set_state(&app, .Load_Config) + + return app } app_deinit :: proc(app: App) { config_deinit(app.config) + nc.delwin(app.input_window) nc.delwin(app.info_bar) if box_message, ok := app.box_message.?; ok { @@ -116,16 +138,23 @@ app_deinit :: proc(app: App) { } app_set_state :: proc(app: ^App, state: State) { - app_clear_box_message(app) - nc.clear() nc.refresh() + app_clear_box_message(app) + app_clear_info_bar(app^) + app.state = state } app_set_info_bar :: proc(app: ^App, content: string) { - + c_content := strings.clone_to_cstring(content, context.temp_allocator) + + color := nc.COLOR_PAIR(1) + + nc.wattron(app.info_bar, color) + nc.wprintw(app.info_bar, c_content) + nc.wattroff(app.info_bar, color) } app_set_box_message :: proc(app: ^App, lines: []string) { @@ -142,27 +171,46 @@ app_set_box_message :: proc(app: ^App, lines: []string) { } height := i32(len(lines)) + 2 - width := i32(max_line_len) + 2 + width := i32(max_line_len) + 4 box_message := nc.newwin( height, width, - (screen_height - height) / 2, + (screen_height - height) / 2 - 2, (screen_width - width) / 2, ) + nc.refresh() + + nc.wattron(box_message, COLOR_FIRST) + for line, i in lines { c_line := strings.clone_to_cstring(line, context.temp_allocator) - nc.mvwprintw(box_message, i32(i + 1), 1, c_line) + nc.mvwprintw(box_message, i32(i + 1), 2, c_line) } nc.box(box_message, 0, 0) + nc.wattroff(box_message, COLOR_FIRST) + nc.wrefresh(box_message) app.box_message = box_message } app_clear_info_bar :: proc(app: App) { + height, width := nc.getmaxyx(app.info_bar) + + color := nc.COLOR_PAIR(1) + + nc.wclear(app.info_bar) + nc.wattron(app.info_bar, color) + + for _ in 0.. string { screen_height, screen_width := nc.getmaxyx(app.screen) nc.curs_set(1) - nc.mvgetnstr(screen_height - 1, 0, raw_data(buffer[:]), len(buffer)) + nc.mvwgetnstr(app.input_window, 0, 0, raw_data(buffer[:]), len(buffer)) nc.curs_set(0) + nc.wclear(app.input_window) + return strings.clone_from_cstring(cstring(raw_data(&buffer))) }