padding in statusbar

This commit is contained in:
Babz 2021-09-12 23:33:51 +02:00
parent 61ae899bc0
commit 304ad19c3e
1 changed files with 29 additions and 2 deletions

View File

@ -14,6 +14,27 @@ static void date_str(char *buf) {
sprintf(buf, "%04d-%02d-%02d %02d:%02d:%02d", t.year, t.month, t.week_day, t.hours, t.minutes, t.seconds);
}
static void str_pad(char *buf, const char *prefix, const char *suffix, int wanted_len) {
const int l1 = strlen(prefix);
const int l2 = strlen(suffix);
const int toadd = wanted_len - (l1 + l2);
for (int i = 0; i < l1; i++) {
buf[i] = prefix[i];
}
for (int i = 0; i < toadd; i++) {
buf[l1 + i] = ' ';
}
for (int i = 0; i < l2; i++) {
buf[l1 + toadd + i] = suffix[i];
}
buf[wanted_len] = '\0';
}
void set_statusbar(int tick_ctr, int shift_state, int alpha_state, int battery) {
// set green bar
for (int i = 0; i < UNS_TERM_COLS; i++)
@ -25,9 +46,15 @@ void set_statusbar(int tick_ctr, int shift_state, int alpha_state, int battery)
char now[32];
date_str(now);
// then add actutal text
char prefix[UNS_TERM_COLS + 1];
sprintf(prefix, "%s%s t=%d bat=%.2fV", shift_symbol, alpha_symbol, tick_ctr, (float)battery / 100);
char suffix[UNS_TERM_COLS + 1];
sprintf(suffix, "%s", now);
char statusbar[UNS_TERM_COLS + 1];
sprintf(statusbar, "%s%s t=%d bat=%.2fV %s", shift_symbol, alpha_symbol, tick_ctr, (float)battery / 100, now);
str_pad(statusbar, prefix, suffix, UNS_TERM_COLS);
term_writeat(0, 0, C_BLACK, C_GREEN, statusbar);
}