-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy pathnvimfy.cpp
More file actions
262 lines (236 loc) · 4.79 KB
/
nvimfy.cpp
File metadata and controls
262 lines (236 loc) · 4.79 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
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
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
#include "nvimfy.hpp"
NVimfy::NVimfy(const std::string& file){
lines.push_back("");
x = y = 0;
mode = 'n';
status = "NORMAL";
section = {};
if(file.empty()){
filename = "untitled";
}else{
filename = file;
}
open();
initscr();
noecho();
cbreak();
keypad(stdscr, true);
use_default_colors();
}
NVimfy::~NVimfy(){
refresh();
endwin();
}
void NVimfy::run(){
while(mode != 'q'){
update();
statusline();
print();
int c = getch();
input(c);
}
}
void NVimfy::update(){
switch (mode){
case 'n':
status = "NORMAL";
break;
case 'i':
status = "INSERT";
break;
case 'q':
break;
}
section = " COLS: " + std::to_string(x) + " | ROWS: " + std::to_string(y) + " | FILE: " + filename;
status.insert(0, " ");
}
void NVimfy::statusline(){
start_color();
if(mode == 'n'){
init_pair(1, COLOR_MAGENTA, COLOR_BLACK);
}else{
init_pair(1, COLOR_GREEN, COLOR_BLACK);
}
attron(A_REVERSE);
attron(A_BOLD);
attron(COLOR_PAIR(1));
for(int i {}; i< COLS; ++i){
mvprintw(LINES - 1, i, " ");
}
mvprintw(LINES - 1, 0, status.c_str());
mvprintw(LINES -1, COLS - section.length(), §ion[0]);
attroff(COLOR_PAIR(1));
attroff(A_BOLD);
attroff(A_REVERSE);
}
void NVimfy::input(int c){
switch (c){
case KEY_UP:
up();
return;
case KEY_LEFT:
left();
return;
case KEY_RIGHT:
right();
return;
case KEY_DOWN:
down();
return;
}
switch (mode){
case 27:
case 'n':
switch (c){
case 'q':
mode = 'q';
break;
case 'i':
mode = 'i';
break;
case 'w':
mode = 'w';
save();
refresh();
endwin();
std::printf("Saved!\n");
exit(0);
// TODO save withot exit
break;
}
break;
case 'i':
switch(c){
case 27:
mode = 'n';
break;
case 127:
case KEY_BACKSPACE:
if( x == 0 && y > 0){
x = lines[y - 1].length();
lines[y - 1] += lines[y];
m_remove(y);
up();
}else if( x > 0 ){
lines[y].erase(--x, 1);
}
break;
case KEY_DC:
if( x == lines[y].length() && y != lines.size() - 1 ){
lines[y] += lines[y + 1];
} else {
lines[y].erase(x, 1);
}
break;
case KEY_ENTER:
case 10:
if(x < lines[y].length()){
m_insert(lines[y].substr(x, lines[y].length() - x), y + 1);
lines[y].erase(x, lines[y].length() - x);
} else {
m_insert("", y + 1);
}
x = 0;
down();
break;
case KEY_BTAB:
case KEY_CTAB:
case KEY_STAB:
case KEY_CATAB:
case 9:
lines[y].insert(x, 2, ' ');
x += 2;
break;
default:
lines[y].insert(x, 1, c);
++x;
}
}
}
void NVimfy::print(){
for (size_t i = 0; i < (size_t)LINES - 1; ++i){
if(i >= lines.size()){
move(i, 0);
clrtoeol();
}else{
mvprintw(i, 0, lines[i].c_str());
}
clrtoeol();
}
move(y, x);
}
void NVimfy::m_remove(int number){
lines.erase(lines.begin() + number);
}
std::string NVimfy::m_tabs(std::string& line){
std::size_t tab = line.find('\t');
return tab == line.npos ? line : m_tabs(line.replace(tab, 1, " "));
}
void NVimfy::m_insert(std::string line, int number){
line = m_tabs(line);
lines.insert(lines.begin() + number, line);
}
void NVimfy::m_append(std::string& line){
line = m_tabs(line);
lines.push_back(line);
}
void NVimfy::up(){
if(y > 0){
--y;
}
if(x >= lines[y].length()){
x = lines[y].length();
}
move(y, x);
}
void NVimfy::left(){
if(x > 0){
--x;
move(y, x);
}
}
void NVimfy::right(){
if((int)x <= COLS && x <= lines[y].length() - 1){
++x;
move(y, x);
}
}
void NVimfy::down(){
if((int)x < LINES && y < lines.size() - 1){
++y;
}
if(x >= lines[y].length()){
x = lines[y].length();
}
move(y, x);
}
void NVimfy::open(){
if(std::filesystem::exists(filename)){
std::ifstream ifile(filename);
if(ifile.is_open()){
while(!ifile.eof()){
std::string buffer;
std::getline(ifile, buffer);
m_append(buffer);
}
}else{
throw std::runtime_error("Cannot open file. Permission denied.");
}
}else{
std::string str {};
m_append(str);
}
}
void NVimfy::save(){
std::ofstream ofile(filename);
if(ofile.is_open()){
for(size_t i {}; i < lines.size(); i++){
ofile << lines[i] << '\n';
}
ofile.close();
}else{
refresh();
endwin();
throw std::runtime_error("Cannot save file. Permission denied.");
}
}