-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathWindow.cpp
More file actions
134 lines (124 loc) · 3.46 KB
/
Window.cpp
File metadata and controls
134 lines (124 loc) · 3.46 KB
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
#include "Window.h"
ITEM** m_items;
MENU* m_menu;
void CreateMenu(const vector<string>& options)
{
initscr();
cbreak();
noecho();
start_color();
keypad(stdscr, TRUE);
m_items = new ITEM*[options.size() + 1];
int index = 0;
for (auto iter = options.begin(); iter != options.end(); ++iter)
m_items[index++] = new_item("+", iter->c_str());
m_items[index] = (ITEM *)nullptr;
m_menu = new_menu((ITEM **)m_items);
post_menu(m_menu);
}
int Waiting(const vector<string>& vec_res)
{
int key = KEY_RESIZE;
string::size_type index = 0;
do {
switch (key)
{
case KEY_DOWN:
menu_driver(m_menu, REQ_DOWN_ITEM);
if (index < vec_res.size() - 1)
++index;
break;
case KEY_UP:
menu_driver(m_menu, REQ_UP_ITEM);
if (index != 0)
--index;
break;
case KEY_RESIZE:
PrintHeadRear();
break;
case _KEY_ENTER:
return index;
}
Clear();
Print(get_file_lines(vec_res[index]));
refresh();
key = getch();
} while (key != _KEY_QUIT && key != _KEY_QUIT2);
return -1;
}
void CloseMenu()
{
int index = 0;
if (m_items[index])
free_item(m_items[index++]);
free_menu(m_menu);
endwin();
}
void Clear()
{
for (int i = _DISPLAY_TOP; i < LINES - 1; ++i)
{
move(i, 0);
clrtoeol();
}
}
void Print(pair<file_name, file_lines> name_line)
{
int total = LINES - _DISPLAY_TOP - 1;
if (total <= 0)
return;
char cmd[1024];
int lines;
sscanf(name_line.second.c_str(), "%d", &lines);
int begin = lines - total / 2 > 0 ? lines - total / 2 : 1;
int end = begin + total - 1;
char _cmd[1024];
snprintf(_cmd, sizeof(_cmd), "echo '%d %d %d' > /tmp/log", end, lines, begin);
system(_cmd);
snprintf(cmd, sizeof(cmd), "sed -n '%d,%dp' %s", begin, end, name_line.first.c_str());
FILE* fp = popen(cmd, "r");
if (!fp)
{
return;
}
init_pair(_COL_YELLOW, COLOR_YELLOW, COLOR_BLACK);
init_pair(_COL_GREEN, COLOR_GREEN, COLOR_BLACK);
char buf[1024];
int index = _DISPLAY_TOP;
unsigned int width = log10(end) + 1;
int curpos = begin;
while (fgets(buf, sizeof(buf), fp))
{
char line_num[32];
string str = strip(buf, false);
snprintf(line_num, sizeof(line_num), "%d", curpos);
if (curpos == lines)
{
wattron(stdscr, COLOR_PAIR( _COL_YELLOW));
mvprintw(index, 0, "%s%s ", width > strlen(line_num) ? " " : "", line_num);
mvprintw(index, width + 1, "%s", deltab(str).c_str());
wattroff(stdscr, COLOR_PAIR( _COL_YELLOW));
}
else
{
wattron(stdscr, COLOR_PAIR(_COL_GREEN));
mvprintw(index, 0, "%s%s ", width > strlen(line_num) ? " " : "", line_num);
wattroff(stdscr, COLOR_PAIR(_COL_GREEN));
mvprintw(index, width + 1, "%s", deltab(str).c_str());
}
++curpos;
++index;
}
curs_set(0);
pclose(fp);
}
void PrintHeadRear()
{
if (LINES < _DISPLAY_TOP)
return;
init_pair(_COL_GREEN, COLOR_GREEN, COLOR_BLACK);
wattron(stdscr, COLOR_PAIR(_COL_GREEN));
mvprintw(LINES - 1, 0, "'q' or 'Q' to exit, 'enter' to editor!");
mvprintw(_DISPLAY_TOP - 1, 1, "%s", string(COLS - 2, '=').c_str());
wattroff(stdscr, COLOR_PAIR(_COL_GREEN));
}