From 6137ac4219b98c412f77ebc3c70ae196c13641ee Mon Sep 17 00:00:00 2001 From: dam Date: Fri, 22 Dec 2023 10:30:44 +0000 Subject: Fixed bug in count_digits. Converted print_time to TUI. --- ttt.jai | 65 +++++++++++++++++++++++++++++++++++++++++++++-------------------- 1 file changed, 45 insertions(+), 20 deletions(-) (limited to 'ttt.jai') diff --git a/ttt.jai b/ttt.jai index 11bf9f4..c411d81 100644 --- a/ttt.jai +++ b/ttt.jai @@ -182,8 +182,13 @@ is_equal_to_any :: (to_compare :string, test_a :string, test_b :string) -> bool // Count digits required to represent number on base. Sign is discarded. count_digits :: (number: s64, base: s64 = 10) -> s64 { - if number == 0 return 1; - return cast(s64) floor( log(cast(float64)abs(number)) / log(cast(float64)abs(base)) ) + 1; + assert(base > 1, "The smallest integer base for a number system is 2."); + digits := 0; + while number != 0 { + number /= base; + digits += 1; + } + return digits; } Text_Encoding :: enum u8 #specified { @@ -250,7 +255,7 @@ is_empty_string :: (str: string) -> bool { // Prints, on row y and column x, the time using 5 characters centered on space. // Returns the result of a call to mvprintw. -mvprintw_time :: (y: s32, x: s32, time: s64, space: s32) -> int { +print_time :: (y: int, x: int, time: s64, space: int) -> int { TIME_CHARS :: 5; assert(space >= TIME_CHARS); @@ -260,24 +265,42 @@ mvprintw_time :: (y: s32, x: s32, time: s64, space: s32) -> int { left_padding := (space - TIME_CHARS) / 2; right_padding := space - TIME_CHARS - left_padding; + + print_padding :: (size: int, char: u8 = #char " ") { + assert(size >= 0, "Cannot print negative padding values. The procedure accepts signed values just for convenience."); + while size > 0 { + print_character(char); + size -= 1; + } + } + + TUI.set_cursor_position(y, x); if time < 0 { + print_padding(left_padding); + write_string(" - "); + print_padding(right_padding); return 0; - // return mvprintw(y, x, "%*s - %*s", left_padding, "", right_padding, ""); TODO DAM } else if time == 0 { + print_padding(left_padding); + write_string(" 0 "); + print_padding(right_padding); return 0; - // return mvprintw(y, x, "%*s 0 %*s", left_padding, "", right_padding, ""); TODO DAM } else if time < SECONDS_IN_MINUTE { + print_padding(left_padding); + print("%s ", FormatInt.{value = time, minimum_digits=3, padding=#char " "}); + print_padding(right_padding); return 0; - // return mvprintw(y, x, "%*s%3jds %*s", left_padding, "", time, right_padding, ""); TODO DAM } else if time < #run mul_f64_s64(100, SECONDS_IN_HOUR) { hours := time / SECONDS_IN_HOUR; minutes := (time - (hours * SECONDS_IN_HOUR) ) / SECONDS_IN_MINUTE; + print_padding(left_padding); + print("%:%", FormatInt.{value = hours, minimum_digits=2}, FormatInt.{value = minutes, minimum_digits=2}); + print_padding(right_padding); return 0; - // return mvprintw(y, x, "%*s%02jd:%02jd%*s", left_padding, "", hours, minutes, right_padding, ""); TODO DAM } else if time < #run mul_f64_s64(9999.5, SECONDS_IN_DAY) { value := cast(float64) time / SECONDS_IN_DAY; @@ -285,8 +308,10 @@ mvprintw_time :: (y: s32, x: s32, time: s64, space: s32) -> int { ifx time >= #run mul_f64_s64(99.95, SECONDS_IN_DAY) then 0 else ifx time >= #run mul_f64_s64(9.995, SECONDS_IN_DAY) then 1 else 2; + print_padding(left_padding); + print("%d", FormatFloat.{value = value, trailing_width=decimals, width=4}); + print_padding(right_padding); return 0; - // return mvprintw(y, x, "%*s%4.*fd%*s", left_padding, "", decimals, value, right_padding, ""); TODO DAM } else if time < #run mul_f64_s64(9999.5, SECONDS_IN_YEAR) { value := cast(float64) time / SECONDS_IN_YEAR; @@ -294,13 +319,16 @@ mvprintw_time :: (y: s32, x: s32, time: s64, space: s32) -> int { ifx time >= #run mul_f64_s64(99.95, SECONDS_IN_YEAR) then 0 else ifx time >= #run mul_f64_s64(9.995, SECONDS_IN_YEAR) then 1 else 2; + print_padding(left_padding); + print("%y", FormatFloat.{value = value, trailing_width=decimals, width=4}); + print_padding(right_padding); return 0; - // return mvprintw(y, x, "%*s%4.*fy%*s", left_padding, "", decimals, value, right_padding, ""); TODO DAM } else { - // TODO Set back the unicode emoji once ncurses has been replaced. + print_padding(left_padding); + write_string(" ∞ "); + print_padding(right_padding); return 0; - // return mvprintw(y, x, "%*s ∞ %*s", left_padding, "", right_padding, ""); TODO DAM } } @@ -1064,13 +1092,13 @@ draw_tui :: (db: *Database, layout: *Layout) { column_width = layout.columns[L_DAYS_IDX + day_idx].width; task_time := task.times[day_idx]; total_time = add(total_time, task_time); - mvprintw_time(xx y, xx x, task_time, xx column_width); + print_time(y, x, task_time, column_width); x += column_width; } // Task total. x += 1; - mvprintw_time(xx y, xx x, total_time, xx layout.columns[L_TOTAL_IDX].width); + print_time(y, x, total_time, layout.columns[L_TOTAL_IDX].width); // Reset theme. //attrset(A_NORMAL); TODO DAM @@ -1079,22 +1107,19 @@ draw_tui :: (db: *Database, layout: *Layout) { /////////////////////////////////////////////////////////////////////////// // Draw selected/total tasks. - // TODO Something is wrong here... sometimes it only changes to the small format on the 1001 instead of 1000. FIXME - size := 1 + count_digits(db.selected_idx + 1) + 1 + count_digits(db.tasks.count) + 1; // " XXX/YYY " + size := 1 + count_digits(db.selected_idx + 1) + 1 + count_digits(db.tasks.count) + 1; // " XXX/YYY " TUI.set_cursor_position(size_y, 2); if (size <= layout.columns[L_TITLE_IDX].width) { - // mvprintw(size_y - 1, 1, " %td/%zd ", db.selected_idx + 1, db.tasks.count); TODO DAM print(" %/% ", db.selected_idx + 1, db.tasks.count); } else { - // mvprintw(size_y - 1, 1, "%td", db.selected_idx + 1); TODO DAM print("%", db.selected_idx + 1); } /////////////////////////////////////////////////////////////////////////// // Draw daily totals. - y = size_y - 1; + y = size_y; x = 0 + 1 + layout.columns[L_TITLE_IDX].width; total_time = 0; for 0..NUM_WEEK_DAYS-1 { @@ -1112,14 +1137,14 @@ draw_tui :: (db: *Database, layout: *Layout) { column_width = layout.columns[L_DAYS_IDX + idx].width; total_time = add(total_time, daily_total); - mvprintw_time(xx y, xx x, daily_total, xx column_width); + print_time(y, x, daily_total, column_width); x += column_width; // Reset theme. //attrset(A_NORMAL); TODO DAM } x += 1; - mvprintw_time(xx y, xx x, total_time, xx layout.columns[L_TOTAL_IDX].width); + print_time(y, x, total_time, layout.columns[L_TOTAL_IDX].width); } free_memory :: () { -- cgit v1.2.3