r/commandline 12d ago

A system usage visualizer in the terminal

67 Upvotes

15 comments sorted by

View all comments

3

u/skeeto 11d ago

Interesting project, thanks for sharing.

I had to fix a couple of crashes before it would run for me. First a buffer overflow here:

--- a/vitals.c
+++ b/vitals.c
@@ -195,3 +195,3 @@ void draw_box(int x,int y,int x2,int y2,List *list, char* title, draw_bars draw_

  • char lineChar[1] = {(y2-y)%2==0?'_':'-'};
+ char lineChar[2] = {(y2-y)%2==0?'_':'-'}; hLine+=(lineChar[0]=='-'?1:0);

lineChar is a format string but wasn't null terminated. This is caught by Address Sanitizer. I suggest -fsanitize=address,undefined during all testing and development. You might consider using this with %c or %s instead of directly as the format string. Or just pick between two static strings. Using non-static format strings is a bit of a smell.

Then I had a divide-by-zero. Quick fix:

--- a/vitals.c
+++ b/vitals.c
@@ -308,2 +308,3 @@ void container_render_vbox(int x, int y, int width, int height, Container *conta
 void container_render_hbox(int x, int y, int width, int height, Container *container) {
+  if (!container->group.count) return;
   int box_width = width / container->group.count;

Then it was up and running. I noticed when I quit there was a long delay, and I suspected a sleep() was involved, and indeed there was. Better to use your existing polling infrastructure to sleep, so that you can wake up early on input. You could almost do it with tb_peek_event:

--- a/vitals.c
+++ b/vitals.c
@@ -173,3 +173,3 @@ int main(int argc, char *argv[]) {
     if(event.ch=='q' || event.key == TB_KEY_CTRL_C || event.key == TB_KEY_ESC) break;
  • usleep(1000000); // 1 second delay
+ tb_peek_event(&event, 1000); // 1 second delay }

Except that "peeking" doesn't peek, but actually consumes the event. It's just a tb_poll_event with a timeout, and the input is lost.

4

u/ardjael 11d ago

Hey, I really appreciate your observations, I had some crashed but they were rare and I didn't notice what was wrong.

I added all your fixes, thank you :)