]> jxnshi.xyz Git - mesange.git/commitdiff
Trying to understand how ncurses work
authorjxnshi <jxnshi@cock.li>
Tue, 31 Dec 2024 23:35:21 +0000 (00:35 +0100)
committerjxnshi <jxnshi@cock.li>
Tue, 31 Dec 2024 23:35:21 +0000 (00:35 +0100)
client-cli/client-cli
client-cli/main.odin

index 3c95ce5b03593e549d055183996ae2fdaac4bf9c..984658c28bfdf8ecf47e83c673baeea6439aeedb 100755 (executable)
Binary files a/client-cli/client-cli and b/client-cli/client-cli differ
index e06d47d3a15caa91bdb0cc1ef68bc61f2d9a74b3..3acd4e54fcf50f98ea1e456541f9b31248af23b8 100644 (file)
@@ -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..<width {
+           nc.waddch(app.info_bar, ' ')
+       }
+       
+       nc.wattroff(app.info_bar, color)
+       nc.wrefresh(app.info_bar)
 }
 
 app_clear_box_message :: proc(app: ^App) {
@@ -183,9 +231,11 @@ app_get_input :: proc(app: App, allocator := context.allocator) -> 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)))
 }