1 /**
2  * A layout manager for ncurses.
3  */
4 
5 #include <string.h>
6 
7 #include "ui-layout.h"
8 
set_coords(struct coords * co,int top,int left,int bottom,int right)9 extern void set_coords(struct coords *co, int top, int left, int bottom, int right)
10 {
11     co->top = top, co->left = left, co->bottom = bottom, co->right = right;
12     co->height = bottom - top, co->width = right - left;
13 }
14 
mvwhsplit(WINDOW * win,int y,int x,int len)15 extern void mvwhsplit(WINDOW *win, int y, int x, int len)
16 {
17     mvwaddch(win, y, x, ACS_LTEE);
18     mvwhline(win, y, x + 1, ACS_HLINE, len - 2);
19     mvwaddch(win, y, x + len - 1, ACS_RTEE);
20 }
21 
mvwvsplit(WINDOW * win,int y,int x,int len)22 extern void mvwvsplit(WINDOW *win, int y, int x, int len)
23 {
24     mvwaddch(win, y, x, ACS_TTEE);
25     mvwvline(win, y + 1, x, ACS_VLINE, len - 2);
26     mvwaddch(win, y + len - 1, x, ACS_BTEE);
27 }
28 
mvwaddlstr(WINDOW * win,int y,int x,const char * s,int maxlen)29 extern void mvwaddlstr(WINDOW *win, int y, int x, const char *s, int maxlen)
30 {
31     int len = (int) strlen(s);
32     wmove(win, y, x);
33     if (len > maxlen) {
34         while (maxlen > 3 && *s != '\0') {
35             waddch(win, *s++);
36             maxlen--;
37         }
38         while (maxlen > 0) {
39             waddch(win, '.');
40             maxlen--;
41         }
42     } else {
43         waddstr(win, s);
44     }
45 }
46 
int_min(int a,int b)47 static int int_min(int a, int b)
48 {
49     return (a < b) ? a : b;
50 }
51 
input_box(char * buf,size_t bufsize,const char * title)52 extern int input_box(char *buf, size_t bufsize, const char *title)
53 {
54     struct coords input;
55     int hlen, maxy, maxx, result;
56 
57     getmaxyx(stdscr, maxy, maxx);
58     set_coords(&input, maxy / 2 - 1, 10, maxy / 2 + 2, maxx - 10);
59     hlen = int_min(strlen(title), (input.right - input.left) - 4);
60 
61     cbreak(), echo(), leaveok(stdscr, FALSE);
62 
63     mvaddch(input.top, input.left, ACS_ULCORNER);
64     hline(ACS_HLINE, input.width - 2);
65     mvaddch(input.top, input.right - 1, ACS_URCORNER);
66     mvaddch(input.top + 1, input.left, ACS_VLINE);
67     hline(' ', input.width - 2);
68     mvaddch(input.top + 1, input.right - 1, ACS_VLINE);
69     mvaddch(input.bottom - 1, input.left, ACS_LLCORNER);
70     mvhline(input.bottom - 1, input.left + 1, ACS_HLINE, input.width - 2);
71     mvaddch(input.bottom - 1, input.right - 1, ACS_LRCORNER);
72     mvaddnstr(input.top, input.left + (input.width - hlen) / 2, title, hlen);
73 
74     curs_set(1);
75     result = (mvgetnstr(input.top + 1, input.left + 1, buf, bufsize) == ERR) ? -1 : 0;
76     curs_set(0);
77 
78     noecho(), halfdelay(5), leaveok(stdscr, TRUE);
79 
80     return result;
81 }
82