nshell/src/ui.c

101 lines
2.6 KiB
C

#include "ui.h"
#include <printf.h>
#include <string.h>
#include <gint/display.h>
#include <gint/kmalloc.h>
#include <gint/mmu.h>
#include <gint/rtc.h>
#include "term.h"
#include "utf8.h"
static void date_str(char *buf) {
rtc_time_t t;
rtc_get_time(&t);
sprintf(buf, "%04d-%02d-%02d %02d:%02d:%02d", t.year, t.month, t.month_day, t.hours, t.minutes, t.seconds);
}
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);
const size_t toadd = wanted_len - (strlen_utf8(prefix) + strlen_utf8(suffix));
for (size_t i = 0; i < l1; i++) {
buf[i] = prefix[i];
}
for (size_t i = 0; i < toadd; i++) {
buf[l1 + i] = ' ';
}
for (size_t i = 0; i < l2; i++) {
buf[l1 + toadd + i] = suffix[i];
}
buf[l1 + l2 + toadd] = '\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++)
term_writeat(0, i, C_NONE, C_GREEN, " ");
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();
char *shift_symbol = shift_state ? "" : " ";
char *alpha_symbol = alpha_state ? (shift_state ? "A" : "a") : "1";
char prefix[UNS_TERM_COLS + 1];
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);
char now[32];
date_str(now);
char suffix[UNS_TERM_COLS + 1];
sprintf(suffix, "%s", now);
char statusbar[UNS_TERM_COLS + 1];
str_pad(statusbar, prefix, suffix, UNS_TERM_COLS);
term_writeat(0, 0, C_BLACK, C_GREEN, statusbar);
}
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);
// print spacers
if (i < 6 - 1)
term_writeat(UNS_TERM_ROWS - 1, col + 10, C_WHITE, C_BLACK, " ");
col += 10 + 1;
}
}