save to file works - gramscii - A simple editor for ASCII box-and-arrow charts | |
Log | |
Files | |
Refs | |
Tags | |
README | |
LICENSE | |
--- | |
commit 52a48d7718c6467da380a51f49327323e15e00c7 | |
parent a2bf8545d73621de7cfa3a06663d1b94fdc668c4 | |
Author: KatolaZ <[email protected]> | |
Date: Fri, 19 Jul 2019 18:03:29 +0100 | |
save to file works | |
Diffstat: | |
M TODO | 16 +++++++++++----- | |
M gramscii.c | 110 +++++++++++++++++++++++++----… | |
2 files changed, 102 insertions(+), 24 deletions(-) | |
--- | |
diff --git a/TODO b/TODO | |
@@ -1,14 +1,20 @@ | |
+ optimize redraws (i.e., avoid to redraw if possible) | |
- (?) change cursor shape according to action | |
-- save to file | |
-- implement auto-arrow 'A' (automatic end-char) | |
-- implement delete 'x' or 'd' | |
+- auto-arrow 'A' (automatic end-char) | |
+- delete -- 'x' or 'd' | |
+- load from file | |
+- read file at point | |
+- visual selection | |
+ - crop | |
+ - fill | |
+ - delete | |
+ - yank/put | |
+- undo (by storing lines changed across insert/remove operations) | |
- manage special chars (DEL/CANC) during text insert | |
(also do not print unmanaged chars!) | |
-- load from file | |
-- insert file at position | |
- get screen geometry | |
- allow scrolling (both vertical and horizontal) | |
+* save to file | |
* implement arrow | |
* set different line styles (done for hl, vl, corner) | |
* add status bar | |
diff --git a/gramscii.c b/gramscii.c | |
@@ -41,6 +41,10 @@ | |
#define ARR_D 'v' | |
+#define HOME 0x01 | |
+#define END 0x02 | |
+#define MIDDLE 0x04 | |
+ | |
#define MIN(x,y) (x) < (y) ? (x) : (y) | |
#define MAX(x,y) (x) > (y) ? (x) : (y) | |
@@ -71,7 +75,7 @@ char line_v; | |
char mark_st; | |
char mark_end; | |
-struct termios t1, t2; | |
+struct termios t1, t2, t3; | |
void cleanup(int s){ | |
@@ -245,6 +249,13 @@ void draw_box(int x1, int y1, int fix){ | |
} | |
+void move_around(char c){ | |
+ | |
+ | |
+ | |
+} | |
+ | |
+ | |
void get_box(){ | |
char c; | |
int orig_x=x, orig_y=y; | |
@@ -285,28 +296,12 @@ void get_box(){ | |
int progr_x(int dir){ | |
- switch(dir){ | |
- case DIR_L: | |
- return -1; | |
- case DIR_R: | |
- return +1; | |
- default: | |
- return 0; | |
- } | |
- return 0; | |
+ return dir == DIR_L ? -1 : dir == DIR_R ? 1: 0; | |
} | |
int progr_y(int dir){ | |
- switch(dir){ | |
- case DIR_D: | |
- return +1; | |
- case DIR_U: | |
- return -1; | |
- default: | |
- return 0; | |
- } | |
- return 0; | |
+ return dir == DIR_U ? -1 : dir == DIR_D ? 1: 0; | |
} | |
@@ -412,9 +407,58 @@ void get_arrow(){ | |
state = MOVE; | |
} | |
+char get_key(char *s){ | |
+ | |
+ printf("\033[%d;1f\033[7m", HEIGHT+1); | |
+ printf("%100s", " "); | |
+ printf("\033[%d;1f\033[7m", HEIGHT+1); | |
+ printf("%s ", s); | |
+ printf("\033[0m"); | |
+ return getchar(); | |
+} | |
+ | |
+void get_string(char *s, int sz){ | |
+ | |
+ printf("\033[%d;1f\033[7m", HEIGHT+1); | |
+ printf("%100s", " "); | |
+ printf("\033[%d;1f\033[7m", HEIGHT+1); | |
+ /* We must activate echo now */ | |
+ t3 = t2; | |
+ t3.c_lflag |= (ECHO | ICANON); | |
+ tcsetattr(0, TCSANOW, &t3); | |
+ printf("Write to: "); | |
+ printf("\033[0m"); | |
+ fgets(s, sz, stdin); | |
+ s[strlen(s)-1] = '\0'; | |
+ tcsetattr(0, TCSANOW, &t2); | |
+} | |
+ | |
+int is_yes(char c){ | |
+ return c=='y' ? 1 : c == 'Y'? 1 : 0; | |
+} | |
+ | |
void write_file(){ | |
char fname[256]; | |
FILE *f; | |
+ int i; | |
+ | |
+ get_string(fname, 255); | |
+ if (f=fopen(fname, "r")){ | |
+ if (!is_yes(get_key("File exists. Overwrite [y/N]?")) ){ | |
+ fclose(f); | |
+ return; | |
+ } | |
+ fclose(f); | |
+ } | |
+ if((f=fopen(fname, "w"))==NULL){ | |
+ get_key("Error opening file."); | |
+ return; | |
+ } | |
+ for (i=0; i<HEIGHT; i++){ | |
+ fprintf(f, "%s\n", screen[i]); | |
+ } | |
+ fclose(f); | |
+ get_key("File saved."); | |
} | |
void toggle_hline(){ | |
@@ -450,6 +494,25 @@ void toggle_end_mark(){ | |
mark_end = end_marks[cur_end]; | |
} | |
+void go_to(int where){ | |
+ switch(where){ | |
+ case HOME: | |
+ x = y = 0; | |
+ break; | |
+ case END: | |
+ x = WIDTH-1; | |
+ y = HEIGHT-1; | |
+ break; | |
+ case MIDDLE: | |
+ x = WIDTH/2; | |
+ y = HEIGHT/2; | |
+ break; | |
+ } | |
+ check_bound(); | |
+ show_cursor(); | |
+} | |
+ | |
+ | |
void commands(){ | |
char c; | |
@@ -494,6 +557,15 @@ void commands(){ | |
case 'w': | |
write_file(); | |
break; | |
+ case 'g': | |
+ go_to(HOME); | |
+ break; | |
+ case 'G': | |
+ go_to(END); | |
+ break; | |
+ case 'm': | |
+ go_to(MIDDLE); | |
+ break; | |
case '-': | |
toggle_hline(); | |
break; |