aboutsummaryrefslogtreecommitdiff
diff options
context:
space:
mode:
-rw-r--r--modules/TUI/module.jai2
-rw-r--r--modules/UTF8.jai40
-rw-r--r--ttt.jai38
3 files changed, 44 insertions, 36 deletions
diff --git a/modules/TUI/module.jai b/modules/TUI/module.jai
index 7c3a71d..a5db3bf 100644
--- a/modules/TUI/module.jai
+++ b/modules/TUI/module.jai
@@ -572,7 +572,7 @@ read_input_line :: (count_limit: int, is_visible: bool = true) -> string, Key {
idx += 1;
// Truncate string to avoid incomplete utf8 codes on the string tail.
- truncate(*str, count_limit);
+ str.count = truncate(str, count_limit).count;
}
}
diff --git a/modules/UTF8.jai b/modules/UTF8.jai
index b583809..205e57d 100644
--- a/modules/UTF8.jai
+++ b/modules/UTF8.jai
@@ -21,15 +21,15 @@ count_character_bytes :: inline (leading_byte: u8) -> int {
return 1;
}
-// Truncates the string to the provided length and zeroes the discarded bytes.
-// Returns the length of truncated string or -1 if string has no data.
-truncate :: (str: *string, length: int) -> length: int {
- if str.data == null then return -1;
+// Returns the string (using same str.data) truncated to the provided length.
+truncate :: (str: string, length: int) -> string {
- if str.count < length then length = str.count;
+ if str.data == null then return "";
+
+ if str.count < length then return .{str.count, str.data};
- data := str.data;
- count := str.count;
+ data := str.data;
+ count := str.count;
// Find index of first continuation byte.
idx := length;
@@ -50,12 +50,10 @@ truncate :: (str: *string, length: int) -> length: int {
&& !(continuation_bytes == 2 && (data[idx - 1] & 0xF0) == 0xE0)
&& !(continuation_bytes == 3 && (data[idx - 1] & 0xF8) == 0xF0)
) {
- length -= (continuation_bytes + 1); // Remove start byte, ence '+ 1'.
+ length -= (continuation_bytes + 1); // Remove start byte, hence '+ 1'.
}
-
- memset(data + length, 0, count - length);
- str.count = length;
- return length;
+
+ return .{length, str.data};
}
// Returns true when the string is empty or consists of space characters.
@@ -78,13 +76,23 @@ is_empty :: (str: string) -> bool {
}
// Counts the number of characters.
-count_characters :: (str: string) -> int {
+count_characters :: (str: string, $is_null_terminated := false) -> int {
characters := 0;
idx := 0;
- while idx < str.count {
- idx += count_character_bytes(str[idx]);
- characters += 1;
+
+ #if is_null_terminated {
+ while idx < str.count && str[idx] != 0 {
+ idx += count_character_bytes(str[idx]);
+ characters += 1;
+ }
+ }
+ else {
+ while idx < str.count {
+ idx += count_character_bytes(str[idx]);
+ characters += 1;
+ }
}
+
return characters;
}
diff --git a/ttt.jai b/ttt.jai
index d8233f9..7d1559b 100644
--- a/ttt.jai
+++ b/ttt.jai
@@ -719,8 +719,8 @@ import_from_csv :: (db: *Database, path: string) -> bool {
csv_values := split(line, ",",, temporary_allocator);
// Truncate and import task name.
- truncate(*csv_values[0], task.name.count);
- memcpy(task.name.data, csv_values[0].data, csv_values[0].count);
+ task_name := truncate(csv_values[0], task.name.count);
+ memcpy(task.name.data, task_name.data, task_name.count);
advance(*csv_values);
for csv_values
@@ -901,11 +901,11 @@ draw_user_interface :: (db: *Database, layout: *Layout) {
start := current_time_monotonic(); // DEBUG
-
-
-
- // TODO During dirty_flag revamp...we may also start using the String_Builder.
-
+ auto_release_temp();
+
+ empty_line := talloc_string(size_x);
+ memset(empty_line.data, #char " ", size_x);
+
adjust_first_day_of_week := int.[
(0 + FIRST_DAY_OF_WEEK) % NUM_WEEK_DAYS,
(1 + FIRST_DAY_OF_WEEK) % NUM_WEEK_DAYS,
@@ -1004,6 +1004,7 @@ draw_user_interface :: (db: *Database, layout: *Layout) {
idx_start := (db.selected_idx / layout_tasks_rows) * layout_tasks_rows;
// Display up to rows allowed by the layout, or less if reached end of database.
idx_stop := idx_start + (ifx layout_tasks_rows > db.tasks.count - idx_start then db.tasks.count - idx_start else layout_tasks_rows);
+
for task_idx: idx_start..idx_stop-1 {
task := *db.tasks[task_idx];
y += 1;
@@ -1026,19 +1027,18 @@ draw_user_interface :: (db: *Database, layout: *Layout) {
// Task title.
x += 1;
column_width = layout.columns[L_TITLE_IDX].width;
- // mvprintw(xx y, xx x, "%-*.*s", column_width, column_width, temp_c_string(xx task.name)); //task.name); TODO Fix required for LLVM/Cncurses. TODO DAM
- // TODO FIXME OH MY GOD SUCH BAD CODEZ
+ // Print title.
+ // When the column is wider than the name, we end up with the trailing zeros.
+ // Thankfully, the trailing zeros are not printed so, it's all good.
+ task_name := cast(string)task.name;
+ task_name = truncate(task_name, column_width);
TUI.set_cursor_position(x, y);
- white_spaces := column_width;
- // FIXME Improve by using buffer instead of printing one char at the time.
- while white_spaces > 0 {
- print_character(#char " ");
- white_spaces -= 1;
- }
- TUI.set_cursor_position(x, y);
- task_name: string = cast(string)task.name;
- task_name.count = ifx task_name.count > column_width then column_width else task_name.count;
- print("%", task_name);
+ write_string(task_name);
+ // Paint the remaining column space.
+ task_name_char_count := count_characters(task_name, is_null_terminated = true);
+ paint_remaining := string.{ column_width - task_name_char_count, empty_line.data };
+ write_string(paint_remaining);
+
x += column_width;
// Task times.