aboutsummaryrefslogtreecommitdiff
path: root/misc.c
diff options
context:
space:
mode:
authordam <dam@gudinoff>2022-12-22 02:44:59 +0000
committerdam <dam@gudinoff>2022-12-22 02:44:59 +0000
commit298cfb1a4dbd58029066c3af09a48fd5ffbdb5ef (patch)
treef3c5c306ee8f81c904ef8e9568ea80a4b277b60b /misc.c
parented4c07fec790af982dbbfc40695da38da438bfb8 (diff)
downloadtask-time-tracker-298cfb1a4dbd58029066c3af09a48fd5ffbdb5ef.tar.zst
task-time-tracker-298cfb1a4dbd58029066c3af09a48fd5ffbdb5ef.zip
Reduced usage of snprintf and string_buffer by replacing them with mvprintw. Prototype code for print_error.
Diffstat (limited to 'misc.c')
-rw-r--r--misc.c48
1 files changed, 48 insertions, 0 deletions
diff --git a/misc.c b/misc.c
index 35a602c..32e4360 100644
--- a/misc.c
+++ b/misc.c
@@ -35,6 +35,54 @@
*/
+// Prints time into string using 5 characters centered on space.
+// The string buffer should be able to store space UTF8 characters plus '\0'.
+char *sprint_time5_utf8(char *string, intmax_t time, int space) {
+ const int TIME_CHARS = 5;
+ assert(space >= TIME_CHARS);
+
+ int buffer_space = space * 4 + 1; // Each that UTF8 char can have 4 bytes.
+ int left_padding = (space - TIME_CHARS) / 2;
+ int right_padding = space - TIME_CHARS - left_padding;
+
+ if (time < 0) {
+ snprintf(string, buffer_space, "%*s - %*s", left_padding, "", right_padding, "");
+ }
+ else if (time == 0) {
+ snprintf(string, buffer_space, "%*s 0 %*s", left_padding, "", right_padding, "");
+ }
+ else if (time < SECONDS_IN_MINUTE) {
+ snprintf(string, buffer_space, "%*s%3jds %*s", left_padding, "", time, right_padding, "");
+ }
+ else if (time < (intmax_t)100 * SECONDS_IN_HOUR) {
+ intmax_t hours = (double)time / (double)SECONDS_IN_HOUR;
+ intmax_t minutes = (time - (hours * SECONDS_IN_HOUR) ) / SECONDS_IN_MINUTE;
+ snprintf(string, buffer_space, "%*s%02jd:%02jd%*s", left_padding, "", hours, minutes, right_padding, "");
+ }
+ else if (time < (intmax_t)(9999.5 * SECONDS_IN_DAY)) {
+ double value = (double)time / (double)SECONDS_IN_DAY;
+ int decimals =
+ time >= 99.95 * SECONDS_IN_DAY ? 0 :
+ time >= 9.995 * SECONDS_IN_DAY ? 1 :
+ 2;
+ snprintf(string, buffer_space, "%*s%4.*fd%*s", left_padding, "", decimals, value, right_padding, "");
+ }
+ else if (time < (intmax_t)(9999.5 * SECONDS_IN_YEAR)) {
+ double value = (double)time / (double)SECONDS_IN_YEAR;
+ int decimals =
+ time >= 99.95 * SECONDS_IN_YEAR ? 0 :
+ time >= 9.995 * SECONDS_IN_YEAR ? 1 :
+ 2;
+ snprintf(string, buffer_space, "%*s%4.*fy%*s", left_padding, "", decimals, value, right_padding, "");
+ }
+ else {
+ snprintf(string, buffer_space, "%*s ∞ %*s", left_padding, "", right_padding, "");
+ }
+
+ return string;
+}
+
+
// Writes only the database core structure and the provided task if not null.
// Returns success.
bool store_database_partial(const database_st *db, const task_st *task, const char *path) {