-
Notifications
You must be signed in to change notification settings - Fork 0
/
abob.c
177 lines (155 loc) · 4.06 KB
/
abob.c
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
/*
* Main script
*/
#include "abob.h" /* move declarations to another file */
/* include other files */
#include "request.c" /* requesting html and validating urls */
#include "html.c" /* only max is able to understand this */
#include "manage_url.c" /* tools to work with current url */
#include "start_page.c" /* basic welcome page */
#include "prompt_mode.c" /* working with prompt */
int main(){
int ch;
initscr();
noecho(); /* turning off echoing input */
curs_set(0); /* turning off cursor */
refresh(); /* refresh to see changes */
open_start(); /* opening welcome page */
do{ /* processing input */
ch = getch();
switch(ch){
case ':': /* enter prompt mode*/
open_prompt();
break;
case 'y': /* yank current url */
clear();
printw("Your clipboard is %s", get_url());
refresh();
break;
case 'i': /* enter insert mode */
open_insert();
break;
case 't': /* delete? */
open_tabs();
break;
case 9: /* tab key */ /* scroll tabs */
scroll_tabs();
open_tabs();
wcenter(stdscr, 5, websites[cur]);
break;
default:
break;
}
upd_tab_bar(); /* after each action update/add tabs */
}while(true);
endwin(); /* nothing will go here */
return 0;
}
void open_insert(){
ins = newwin(0, 0, 0, 0);
if (check_win(ins)) return;
curs_set(1); /* turning cursor on while in insert mode */
wrefresh(ins);
for (;;) {
int ch = getch();
waddch(ins, ch);
switch(ch){
case 27:/* escape button */
clear(); refresh();
curs_set(0);return; /* exit insert mode */
default:
break;
}
wrefresh(ins);
}
clear();
refresh();
curs_set(0);
}
/* some web stuff, added by max */
void tag_callback(char *tagname){
printf("%s", tagname);
}
void render_website(char *url){
wprintw(web_wins[cur], "rendering %s", url);
char *html = request(url);
parse(html, *tag_callback);
}
void open_website(char *site){
web_wins[cur] = newwin(0, 0, 1, 0); /* everything but first line of screen */
if (check_win(web_wins[cur])) return;
int validity = validate_url(site);
if (validity){
wprintw(web_wins[cur], "Opening %s...", site);
wrefresh(web_wins[cur]);
render_website(site);
set_url(site);
} else {
wclear(web_wins[cur]);
wprintw(web_wins[cur], "Error: Url %s is not valid!", site);
wrefresh(web_wins[cur]);
}
}
/* end of web stuff */
void open_tabs(){
t_bar = newwin(1, 0, 0, 0); /* first line of screen */
if (check_win(t_bar)) return;
wclear(t_bar);
int i = -1;
for (i = 0; i <= wmax; i++){ /* for every website url */
wprintw(t_bar, "| ");
if (i == cur) {
wattron(t_bar, A_STANDOUT); /* highlight current website */
wprintw(t_bar, "%s", websites[i]); /* print its name */
wattroff(t_bar, A_STANDOUT);
waddch(t_bar, ' ');
} else {
wprintw(t_bar, "%s", websites[i]);
waddch(t_bar, ' ');
}
}
i == 0 ? : waddstr(t_bar, "|");
/* if no websites opened, do not print closing '|' */
wrefresh(t_bar);
}
void wcenter(WINDOW *win, int row, char *title){
int len, indent, y, width;
getmaxyx(win, y, width); /* get coords of window */
len = strlen(title); /* getting len of the given string */
indent = width - len; /* counting how much space */
indent /= 2; /* will we need to retreat */
mvwaddstr(win, row, indent, title); /* finally print message */
wrefresh(win); /* and refresh given window */
}
char *get_thing_after_space(char *text) {
char* space_ptr = strstr(text, " "); /* set pointer for first space */
if(space_ptr == NULL) return ""; /* if nothing after space, exit */
space_ptr++; /* do not include space */
return space_ptr; /* finally return */
}
bool check_win(WINDOW *win){
if(win == NULL){
addstr("Error while allocating memory for window");
endwin();
return true;
} else {
return false;
}
}
void upd_tab_bar(){
open_tabs();
touchwin(t_bar);
wrefresh(t_bar);
}
void scroll_tabs(void){
/*
* simple loop:
* if it hasn't reached it limits, add 1
* else go to the beginning
*/
if(cur < wmax){
cur++;
} else {
cur = 0;
}
}