nshell/src/ui.c

101 lines
2.6 KiB
C
Raw Permalink Normal View History

2021-09-01 11:04:31 +02:00
#include "ui.h"
#include <printf.h>
#include <string.h>
2021-09-01 11:04:31 +02:00
#include <gint/display.h>
2021-09-14 18:41:53 +02:00
#include <gint/kmalloc.h>
#include <gint/mmu.h>
2021-09-10 23:12:01 +02:00
#include <gint/rtc.h>
2021-09-01 11:04:31 +02:00
#include "term.h"
#include "utf8.h"
2021-09-01 11:04:31 +02:00
2021-09-10 23:12:01 +02:00
static void date_str(char *buf) {
rtc_time_t t;
rtc_get_time(&t);
2021-12-05 11:22:25 +01:00
sprintf(buf, "%04d-%02d-%02d %02d:%02d:%02d", t.year, t.month, t.month_day, t.hours, t.minutes, t.seconds);
2021-09-10 23:12:01 +02:00
}
2021-09-12 23:33:51 +02:00
static void str_pad(char *buf, const char *prefix, const char *suffix, int wanted_len) {
const size_t l1 = strlen(prefix);
const size_t l2 = strlen(suffix);
2021-09-12 23:33:51 +02:00
const size_t toadd = wanted_len - (strlen_utf8(prefix) + strlen_utf8(suffix));
2021-09-12 23:33:51 +02:00
for (size_t i = 0; i < l1; i++) {
2021-09-12 23:33:51 +02:00
buf[i] = prefix[i];
}
for (size_t i = 0; i < toadd; i++) {
2021-09-12 23:33:51 +02:00
buf[l1 + i] = ' ';
}
for (size_t i = 0; i < l2; i++) {
2021-09-12 23:33:51 +02:00
buf[l1 + toadd + i] = suffix[i];
}
buf[l1 + l2 + toadd] = '\0';
2021-09-12 23:33:51 +02:00
}
2021-09-12 21:18:01 +02:00
void set_statusbar(int tick_ctr, int shift_state, int alpha_state, int battery) {
2021-09-01 11:04:31 +02:00
// set green bar
for (int i = 0; i < UNS_TERM_COLS; i++)
2021-09-08 19:22:36 +02:00
term_writeat(0, i, C_NONE, C_GREEN, " ");
2021-09-01 11:04:31 +02:00
2021-09-14 18:41:53 +02:00
kmalloc_arena_t *uram_arena = kmalloc_get_arena("_uram");
kmalloc_gint_stats_t *ram_stats = kmalloc_get_gint_stats(uram_arena);
const int uram_percent = 100 * ram_stats->used_memory / mmu_uram_size();
2021-09-02 21:52:17 +02:00
char *shift_symbol = shift_state ? "" : " ";
char *alpha_symbol = alpha_state ? (shift_state ? "A" : "a") : "1";
2021-09-14 18:41:53 +02:00
char prefix[UNS_TERM_COLS + 1];
2021-09-15 18:58:15 +02:00
sprintf(prefix, "%s%s t=%d bat=%.2fV uram=%d%%(%dk)", shift_symbol, alpha_symbol, tick_ctr, (float)battery / 100,
uram_percent, ram_stats->used_memory / 1024);
2021-09-14 18:41:53 +02:00
2021-09-10 23:12:01 +02:00
char now[32];
date_str(now);
2021-09-12 23:33:51 +02:00
char suffix[UNS_TERM_COLS + 1];
sprintf(suffix, "%s", now);
2021-09-01 11:04:31 +02:00
char statusbar[UNS_TERM_COLS + 1];
2021-09-12 23:33:51 +02:00
str_pad(statusbar, prefix, suffix, UNS_TERM_COLS);
2021-09-08 19:22:36 +02:00
term_writeat(0, 0, C_BLACK, C_GREEN, statusbar);
2021-09-01 11:04:31 +02:00
}
static void center_str(int len, char buf[len], const char *s) {
const int l = strlen(s);
const int before = len / 2 - l / 2;
int i;
for (i = 0; i < before; i++)
buf[i] = ' ';
for (int j = 0; j < l; j++)
buf[i++] = s[j];
for (; i < len; i++)
buf[i] = ' ';
}
char fnkeys[6][11] = {"opt 1-", "opt 2--", "opt 3---", "opt 4----", "opt 5-----", "opt 6"};
void set_menubar(void) {
int col = 0;
for (int i = 0; i < 6; i++) {
char buf[11] = "";
center_str(10, buf, fnkeys[i]);
buf[10] = '\0';
term_writeat(UNS_TERM_ROWS - 1, col, C_WHITE, C_BLUE, buf);
2021-09-01 13:43:55 +02:00
// print spacers
if (i < 6 - 1)
term_writeat(UNS_TERM_ROWS - 1, col + 10, C_WHITE, C_BLACK, " ");
2021-09-01 13:43:55 +02:00
2021-09-01 11:04:31 +02:00
col += 10 + 1;
}
}