[go: up one dir, main page]

File: tbox.c

package info (click to toggle)
s390-tools 2.3.0-2~deb10u1
  • links: PTS
  • area: main
  • in suites: buster
  • size: 6,188 kB
  • sloc: ansic: 87,755; sh: 8,398; cpp: 8,384; perl: 3,783; makefile: 1,476; asm: 654
file content (240 lines) | stat: -rw-r--r-- 4,204 bytes parent folder | download
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
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
/*
 * hyptop - Show hypervisor performance data on System z
 *
 * Text box: Provide scrollable text window under curses.
 *
 * Copyright IBM Corp. 2010, 2017
 *
 * s390-tools is free software; you can redistribute it and/or modify
 * it under the terms of the MIT license. See LICENSE for details.
 */

#include <assert.h>
#include <ncurses.h>

#include "helper.h"
#include "hyptop.h"
#include "tbox.h"

/*
 * Delete one line
 */
static void l_line_free(struct tbox_line *line)
{
	ht_free(line->str);
	ht_free(line);
}

/*
 * Delete all lines
 */
void tbox_line_del_all(struct tbox *tb)
{
	struct tbox_line *line, *tmp;

	util_list_iterate_safe(&tb->line_list, line, tmp) {
		util_list_remove(&tb->line_list, line);
		l_line_free(line);
	}
	tb->tbox_ready = 0;
	tb->line_cnt = 0;
}

/*
 * Finish text box after all lines have been added
 */
void tbox_finish(struct tbox *tb)
{
	tb->tbox_ready = 1;
}

/*
 * Add one line to text box
 */
void tbox_line_add(struct tbox *tb, const char *str)
{
	struct tbox_line *line;

	if (strlen(str) > TBOX_MAX_STR)
		assert(0);
	line = ht_zalloc(sizeof(*line));
	line->str = ht_strdup(str);
	util_list_add_tail(&tb->line_list, line);
	tb->last_line = line;
	tb->line_cnt++;
}

/*
 * Adjust values, if we scrolled out of range
 */
static void l_adjust_values(struct tbox *tb)
{
	if (tb->line_cnt - tb->line_start < g.c.row_cnt)
		tb->line_start = MAX(tb->line_cnt - g.c.row_cnt, 0);
}

/*
 * Scroll text box down
 */
void tbox_scroll_down(struct tbox *tb, enum tbox_scroll_unit unit)
{
	switch (unit) {
	case TBOX_SCROLL_LINE:
		tb->line_start++;
		break;
	case TBOX_SCROLL_PAGE:
		tb->line_start += (g.c.row_cnt - 2);
		break;
	case TBOX_SCROLL_LAST:
		tb->line_start = tb->line_cnt;
		break;
	}
}

/*
 * Scroll text box up
 */
void tbox_scroll_up(struct tbox *tb, enum tbox_scroll_unit unit)
{
	switch (unit) {
	case TBOX_SCROLL_LINE:
		tb->line_start = MAX(tb->line_start - 1, 0);
		break;
	case TBOX_SCROLL_PAGE:
		tb->line_start = MAX(tb->line_start - (g.c.row_cnt - 2), 0);
		break;
	case TBOX_SCROLL_LAST:
		tb->line_start = 0;
		break;
	}
}

/*
 * Resize text box
 */
void tbox_term_resize(struct tbox *tb)
{
	l_adjust_values(tb);
}

/*
 * Toggle bold curses format attribute
 */
static void l_bold_toggle(void)
{
	static int bold_on;

	if (bold_on) {
		ht_bold_off();
		bold_on = 0;
	} else {
		ht_bold_on();
		bold_on = 1;
	}
}

/*
 * Toggle underline curses format attribute
 */
static void l_underline_toggle(void)
{
	static int underline_on;

	if (underline_on) {
		ht_underline_off();
		underline_on = 0;
	} else {
		ht_underline_on();
		underline_on = 1;
	}
}

/*
 * Print one line with attributes (bold and underline)
 */
static void l_print_line(const char *line)
{
	char line_cpy[TBOX_MAX_STR + 1];
	char *ptr_old, *ptr;

	strncpy(line_cpy, line, sizeof(line_cpy));
	ptr_old = ptr = line_cpy;
	do {
		ptr = strchr(ptr, '\\');
		if (ptr) {
			*ptr = 0;
			hyptop_printf("%s", ptr_old);
			switch (ptr[1]) {
			case 'B':
				l_bold_toggle();
				break;
			case 'U':
				l_underline_toggle();
				break;
			}
			ptr += 2;
			ptr_old = ptr;
		} else {
			hyptop_printf("%s", ptr_old);
			return;
		}
	} while (*ptr);
}

#ifdef WITH_SCROLL_BAR
static int l_can_scroll_down(struct tbox *tb)
{
	return (tb->line_cnt - tb->line_start > g.c.row_cnt);
}

static int l_can_scroll_up(struct tbox *tb)
{
	return (tb->line_start > 0);
}
#endif

/*
 * Print text box to screen
 */
void tbox_print(struct tbox *tb)
{
	int line_nr = 0, first = 1;
	struct tbox_line *line;

	if (!tb->tbox_ready)
		return;

	l_adjust_values(tb);
	util_list_iterate(&tb->line_list, line) {
		if (line_nr < tb->line_start) {
			line_nr++;
			continue;
		}
		/* Have we printed the whole visible screen ? */
		if (line_nr - tb->line_start >= g.c.row_cnt)
			break;
		if (first)
			first = 0;
		else
			hyptop_print_nl();
		l_print_line(line->str);
		line_nr++;
	}
#ifdef WITH_SCROLL_BAR
	ht_print_scroll_bar(tb->line_cnt, tb->line_start, 0,
				0, l_can_scroll_up(tb), l_can_scroll_down(tb),
				0);
#endif
}

/*
 * Create new text box
 */
struct tbox *tbox_new(void)
{
	struct tbox *tb;

	tb = ht_zalloc(sizeof(*tb));
	util_list_init(&tb->line_list, struct tbox_line, list);
	return tb;
}