107 lines
3.1 KiB
C
107 lines
3.1 KiB
C
#include <ncurses.h>
|
|
#include <stdio.h>
|
|
#include <stdlib.h>
|
|
#include <string.h>
|
|
#include "editor.h"
|
|
|
|
void help() {
|
|
printf("Usage:\n");
|
|
printf(" cmd open <path/to/file> // opens the specified file\n");
|
|
printf(" cmd rm <path/to/file> // deletes the specified file\n");
|
|
printf(" cmd new <path/to/file> // creates a new empty file at the specified path\n");
|
|
printf(" cmd mv <path/to/file> <new/path> // moves the specified file to the new path\n");
|
|
printf(" cmd cp <path/to/file> <new/path> // copies the specified file to the new path\n");
|
|
printf(" cmd len <path/to/file> // returns the length of the specified file\n");
|
|
printf(" cmd log // prints a list of all changes made to the file\n");
|
|
}
|
|
|
|
int open_editor(char* filename) {
|
|
initscr();
|
|
raw();
|
|
noecho();
|
|
keypad(stdscr, true);
|
|
int max_y, max_x;
|
|
getmaxyx(stdscr, max_y, max_x);
|
|
|
|
start_color();
|
|
use_default_colors();
|
|
|
|
init_pair(1, COLOR_CYAN, -1);
|
|
init_pair(2, COLOR_MAGENTA, -1);
|
|
|
|
move(0, 5);
|
|
|
|
Editor editor;
|
|
if (strcmp(filename, "") == 0) {
|
|
editor = new_editor();
|
|
} else {
|
|
editor = editor_from(str_from_file(fopen(filename, "r")));
|
|
}
|
|
while (true) {
|
|
refresh();
|
|
int c = getch();
|
|
if (editor.editmode == true) {
|
|
switch (c) {
|
|
case 27:
|
|
switch_mode(&editor);
|
|
break;
|
|
case KEY_BACKSPACE:
|
|
move_cursor(&editor, -1, 0);
|
|
delchar(&editor);
|
|
break;
|
|
case KEY_DC:
|
|
delchar(&editor);
|
|
break;
|
|
case '\n':
|
|
pressed_enter(&editor);
|
|
break;
|
|
case KEY_UP:
|
|
move_cursor(&editor, 0, -1);
|
|
break;
|
|
case KEY_DOWN:
|
|
move_cursor(&editor, 0, 1);
|
|
break;
|
|
case KEY_LEFT:
|
|
move_cursor(&editor, -1, 0);
|
|
break;
|
|
case KEY_RIGHT:
|
|
move_cursor(&editor, 1, 0);
|
|
break;
|
|
default:
|
|
fprintf(stderr, "%d\n", c);
|
|
addchar(&editor, c);
|
|
break;
|
|
}
|
|
} else {
|
|
switch (c) {
|
|
case 'q':
|
|
endwin();
|
|
return 0;
|
|
case 'i':
|
|
switch_mode(&editor);
|
|
break;
|
|
case 'w':
|
|
editor.unsaved_changes = false;
|
|
// TODO: write function to save the data to a file
|
|
break;
|
|
default:
|
|
break;
|
|
}
|
|
}
|
|
}
|
|
}
|
|
|
|
int main(int argc, char* argv[]) {
|
|
if (argc == 1) {
|
|
Editor e = new_editor();
|
|
open_editor("");
|
|
} else if (argc == 3) {
|
|
if (strcmp(argv[1], "open") == 0) {
|
|
fprintf(stderr, "starting editor openiing file %s", argv[2]);
|
|
open_editor(argv[2]);
|
|
}
|
|
}
|
|
return 0;
|
|
}
|
|
|