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
154
155
156
157
158
159
160
161
162
|
use tui;
use tui::widget;
use io;
use unix::tty;
use memio;
use strings;
use fmt;
export type scrolllist = struct {
widget: widget::widget,
items: []str,
frame: frame,
cursor: int,
};
// Return an instance of list. out is the tty file, pos the starting position,
// sz is the size of the widget (if void is used, the maximum possible
// size is used), items is the slice of items of the list.
export fn newscrolllist(state: *tui::tui, pos: widget::coords, sz: widget::widgetsize,
style: (*widget::style | void), items: str...) (scrolllist | tty::error) = {
const tsz = tty::winsize(state.out)?;
let end = match (sz) {
case let sz: tty::ttysize =>
const rows = if (tsz.rows < sz.rows) tsz.rows - 1 else sz.rows;
yield rows;
case void =>
yield tsz.rows - 1;
};
let end = end: int;
if (end > len(items): int) {
end = len(items): int;
};
return scrolllist {
widget = widget::widget {
state = state,
print = &printscrolllist,
resize = &resizescrolllist,
finish = &finishscrolllist,
pos = pos,
sz = sz,
style = style,
damage = widget::damageall,
...
},
items = items,
frame = frame {
start = 0,
end = end,
},
cursor = 0,
};
};
fn printscrolllist(widget: *widget::widget) void = {
const list = widget: *scrolllist;
assert(list.frame.start >= 0);
assert(list.frame.end <= len(list.items): int);
list.widget.buf = widget::linesbuf {
lines = list.items[list.frame.start..list.frame.end],
styles = &stylesscrolllist,
};
widget::print(list);
};
fn resizescrolllist(widget: *widget::widget, ttysize: tty::ttysize) void = {
return;
};
fn finishscrolllist(widget: *widget::widget) void = {
widget::finish(widget);
};
export fn down(li: *scrolllist) void = {
if (li.cursor == li.frame.end - 1) {
framedown(li);
};
if (li.cursor < len(li.items): int - 1) {
li.cursor += 1;
};
};
export fn up(li: *scrolllist) void = {
if (li.cursor == li.frame.start) {
frameup(li);
};
if (li.cursor > 0) {
li.cursor -= 1;
};
};
export fn frameup(li: *scrolllist) void = {
if (li.frame.start > 0) {
li.frame.start -= 1;
li.frame.end -= 1;
};
};
export fn framedown(li: *scrolllist) void = {
if (li.frame.end < len(li.items): int) {
li.frame.start += 1;
li.frame.end += 1;
};
};
export fn top(li: *scrolllist) void = {
const sz = li.frame.end - li.frame.start;
li.cursor = 0;
li.frame.end = sz;
li.frame.start = 0;
};
export fn bottom(li: *scrolllist) void = {
const sz = li.frame.end - li.frame.start;
li.cursor = len(li.items): int - 1;
li.frame.end = len(li.items): int;
li.frame.start = len(li.items): int - sz;
};
export fn setcursor(li: *scrolllist, newpos: int) void = {
li.cursor = newpos;
reframe(li);
};
fn reframe(li: *scrolllist) void = {
if (li.cursor > li.frame.end) {
const diff = li.frame.end - li.frame.start;
li.frame.start = if (li.cursor <= len(li.items): int - diff) li.cursor else len(li.items): int - diff;
li.frame.end = if (li.frame.start + diff > len(li.items): int) {
yield len(li.items): int;
} else {
yield li.frame.start + diff;
};
} else if (li.cursor < li.frame.start) {
const diff = li.frame.end - li.frame.start;
li.frame.end = if (li.cursor > diff) li.cursor else diff;
li.frame.start = if (li.frame.end - diff < 0) {
yield 0;
} else {
yield li.frame.end - diff;
};
};
assert(li.frame.start >= 0);
assert(li.frame.end <= len(li.items): int);
};
fn stylesscrolllist(widget: *widget::widget, txt: str, idx: size) str = {
const list = widget: *scrolllist;
const idx = idx: int + list.frame.start;
let st = memio::dynamic();
defer io::close(&st)!;
if (idx == list.cursor) {
memio::concat(&st, "\x1B[7m")!;
};
memio::concat(&st, txt)!;
if (idx == list.cursor) {
memio::concat(&st, "\x1B[27m")!;
};
return strings::dup(memio::string(&st)!);
};
|