removed the segment feature all over again - iomenu - interactive terminal-based selection menu
(HTM) git clone git://bitreich.org/iomenu git://enlrupgkhuxnvlhsf6lc3fziv5h2hhfrinws65d7roiv6bfj7d652fid.onion/iomenu
(DIR) Log
(DIR) Files
(DIR) Refs
(DIR) Tags
(DIR) README
(DIR) LICENSE
---
(DIR) commit 8850bebc9c87d3331bcfcf6139f05821c6f1237e
(DIR) parent 0502e73cca4f319ac484f66983cf6d40bdf843da
(HTM) Author: Josuah Demangeon <mail@josuah.net>
Date: Mon, 30 Oct 2017 00:41:32 +0100
removed the segment feature all over again
Diffstat:
M control.c | 59 +++----------------------------
M display.c | 70 +++++++++++--------------------
M iomenu.1 | 5 +----
M main.c | 2 +-
4 files changed, 32 insertions(+), 104 deletions(-)
---
(DIR) diff --git a/control.c b/control.c
@@ -15,45 +15,6 @@
#define ALT(char) ((char) + 0x80)
#define CSI(char) ((char) + 0x80 + 0x80)
-static size_t
-width(char *s)
-{
- int width = 0;
-
- while (*s) {
- if (*s++ == '\t')
- width += 8 - (width % 8);
- else
- width++;
- }
-
- return width;
-}
-
-int
-prev_page(int pos)
-{
- int col;
- int cols = ws.ws_col - MARGIN - 4;
-
- pos -= pos > 0 ? 1 : 0;
- for (col = 0; pos > 0; pos--)
- if ((col += width(matchv[pos]) + 2) > cols)
- return pos + 1;
- return pos;
-}
-
-int
-next_page(int pos)
-{
- int col, cols = ws.ws_col - MARGIN - 4;
-
- for (col = 0; pos < matchc; pos++)
- if ((col += width(matchv[pos]) + 2) > cols)
- return pos;
- return pos;
-}
-
void
move(signed int sign)
{
@@ -72,21 +33,11 @@ move_page(signed int sign)
{
int i;
- if (opt['l'] <= 0) {
- if (sign > 0) {
- offset = current = next;
- next = next_page(next);
- } else if (sign < 0) {
- next = offset;
- offset = current = prev_page(offset);
- }
- } else {
- i = current - current % rows + rows * sign;
- if (!(0 < i && i < matchc))
- return;
- current = i - 1;
- move(+1);
- }
+ i = current - current % rows + rows * sign;
+ if (!(0 < i && i < matchc))
+ return;
+ current = i - 1;
+ move(+1);
}
static void
(DIR) diff --git a/display.c b/display.c
@@ -40,60 +40,40 @@ format(char *str, int cols)
}
static void
-print_lines(void)
+print_line(char *line, int cur)
{
- int printed = 0, i = current - current % rows;
-
- for (; printed < rows && i < matchc; i++, printed++) {
- fprintf(stderr,
- opt['#'] && matchv[i][0] == '#' ?
- "\n\x1b[1m\x1b[K %s\x1b[m" :
- i == current ?
- "\n\x1b[47;30m\x1b[K %s\x1b[m" :
- "\n\x1b[K %s",
- format(matchv[i], ws.ws_col - 1)
- );
+ if (opt['#'] && line[0] == '#') {
+ format(line + 1, ws.ws_col - 1);
+ fprintf(stderr, "\n\x1b[1m\x1b[K %s\x1b[m", formatted);
+ } else if (cur) {
+ format(line, ws.ws_col - 1);
+ fprintf(stderr, "\n\x1b[47;30m\x1b[K %s\x1b[m", formatted);
+ } else {
+ format(line, ws.ws_col - 1);
+ fprintf(stderr, "\n\x1b[K %s", formatted);
}
- while (printed++ < rows)
- fputs("\n\x1b[K", stderr);
- fprintf(stderr, "\x1b[%dA\r\x1b[K", rows);
-}
-
-static void
-print_segments(void)
-{
- int i;
-
- if (current < offset) {
- next = offset;
- offset = prev_page(offset);
- } else if (current >= next) {
- offset = next;
- next = next_page(offset);
- }
- fprintf(stderr, "\r\x1b[K\x1b[%dC", MARGIN);
- fputs(offset > 0 ? "< " : " ", stderr);
- for (i = offset; i < next && i < matchc; i++) {
- fprintf(stderr,
- opt['#'] && matchv[i][0] == '#' ? "\x1b[1m %s \x1b[m" :
- i == current ? "\x1b[7m %s \x1b[m" : " %s ",
- format(matchv[i], ws.ws_col - 1)
- );
- }
- if (next < matchc)
- fprintf(stderr, "\x1b[%dC\b>", ws.ws_col - MARGIN);
- fputc('\r', stderr);
}
void
print_screen(void)
{
+ char **m;
+ int p;
+ int i;
int cols = ws.ws_col - 1;
- if (opt['l'] > 0)
- print_lines();
- else
- print_segments();
+ p = 0;
+ i = current - current % rows;
+ m = matchv + i;
+ while (p < rows && i < matchc) {
+ print_line(*m, i == current);
+ p++, i++, m++;
+ }
+ while (p < rows) {
+ fputs("\n\x1b[K", stderr);
+ p++;
+ }
+ fprintf(stderr, "\x1b[%dA\r\x1b[K", rows);
if (*prompt) {
format(prompt, cols - 2);
fprintf(stderr, "\x1b[30;47m %s \x1b[m", formatted);
(DIR) diff --git a/iomenu.1 b/iomenu.1
@@ -35,10 +35,7 @@ The selected line is printed to standard output.
.It Fl l Ar lines
If
.Ar lines
-is 0, the items are displayed in an horizontal list;
-otherwise, in a vertical list of at most
-.Ar lines
-lines.
+Total amount of lines to display on the screen.
.
.It Fl p Ar prompt
Set the prompt to display at the beginning of the input to
(DIR) diff --git a/main.c b/main.c
@@ -103,7 +103,7 @@ parse_opt(int argc, char *argv[])
usage();
switch ((*argv)[1]) {
case 'l':
- if (!--argc || sscanf(*++argv, "%d", &opt['l']) <= 0)
+ if (!--argc || (opt['l'] = atoi(*++argv)) <= 0)
usage();
break;
case 'p':