-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathterm_printf.c
59 lines (51 loc) · 988 Bytes
/
term_printf.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
#include "term.h"
#include <stdarg.h>
void term_printf(struct terminfo *term, char *format, ...) {
va_list va;
va_start(va, format);
while(*format != '\0') {
switch(*format) {
case '%':
++format;
switch(*format) {
case 'x':
term_print_hex(term, va_arg(va, int));
break;
case '\0':
goto end;
}
break;
case ' ':
term->x++;
if(term->x == term->width) {
term->x = 0;
term->y++;
if(term->y == term->height)
term->y--;
}
break;
case '\n':
term->x = 0;
term->y++;
if(term->y == term->height)
term->y--;
break;
default:
term_print_char(term, *format);
}
++format;
}
end:
va_end(va);
}
void term_print_hex(struct terminfo *term, unsigned int number) {
char str[9];
int i = 8;
str[8] = '\0';
do {
unsigned short digit = number & 0xf;
str[--i] = digit + (digit >= 0xa ? 0x57 : 0x30);
number >>= 4;
} while(number != 0);
term_printf(term, str + i);
}