-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathbuffer.cpp
736 lines (660 loc) · 15.8 KB
/
buffer.cpp
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
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
/*
This program should define a buffer object.
TODO: describe buffer object
TODO: add tests
*/
#include "buffer.h"
void buffer::init()
{
getmaxyx(stdscr,ymax,xmax);
startLine = 0;
currentLine = 0;
finishLine = ymax;
CurrentFileName = string(" ");
editorIsRunning = true;
point.x = 0;
point.y = 0;
bufSubWin = subwin(stdscr,ymax-1,xmax,1,0); // subwin in stdscr
wmove(bufSubWin,point.y,point.x);
//name = "";
}
// get current screen dimensions,
// and current cursor coordinates
void buffer::update()
{
getmaxyx(bufSubWin,ymax,xmax);
getyx(bufSubWin,point.y,point.x);
lineNumber = currentLine + 1;
getStartFinish();
displayLines();
refresh();
stats();
//touchwin(stdscr);
wrefresh(bufSubWin);
}
void buffer::setName(const string& thisName)
{
name = thisName;
}
string buffer::getName()
{
return name;
}
// display relevent vars
/*
Tue Dec 02, 2014 01:38:39
Took me a long time to figure out why this single line
kept following the cursor around.
The problem is that getmaxyx() returns
the max number of lines + 1.
NOT THE MAX NUMBER OF LINES!
This of course changes everything.
*/
void buffer::stats()
{
wattron(bufSubWin,A_REVERSE);
int ty,tx;
getyx(bufSubWin,ty,tx); // store point
getmaxyx(bufSubWin,ymax,xmax);
wmove(bufSubWin,ymax-1,0);
//wmove(stdscr,finishLine,0);
wclrtoeol(bufSubWin); // clear line
wmove(bufSubWin,ymax-1,0);
wattron(bufSubWin,A_BOLD);
wprintw(bufSubWin,"%s: ",name.c_str());
wattroff(bufSubWin,A_BOLD);
wprintw(bufSubWin,"cx:%d,cy:%d,sl:%d,cl:%d,fl:%d,xmax:%d,ymax:%d,ln:%d,lines:%d",
point.x,point.y,startLine,currentLine,finishLine,xmax,ymax,lineNumber,lines.size());
wrefresh(bufSubWin);
wmove(bufSubWin,ty,tx); // restore point
wattroff(bufSubWin,A_REVERSE);
}
/*
Mon Dec 01, 2014 23:10:15
Can you use point.y and currentLine to find startLine and finishLine?
I think so. But how?
*/
void buffer::getStartFinish()
{
int minBoundary = currentLine - point.y; // obviously this is wrong
// from test cases
int maxBoundary = currentLine + ymax - point.y;
// startLine must be >= 0
startLine = (minBoundary >= 0) ? minBoundary : 0;
// finishLine must be <= lines.size()
finishLine = (maxBoundary < lines.size()) ? maxBoundary : lines.size();
}
// this should renumber the lines appropriately
void buffer::updateLineNumbers() {
for (int i = 0; i < lines.size(); i++)
lines[i].number = i+1;
}
// this should just push text into a vector
void buffer::print2vec(const char cstring[]) {
line thisLine;
thisLine.text = string(cstring);
lines.push_back(thisLine);
updateLineNumbers();
}
/*
displayLines() should render ymax lines on the screen.
Where does it start from though?
*/
void buffer::displayLines()
{
int ty,tx;
getyx(bufSubWin,ty,tx);
getmaxyx(bufSubWin,ymax,xmax);
for (int i = startLine; i < finishLine; i++)
{
wmove(bufSubWin,(startLine-i), 0); // reposition cursor
for (int j = 0; j < min(xmax,(int)lines[i].text.length()); j++)
{
if (lines[i].text[j] != '\t')
wprintw(bufSubWin,"%c", lines[i].text[j] );
else{
lines[i].text[j] = ' '; // delete tab. Ouch.
//wprintw(bufSubWin,"%s", " "); // 5 spaces.
for (int k = 0; k < 4; k++)
lineGap(' ');
}
}
}
wmove(bufSubWin,ty,tx);
}
void buffer::getCwd()
{
pathName = fs::current_path().string();
}
// void buffer::getNumberOfBuffers()
// {
// }
//============== KEY COMMANDS ========================//
// callback for up arrow keypresses
void buffer::key_up()
{
update();
if (point.y-1 >= 0)
{
int prevLineLength = lines[currentLine-1].text.length()-1;
currentLine--;
if (point.x <= prevLineLength)
wmove(bufSubWin,point.y-1,point.x);
else
wmove(bufSubWin,point.y-1,prevLineLength);
update();
}
else
{
if (currentLine - 1 >= 0)
{
startLine--;
currentLine--;
finishLine--;
displayLines();
update();
}
}
} // end key_up
void buffer::key_down()
{
update();
if (point.y+1 < ymax-1) {
if (point.y+1 < lines.size()) {
int nextLineLength = lines[currentLine+1].text.length()-1;
if (point.x <= nextLineLength){
wmove(bufSubWin,point.y+1,point.x);
}
else
wmove(bufSubWin,point.y+1,nextLineLength);
getyx(bufSubWin,point.y,point.x);
currentLine++;
update();
}
}
else // scroll window by one line
{
if (currentLine + 1 < lines.size())
{
startLine++;
currentLine++;
finishLine++;
displayLines();
update();
}
}
} // end key_down
void buffer::key_right()
{
// getyx(stdscr,point.y,point.x);
update();
if (point.x+1 < lines[currentLine].text.length())
wmove(bufSubWin,point.y,point.x+1);
else
{
wmove(bufSubWin,point.y+1,0);
currentLine++;
}
update();
}
void buffer::key_left()
{
//getyx(stdscr,point.y,point.x);
update();
if (point.x-1 >= 0)
wmove(bufSubWin,point.y,point.x-1);
else
{
if (currentLine-1 >= 0)
{
wmove(bufSubWin,point.y-1,
lines[currentLine-1].text.length()-1
);
currentLine--;
}
}
update();
}
void buffer::key_home()
{
//getyx(stdscr,point.y,point.x);
update();
wmove(bufSubWin,point.y,0);
update();
}
void buffer::key_end()
{
//getyx(stdscr,point.y,point.x);
update();
wmove(bufSubWin,point.y,
lines[currentLine].text.length()-1);
update();
}
void buffer::key_pgUp()
{
//getyx(stdscr,point.y,point.x);
update();
if (startLine - ymax - 1 <= 0){
startLine = 0;
currentLine = startLine;
wmove(bufSubWin,0,0);
update();
}
else{
startLine = startLine - ymax - 1;
currentLine = startLine;
update();
wmove(bufSubWin,0,0);
update();
}
}
void buffer::key_pgDown()
{
//getyx(stdscr,point.y,point.x);
update();
if (finishLine + ymax - 1 <= lines.size()){
startLine = finishLine;
currentLine = startLine;
update();
wmove(bufSubWin,0,0);
update();
}
else{
finishLine = lines.size();
startLine = finishLine - ymax;
currentLine = startLine;
update();
wmove(bufSubWin,0,0);
update();
}
}
void buffer::key_backspace()
{
getyx(bufSubWin,point.y,point.x);
string before,after,current;
current = lines[currentLine].text;
// split current line text into two strings
// 1 before cursor, 1 after cursor
before = current.substr(0,point.x); // includes current cursor
after = current.substr(point.x,current.length());
if (point.x-1 >= 0)
{
// remove last char before point.
// can't use string.pop_back()
// because it's only available in c++11
// before.pop_back();
before = before.substr(0,before.length()-1);
lines[currentLine].text = before + after;
wmove(bufSubWin,point.y,point.x-1);
}
else // need to join the front of this line with the tail of the
// previous line
{
if (currentLine > 0)
{
//before = current = "";
before = lines[currentLine-1].text;
before = before.substr(0,before.length()-1);
after = lines[currentLine].text;
lines[currentLine-1].text = before + after;
wmove(bufSubWin,point.y-1,before.length());
lines.erase(lines.begin()+currentLine);
currentLine--;
update();
}
}
refresh();
update();
}
void buffer::key_enter_fileMode()
{
string fn = lines[currentLine].text;
// need to remove '\n' newline chars from fn.
fn.erase(std::remove(fn.begin(),fn.end(),'\n'),fn.end());
// clear();
// printw("keyed to open: %s",fn.c_str());
// refresh();
// usleep(10000);
openFile(fn);
}
/*
add a new line.
This is trickier than it sounds.
case0: there is no text before point
*/
void buffer::key_enter_textMode()
{
getyx(bufSubWin,point.y,point.x);
if (point.x == 0)
{
line lineToAdd; // make an empty string for this
lineToAdd.text = "\n";
lines.insert(lines.begin()+currentLine, lineToAdd);
if (currentLine + 1 == finishLine)
{
startLine++;
currentLine++;
finishLine++;
key_down();
key_up();
}
}
else
{
// split the current line.
line thisLine = lines[currentLine];
line lineToAdd;
string before,after;
// get currentLine.text before point
before = thisLine.text.substr(0,point.x) + "\n";
// get currentLine.text after point
after = thisLine.text.substr(point.x,thisLine.text.length());
// put text after point on the new line.
thisLine.text = before;
lineToAdd.text = after;
// restore modified currentLine text
lines[currentLine] = thisLine;
// insert the new line into the vector of lines
lines.insert(lines.begin()+currentLine+1, lineToAdd);
//update();
}
refresh();
update();
currentLine++; // update currentLine
wmove(bufSubWin,point.y+1,0); // update cursor position
refresh();
update();
} // end key_enter
void buffer::lineGap(int character)
{
char thisChar = (char)character;
// if (thisChar == '\n')
// if (mode == "text")
// key_enter_textMode();
// else if (mode == "file")
// key_enter_fileMode();
// else
if (character == TAB)
{
// insert 5 spaces.
// call lineGap 5 times?
static int n = 5;
while (n !=0){
lineGap(' ');
n--;
}
// wmove(bufSubWin,point.y,point.x+5);
}
else
{
getyx(bufSubWin,point.y,point.x);
string before,after,current;
current = lines[currentLine].text;
// split current line text into two strings
// 1 before cursor, 1 after cursor
before = current.substr(0,point.x); // includes current cursor
after = current.substr(point.x,current.length());
// push character into string "before" point
before.push_back(thisChar);
// concat "before" and "after" strings
current = before + after;
// update cursor
wmove(bufSubWin,point.y,point.x+1);
// restore line in structure
lines[currentLine].text = current;
refresh();
update();
}
} // end lineGap function
void buffer::getUserInput()
{
while (editorIsRunning)
{
keyPress = getch();
getUserInput(keyPress);
} // end while
} // end getUserInput()
// TODO: make this a factory, get rid of switch
void buffer::getUserInput(int keyPress)
{
switch (keyPress)
{
case KEY_UP:
{
key_up();
break;
}
case KEY_DOWN:
{
key_down();
break;
}
case KEY_LEFT:
{
key_left();
break;
}
case KEY_RIGHT:
{
key_right();
break;
}
case KEY_HOME:
{
key_home();
break;
}
case KEY_NPAGE:
{
key_pgDown();
break;
}
case KEY_PPAGE:
{
key_pgUp();
break;
}
case KEY_END:
{
key_end();
break;
}
// case TAB:
// {
// break;
// }
case ENTER: // doesnt' seem to work
{
if (mode == "text")
key_enter_textMode();
else if (mode == "file")
key_enter_fileMode();
break;
}
case KEY_BACKSPACE: // needs to update string in lines
{
update();
key_backspace();
break;
}
// http://stackoverflow.com/questions/406933/how-to-intercept-special-alt-ctrl-key-pressed-in-python-curses
/*
CTRL-A: getch() returns 1
CTRL-B: getch() returns 2
...
CTRL-Z: getch() returns 26
*/
case 15:// ctrl+o: create a new fileEditor buffer
{
//fileEditor fe;
//printw("Ctrl-o pressed");
string thisArg = string(".");
openFile(thisArg);
break;
}
default:
{
lineGap(keyPress);
break;
}
} // end switch
} // end getUserInput()
/* -------------------- listFiles(string thisPath)
overview:
input:
output:
---------------------------------------------------- */
void buffer::listFiles(string& thisPath)
{
clear();
lines.clear();
init();
update();
stringstream textStream;
if (fs::exists(thisPath.c_str()))
{
fs::path p (thisPath.c_str());
fs::path absPath = p.string();
fs::directory_iterator end_itr;
print2vec(".\n");
print2vec("..\n");
// cycle through the directory
for (fs::directory_iterator itr(p); itr != end_itr; ++itr)
{
if (is_regular_file(itr->path())) {
// assign current file name to current_file and echo it out to the console.
//string current_file = itr->path().string();
string current_file = itr->path().filename().c_str();
textStream << current_file << endl;
print2vec(textStream.str().c_str());
textStream.str(std::string()); // clear textStream for future use
}
else if (is_directory(itr->path())) { // file is a directory
string current_file = itr->path().string();
attron(A_REVERSE);
textStream << current_file << endl;
print2vec(textStream.str().c_str());
textStream.str(std::string()); // clear textStream for future use
attroff(A_REVERSE);
}
} // end for loop
} // end if file exists
else
{
textStream << "That path does not exist." << endl;
print2vec(textStream.str().c_str());
textStream.str(std::string()); // clear textStream for future use
}
update();
refresh();
}
/* -------------------- openFile(string fileName)
overview:
input:
output:
---------------------------------------------------- */
void buffer::openFile(string fileName)
{
//fileName = fileToRead;
//printw("%s","Opening file");
string tmp;
std::fstream fin;
wmove(bufSubWin,0,0);
getyx(bufSubWin,point.y,point.x);
init();
startLine=0;
currentLine=0;
finishLine=1;
clear();
lines.clear();
update();
if (fs::exists(fileName.c_str())){
fs::path p(fileName.c_str());
name = p.string();
CurrentFileName = name;
if (is_directory(p))
{
mode = "file";
name = fs::absolute(p.string()).string();
//chdir(p.string().c_str());
chdir(name.c_str());
listFiles(fileName);
//wmove(bufSubWin,0,0);
key_down();
wmove(bufSubWin,0,0);
currentLine = 0;
refresh();
}
else if (is_regular_file(p))
{
mode = "text";
// need to convert fileName string to something more useful
// already converted it to a path.
//fin.open(fileName.c_str(),std::ios::in);
wprintw(bufSubWin,"Trying to open: %s okay?",p.string().c_str());
wrefresh(bufSubWin);
usleep(100000);
fin.open(p.string().c_str(),std::ios::in);
if (fin.fail())
{
wmove(bufSubWin,ymax,0);
wprintw(bufSubWin,"failed to open file");
refresh();
//exit (1);
}
while (getline(fin,tmp)) // read file into lines vector
print2vec((tmp+"\n").c_str());
//wmove(bufSubWin,1,0);
key_down();
wmove(bufSubWin,0,0);
currentLine = 0;
refresh();
} // end regular file case
//wmove(bufSubWin,1,0);
// wmove(bufSubWin,0,0);
fin.close();
update();
refresh();
} // end file exists check
else {
wprintw(bufSubWin,"file %s does not exist.",fileName.c_str());
refresh();
usleep(5000000);
tmp = "./";
listFiles(tmp);
}
} // end openFile function
/* -------------------- openFile(string fileName)
overview:
input:
output:
---------------------------------------------------- */
void buffer::writeFile(string fileName)
{
int ty,tx;
getyx(bufSubWin,ty,tx); // store point
std::fstream fOut;
fOut.open(fileName.c_str(),std::ios::out);
if (fOut.fail())
{
wmove(bufSubWin,ymax,0);
wprintw(bufSubWin,"failed to open file for writing");
refresh();
//exit (1);
}
for (int i = 0;
i < lines.size();
++i)
//fOut << lines[i].text << '\n';
fOut << lines[i].text ;
fOut.close();
wattron(bufSubWin,A_REVERSE);
wattron(bufSubWin,A_BOLD);
mvwprintw(bufSubWin,ymax-1,0,"Wrote %s.",fileName.c_str());
wattroff(bufSubWin,A_REVERSE);
wattroff(bufSubWin,A_BOLD);
wmove(bufSubWin,ty,tx); // restore point
wrefresh(bufSubWin);
refresh();
usleep(100000);
}