-
Notifications
You must be signed in to change notification settings - Fork 1
/
status_line.c
105 lines (92 loc) · 2.76 KB
/
status_line.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
#include "pch.h"
#include "pager.h"
#include "state.h"
#include "status_line.h"
#include "tui.h"
struct status_line g_statline;
void
status_line_init(void)
{
for (int cid = 0; cid < STATUS_LINE_COMPONENT_COUNT; ++cid)
{
struct status_line_component *const c = &g_statline.components[cid];
c->invalidated = true;
}
}
void
status_line_paint(void)
{
// Draw status line
for (int cid = 0; cid < STATUS_LINE_COMPONENT_COUNT; ++cid)
{
struct status_line_component *const c = &g_statline.components[cid];
// Don't need to update it
if (!c->invalidated) continue;
c->len_prev = c->len;
char text[256];
int x_pos = 0;
switch(cid)
{
case STATUS_LINE_COMPONENT_LEFT:
{
// Just display page URI for now
c->bytes = uri_str(&g_state.uri,
text, sizeof(text),
#if STATUS_LINE_FANCY_URI_FORMAT
URI_FLAGS_FANCY_BIT
#else
0
#endif
);
c->len = utf8_width(text, c->bytes);
// Don't render whole text if it exceeds terminal width
if (c->len > g_tui->w - 4)
{
c->len = g_tui->w - 4;
c->bytes = utf8_size_w_formats(text, c->len);
}
x_pos = 1;
} break;
case STATUS_LINE_COMPONENT_RIGHT:
{
int scroll_percent = (int)ceil(100.0 *
g_pager->scroll /
((int)max(g_pager->buffer.line_count, 2) - 1));
scroll_percent = max(scroll_percent, 0);
switch (scroll_percent)
{
case 0:
c->len = snprintf(text, sizeof(text),
" %s top", g_recv->mime.str);
break;
case 100:
c->len = snprintf(text, sizeof(text),
" %s bottom", g_recv->mime.str);
break;
default:
c->len = snprintf(text, sizeof(text),
" %s %d%%", g_recv->mime.str, scroll_percent);
break;
}
c->bytes = c->len;
x_pos = g_tui->w - c->len + 1;
} break;
default: continue;
}
// Draw the new text
tui_cursor_move(x_pos, g_tui->h - 1);
tui_say(COLOUR_STATUS_LINE);
tui_sayn(text, c->bytes);
tui_say("\x1b[0m");
// Clear previous text
int clear = (int)(c->len_prev - c->len);
if (clear <= 0) continue;
if (cid == STATUS_LINE_COMPONENT_RIGHT)
{
tui_cursor_move(
max(g_tui->w - (int)c->len_prev + 1, 0),
g_tui->h - 1);
}
tui_printf("%*s", clear, "");
}
}