-
Notifications
You must be signed in to change notification settings - Fork 2
/
widget_log.c
153 lines (133 loc) · 3.01 KB
/
widget_log.c
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
#define _GNU_SOURCE
#include <stdio.h>
#include <stdlib.h>
#include <stdarg.h>
#include <string.h>
#include <assert.h>
#include <time.h>
#include "termio.h"
#include "circbuf.h"
// #define DBG(...) do {FILE *f__=fopen("/dev/ttys002","w"); fprintf(f__,__VA_ARGS__); fclose(f__);} while(0)
#define DBG(...)
struct Logwidget{
Circbuf *cb;
int nrows;
int x,y,w,h;
char *title;
bool timestamps;
};
static void lgw_drawborder(Logwidget *lgw){
moveto(lgw->x,lgw->y);
tputc('+');
tputc('-');
int len=tprintf("%s",lgw->title);
for(int i=len+2;i<lgw->w-1;i++)tputc('-');
tputc('+');
for(int i=1;i<lgw->h-1;i++){
moveto(lgw->x,lgw->y+i);
tputc('|');
moveto(lgw->x+lgw->w-1,lgw->y+i);
tputc('|');
}
moveto(lgw->x,lgw->y+lgw->h-1);
tputc('+');
for(int i=1;i<lgw->w-1;i++)tputc('-');
tputc('+');
}
Logwidget* lgw_make(int x,int y,int w,int h,const char *title,bool timestamps){
assert(x>=0&&y>=0);
assert(w>=3&&h>=3);
Logwidget *lgw=malloc(sizeof(Logwidget));
if(!lgw)return NULL;
lgw->cb=cb_make(h-2,free);
if(!lgw->cb){
free(lgw);
return NULL;
}
lgw->nrows=h-2;
lgw->x=x;
lgw->y=y;
lgw->w=w;
lgw->h=h;
lgw->title=NULL;
lgw_changetitle(lgw,title);
lgw->timestamps=timestamps;
return lgw;
}
void lgw_destroy(Logwidget *lgw){
cb_destroy(lgw->cb);
if(lgw->title)free(lgw->title);
free(lgw);
}
void lgw_redraw(Logwidget *lgw){
pushcursor();
lgw_drawborder(lgw);
int len=cb_length(lgw->cb);
DBG("len=%d nrows=%d\n",len,lgw->nrows);
int i;
for(i=0;i<len;i++){
moveto(lgw->x+1,lgw->y+1+i);
char *line=cb_get(lgw->cb,i);
int linelen=tprintf("%s",line);
DBG("line=<%s> linelen=%d\n",line,linelen);
for(int j=linelen;j<lgw->w-2;j++)tputc(' ');
}
for(;i<lgw->nrows;i++){
moveto(lgw->x+1,lgw->y+1+i);
for(int j=0;j<lgw->w-2;j++)tputc(' ');
}
popcursor();
}
static const char* formatTime(time_t tt) {
static char buf[6];
struct tm *timeinfo=localtime(&tt);
strftime(buf,6,"%H:%M",timeinfo);
return buf;
}
void lgw_add(Logwidget *lgw,const char *line){
char *allocLine=NULL;
if(lgw->timestamps){
asprintf(&allocLine,"%s %s",formatTime(time(NULL)),line);
assert(allocLine!=NULL);
line=allocLine;
}
int len=strlen(line);
while(len>0){
int copylen=lgw->w-2;
if(copylen>len)copylen=len;
char *copy=calloc(copylen+1,1);
assert(copy);
for(int i=0,j=0;i<len&&j<copylen;i++){
if(line[i]>=32&&line[i]<127)copy[j++]=line[i];
}
cb_append(lgw->cb,copy);
len-=copylen;
line+=copylen;
}
if(allocLine!=NULL){
free(allocLine);
}
lgw_redraw(lgw);
}
__attribute__((format (printf, 2,3))) void lgw_addf(Logwidget *lgw,const char *format,...){
char *str;
va_list ap;
va_start(ap,format);
vasprintf(&str,format,ap);
va_end(ap);
assert(str);
lgw_add(lgw,str);
free(str);
}
void lgw_clear(Logwidget *lgw){
cb_clear(lgw->cb);
lgw_redraw(lgw);
}
void lgw_changetitle(Logwidget *lgw,const char *title){
if(lgw->title)free(lgw->title);
if(title==NULL)title="";
lgw->title=strdup(title);
assert(lgw->title);
if((int)strlen(lgw->title)>lgw->w-2)lgw->title[lgw->w]='\0';
lgw_redraw(lgw);
}