-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathmain.c
179 lines (152 loc) · 3.92 KB
/
main.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
178
179
#include <stdio.h>
#include <stdlib.h>
#include "editor.h"
//TODO handle resize add search option
void bomb(void);
void initialize_header_window(WINDOW *win);
void initialize_footer_window(WINDOW *win);
void help();
int main(int argc, char *argv[]) {
int i, ch, maxx, maxy;
data_structure *ds;
WINDOW *header_window, *footer_window, *editor_window;
char filename[128], temp[128];
filename[0] = '\0';
if(argc >= 2) {
if(!strcmp(argv[1], "-h") || !strcmp(argv[1], "--help")) {
help();
return 0;
}
strcpy(filename, argv[1]);
}
ds = ds_load_file(argv[1]);
initscr();
noecho();
raw();
getmaxyx(stdscr, maxy, maxx);
if( (header_window = newwin(2, maxx, 0, 0)) == NULL) bomb();
if( (editor_window = newwin(maxy - 4,maxx, 2, 0)) == NULL) bomb();
if( (footer_window = newwin(2, maxx, maxy - 2, 0)) == NULL) bomb();
wrefresh(footer_window);
wrefresh(header_window);
wrefresh(editor_window);
initialize_header_window(header_window);
initialize_footer_window(footer_window);
getmaxyx(header_window, maxy, maxx);
keypad(editor_window, TRUE);
scrollok(editor_window, TRUE);
add_datastructure_to_window(editor_window, ds);
wrefresh(editor_window);
while(1) {
ch = wgetch(editor_window);
switch (ch) {
case KEY_LEFT:
moveleft(editor_window, ds);
break;
case KEY_RIGHT:
moveright(editor_window, ds);
break;
case KEY_UP:
moveup(editor_window, ds);
break;
case KEY_DOWN:
movedown(editor_window, ds);
break;
case KEY_DL:
break;
case KEY_DC:
if(moveright(editor_window, ds))
editor_backspace_key(editor_window, ds);
break;
case KEY_SF:
break;
case KEY_BACKSPACE:
editor_backspace_key(editor_window, ds);
break;
/* TAB KEY */
case 9:
for(i = 0; i < TAB; i++)
editor_add_default_char(editor_window, ds, ' ');
break;
/* ENTER KEY */
case 10:
editor_enter_key(editor_window, ds);
break;
/* Ctrl + X */
case 24:
wmove(footer_window, 0, 0);
wprintw(footer_window, "File name to write : %s", filename);
echo();
wrefresh(footer_window);
wgetstr(footer_window, temp);
strcat(filename, temp);
ds_save_file(ds, filename);
noecho();
wrefresh(editor_window);
endwin();
return 0;
break;
/* Ctrl + C */
case 3:
endwin();
return 0;
case KEY_RESIZE:
getmaxyx(stdscr, maxy, maxx);
if(maxy < 6 || maxx < 3)
break;
delwin(header_window);
delwin(footer_window);
delwin(editor_window);
if( (header_window = newwin(2, maxx, 0, 0)) == NULL) bomb();
if( (editor_window = newwin(maxy - 4,maxx, 2, 0)) == NULL) bomb();
if( (footer_window = newwin(2, maxx, maxy - 2, 0)) == NULL) bomb();
keypad(editor_window, TRUE);
scrollok(editor_window, TRUE);
add_datastructure_to_window(editor_window, ds);
initialize_header_window(header_window);
initialize_footer_window(footer_window);
break;
default:
editor_add_default_char(editor_window, ds, ch);
break;
}
wrefresh(editor_window);
}
}
void bomb(void) {
addstr("Unable to allocate memory for new window.\n");
endwin();
}
void initialize_footer_window(WINDOW *win) {
wmove(win, 1, 0);
wprintw(win, " ");
wattron(win, A_REVERSE);
wprintw(win, "^X");
wattroff(win, A_REVERSE);
wprintw(win, " Save");
wprintw(win, " ");
wattron(win, A_REVERSE);
wprintw(win, "^C");
wattroff(win, A_REVERSE);
wprintw(win, " Exit");
wrefresh(win);
}
void initialize_header_window(WINDOW *win) {
int y, x, i;
char str[] = "Text Editor";
getmaxyx(win, y, x);
wattron(win, A_REVERSE);
for(i = 0; i < x; i++) {
waddch(win, ' ');
}
wmove(win, 0, (x - strlen(str))/2);
waddstr(win, str);
wattroff(win, A_REVERSE);
wrefresh(win);
}
void help() {
printf("Usage : ./project [filename]\n");
printf("^ means Ctrl\n");
printf("For exit without saving use ^C\n");
printf("For save use ^X\n");
}