diff options
| author | dam <dam@gudinoff> | 2023-03-31 01:52:05 +0100 |
|---|---|---|
| committer | dam <dam@gudinoff> | 2023-03-31 01:52:05 +0100 |
| commit | ee5fa34d14288a4ce79d77a0d3a89cc14b16a6b1 (patch) | |
| tree | bc79002251d4e1dc2d07e73ad1e1458ace44462b | |
| parent | f82ce565436dc04d58c85d55fb78038acf0191b2 (diff) | |
| download | task-time-tracker-ee5fa34d14288a4ce79d77a0d3a89cc14b16a6b1.tar.zst task-time-tracker-ee5fa34d14288a4ce79d77a0d3a89cc14b16a6b1.zip | |
Implemented initialize_tui.
| -rw-r--r-- | curses.jai | 36 | ||||
| -rw-r--r-- | ttt.jai | 216 |
2 files changed, 132 insertions, 120 deletions
@@ -78,12 +78,30 @@ tinfo :: #system_library "libtinfo"; // Required by some of ncurses functions. // I suspect this is an attempt to help the compilers to include libtinfo automatically. // ncurses :: #system_library "libncurses"; ncurses :: #library "libncurses"; -initscr :: () -> *WINDOW #foreign ncurses; -getch :: () -> s8 #foreign ncurses; -endwin :: () -> void #foreign ncurses; - -curs_set :: (visibility: s32) -> s32 #foreign ncurses; -mvaddstr :: (y: s32, x: s32, str: *u8) -> s32 #foreign ncurses; -noecho :: () -> s32 #foreign ncurses; -box :: (win :*WINDOW, verch :u8, horch :u8) -> s32 #foreign ncurses; - + +COLOR_BLACK :: 0; +COLOR_RED :: 1; +COLOR_GREEN :: 2; +COLOR_YELLOW :: 3; +COLOR_BLUE :: 4; +COLOR_MAGENTA :: 5; +COLOR_CYAN :: 6; +COLOR_WHITE :: 7; + +stdscr : *WINDOW; + +initscr :: () -> *WINDOW #foreign ncurses; +getch :: () -> s8 #foreign ncurses; +endwin :: () -> void #foreign ncurses; +cbreak :: () -> void #foreign ncurses; +start_color :: () -> void #foreign ncurses; +use_default_colors :: () -> void #foreign ncurses; + + +keypad :: (win: *WINDOW, bf: bool) -> s32 #foreign ncurses; + +curs_set :: (visibility: s32) -> s32 #foreign ncurses; +mvaddstr :: (y: s32, x: s32, str: *u8) -> s32 #foreign ncurses; +noecho :: () -> s32 #foreign ncurses; +box :: (win: *WINDOW, verch: u8, horch: u8) -> s32 #foreign ncurses; +init_pair :: (pair: s16, f: s16, b: s16) -> s32 #foreign ncurses; @@ -82,13 +82,18 @@ ar_file_path : string; // size_t string_buffer_size = 0; // int size_x, size_y, pos_x, pos_y; -// typedef enum { -// STYLE_SELECTED = 1, -// STYLE_SELECTED_INVERTED, -// STYLE_ACTIVE, -// STYLE_ACTIVE_SELECTED, -// STYLE_ERROR, -// } styles_et; +Styles :: enum s16 { + STYLE_SELECTED :: 1; + STYLE_SELECTED_INVERTED; + STYLE_ACTIVE; + STYLE_ACTIVE_SELECTED; + STYLE_ERROR; +} + +Layouts :: enum u8 { + NORMAL; + COMPACT; +} error_window : *WINDOW = null; error_time_limit := Apollo_Time.{0, 0}; @@ -213,7 +218,7 @@ truncate_string :: (str: string, length: s64, $encoding: Text_Encoding = .UTF8) // Returns true when the string is empty or consists of space characters. is_empty_string :: (str: string) -> bool { for 0..str.count-1 { - if str[it] == {; + if str[it] == { case #char "\0"; #through; case #char "\t"; #through; // horizontal tab case #char "\n"; #through; // line feed @@ -926,114 +931,105 @@ bool is_database_full(database_st *db) { assert(db != NULL); return db->count >= MAX_DATABASE_TASKS; } +*/ -#define INPUT_TIMEOUT_MS 1000 -#define INPUT_AWAIT_INF -1 - -#define NUM_HEADER_ROWS 1 -#define NUM_FOOTER_ROWS 1 -#define NUM_COLUMNS 9 +INPUT_TIMEOUT_MS :: 1000; +INPUT_AWAIT_INF :: -1; -#define L_TITLE_IDX 0 -#define L_DAYS_IDX 1 -#define L_TOTAL_IDX 8 +NUM_HEADER_ROWS :: 1; +NUM_FOOTER_ROWS :: 1; +NUM_COLUMNS :: 9; -typedef enum { - L_NORMAL, - L_COMPACT, - NUM_LAYOUTS, -} layouts_et; +L_TITLE_IDX :: 0; +L_DAYS_IDX :: 1; +L_TOTAL_IDX :: 8; -typedef struct { - char *header; - int width; - int alignment_offset; - char alignment; -} column_st; +Column :: struct { + header : string; + width : int; + alignment_offset : int; + alignment : u8; +} -typedef struct { - column_st columns[NUM_COLUMNS]; - char *archive_title; -} layout_st; +Layout :: struct { + columns : [NUM_COLUMNS] Column; + archive_title : string; +} -layout_st layouts[NUM_LAYOUTS]; -int layout_tasks_rows; -bool is_terminal_too_small = true; +layouts : [#run type_info(Layouts).values.count] Layout; +layout_tasks_rows : int; +is_terminal_too_small := true; -void initialize_tui() { +initialize_tui :: () { // Normal layout. - layouts[L_NORMAL] = (layout_st) { - .archive_title = " Archive ", - .columns = { - { .header = " Task Time Tracker v" VERSION " ", .width = -1, .alignment = 'L' }, - { .header = " Sun ", .width = 7, .alignment = 'C' }, - { .header = " Mon ", .width = 7, .alignment = 'C' }, - { .header = " Tue ", .width = 7, .alignment = 'C' }, - { .header = " Wed ", .width = 7, .alignment = 'C' }, - { .header = " Thu ", .width = 7, .alignment = 'C' }, - { .header = " Fri ", .width = 7, .alignment = 'C' }, - { .header = " Sat ", .width = 7, .alignment = 'C' }, - { .header = " Total ", .width = 9, .alignment = 'C' }, - } + layouts[Layouts.NORMAL] = .{ + archive_title = " Archive ", + columns = .[ + .{ header = #run join(" Task Time Tracker v", VERSION, " "), width = -1, alignment = #char "L" }, + .{ header = " Sun ", width = 7, alignment = #char "C" }, + .{ header = " Mon ", width = 7, alignment = #char "C" }, + .{ header = " Tue ", width = 7, alignment = #char "C" }, + .{ header = " Wed ", width = 7, alignment = #char "C" }, + .{ header = " Thu ", width = 7, alignment = #char "C" }, + .{ header = " Fri ", width = 7, alignment = #char "C" }, + .{ header = " Sat ", width = 7, alignment = #char "C" }, + .{ header = " Total ", width = 9, alignment = #char "C" }, + ] }; // Compact layout. - layouts[L_COMPACT] = (layout_st) { - .archive_title = " Archive ", - .columns = { - { .header = " TTT " VERSION " ", .width = -1, .alignment = 'L' }, - { .header = " S ", .width = 5, .alignment = 'C' }, - { .header = " M ", .width = 5, .alignment = 'C' }, - { .header = " T ", .width = 5, .alignment = 'C' }, - { .header = " W ", .width = 5, .alignment = 'C' }, - { .header = " T ", .width = 5, .alignment = 'C' }, - { .header = " F ", .width = 5, .alignment = 'C' }, - { .header = " S ", .width = 5, .alignment = 'C' }, - { .header = " # ", .width = 5, .alignment = 'C' }, - } + layouts[Layouts.COMPACT] = .{ + archive_title = " Archive ", + columns = .[ + .{ header = #run join(" TTT ", VERSION, " "), width = -1, alignment = #char "L" }, + .{ header = " S ", width = 5, alignment = #char "C" }, + .{ header = " M ", width = 5, alignment = #char "C" }, + .{ header = " T ", width = 5, alignment = #char "C" }, + .{ header = " W ", width = 5, alignment = #char "C" }, + .{ header = " T ", width = 5, alignment = #char "C" }, + .{ header = " F ", width = 5, alignment = #char "C" }, + .{ header = " S ", width = 5, alignment = #char "C" }, + .{ header = " # ", width = 5, alignment = #char "C" }, + ] }; // Calculate alignment_offsets. - for (layout_st *layout = layouts; layout < layouts + NUM_LAYOUTS; layout++) { - for (column_st *col = layout->columns; col < layout->columns + NUM_COLUMNS; col++) { - int offset; - switch(col->alignment) { - default: - case 'L': + for * layout: layouts { + for * col: layout.columns { + offset: int; + if col.alignment == { + case #char "L"; offset = 0; - break; - - case 'C': - offset = ((col->width - strlen(col->header)) / 2); - break; - - case 'R': - offset = (col->width - strlen(col->header)); - break; + case #char "C"; + offset = ((col.width - col.header.count) / 2); + case #char "R"; + offset = (col.width - col.header.count); } - col->alignment_offset = offset; + col.alignment_offset = offset; } } - setlocale(LC_ALL, "C.UTF-8"); // Sets locale for C library functions; Allows usage of UTF-8. - initscr(); // Start curses mode. + // TODO + //setlocale(LC_ALL, "C.UTF-8"); // Sets locale for C library functions; Allows usage of UTF-8. + stdscr = initscr(); // Start curses mode. cbreak(); // Line buffering disabled; pass on everty thing to me. - keypad(stdscr, TRUE); // I need that nifty F1. + keypad(stdscr, true); // I need those nifty F1..F12. curs_set(0); // Set cursor invisible. noecho(); // Disable echoing input characters. // Initialize pairs of colors. start_color(); use_default_colors(); // Using default (-1) instead of COLOR_BLACK. - init_pair(STYLE_SELECTED, COLOR_BLACK, COLOR_CYAN); - init_pair(STYLE_SELECTED_INVERTED, COLOR_CYAN, -1); - init_pair(STYLE_ACTIVE, COLOR_BLUE, -1); - init_pair(STYLE_ACTIVE_SELECTED, COLOR_WHITE, COLOR_BLUE); - init_pair(STYLE_ERROR, COLOR_RED, -1); + init_pair(xx Styles.STYLE_SELECTED, COLOR_BLACK, COLOR_CYAN); + init_pair(xx Styles.STYLE_SELECTED_INVERTED, COLOR_CYAN, -1); + init_pair(xx Styles.STYLE_ACTIVE, COLOR_BLUE, -1); + init_pair(xx Styles.STYLE_ACTIVE_SELECTED, COLOR_WHITE, COLOR_BLUE); + init_pair(xx Styles.STYLE_ERROR, COLOR_RED, -1); } +/* void update_layout() { // Calculate number of available rows to display tasks. layout_tasks_rows = (size_y - NUM_HEADER_ROWS - NUM_FOOTER_ROWS); @@ -1472,13 +1468,12 @@ main :: () { } } - if (load_database(*database, db_file_path) == false) { print_error("Failed to load database."); exit(1); } - //initialize_tui(); + initialize_tui(); /* // TODO Remove this?! //signal(SIGTERM, exit_gracefully); @@ -1868,32 +1863,31 @@ main :: () { timeout(INPUT_TIMEOUT_MS); } - + */ // Save any unsaved changes. - show_processing(); - bool error_saving = false; - if (db == &archive) { - if (export_to_csv(&archive, ar_file_path) == false) { - print_error("Failed to save archive."); - error_saving |= true; - } - } - if (countdown_to_autosave > 0 || is_autosave_enabled == false) { - if (store_database(&database, db_file_path) == false) { - print_error("Failed to save database."); - error_saving |= true; - } - } - if (error_saving) { - print_error("Press any key to close."); - draw_error_window(); - timeout(INPUT_AWAIT_INF); - getch(); - } +// show_processing(); + error_saving := false; +// if (db == &archive) { +// if (export_to_csv(&archive, ar_file_path) == false) { +// print_error("Failed to save archive."); +// error_saving |= true; +// } +// } +// if (countdown_to_autosave > 0 || is_autosave_enabled == false) { +// if (store_database(&database, db_file_path) == false) { +// print_error("Failed to save database."); +// error_saving |= true; +// } +// } +// if (error_saving) { +// print_error("Press any key to close."); +// draw_error_window(); +// timeout(INPUT_AWAIT_INF); +// getch(); +// } endwin(); - return error_saving ? EXIT_FAILURE : EXIT_SUCCESS; - */ + exit(xx ifx error_saving then 1 else 0); } |
