first steps towards script-mode - gramscii - A simple editor for ASCII box-and-… | |
Log | |
Files | |
Refs | |
Tags | |
README | |
LICENSE | |
--- | |
commit a74b46c7a4d1ccad6934aeb00915a2fd383f072c | |
parent 1aa7efdd7a8dcfd1a55f30c2754d1e473f0bb59b | |
Author: KatolaZ <[email protected]> | |
Date: Wed, 24 Jul 2019 17:00:05 +0100 | |
first steps towards script-mode | |
Diffstat: | |
M gramscii.c | 163 +++++++++++++++++++----------… | |
1 file changed, 101 insertions(+), 62 deletions(-) | |
--- | |
diff --git a/gramscii.c b/gramscii.c | |
@@ -30,6 +30,8 @@ | |
#include "config.h" | |
+#include "arg.h" | |
+ | |
#define MOVE 0x00 | |
#define BOX 0x01 | |
#define ARROW 0x02 | |
@@ -98,13 +100,26 @@ char modified; | |
char fname[256]; | |
char visual; | |
+char silent; | |
+ | |
+char *argv0; | |
struct termios t1, t2, t3; | |
-void cleanup(int s){ | |
+void dump_lines(){ | |
+ int i; | |
+ for (i=0; i<HEIGHT; i++){ | |
+ printf("%s\n", screen[i]); | |
+ } | |
+} | |
- printf("\033[;H\033[2J"); | |
+void cleanup(int s){ | |
+ | |
+ if (!silent) | |
+ printf("\033[;H\033[2J"); | |
+ if (silent) | |
+ dump_lines(); | |
tcsetattr(0, TCSANOW, &t1); | |
fflush(stdout); | |
exit(0); | |
@@ -135,6 +150,8 @@ char* state_str(){ | |
void status_bar(){ | |
+ if (silent) | |
+ return; | |
printf("\033[%d;1f\033[7m", HEIGHT+1); | |
printf("%*s", WIDTH-1, ""); | |
printf("\033[%d;1f\033[7m", HEIGHT+1); | |
@@ -151,32 +168,38 @@ void status_bar(){ | |
fflush(stdout); | |
} | |
-char get_key(char *msg){ | |
+char get_key(FILE *fc, char *msg){ | |
+ if (silent) | |
+ return 0; | |
printf("\033[%d;1f\033[7m", HEIGHT+1); | |
printf("%*s", WIDTH, ""); | |
printf("\033[%d;1f\033[7m", HEIGHT+1); | |
printf("%s", msg); | |
printf("\033[0m"); | |
fflush(stdout); | |
- return getchar(); | |
+ return fgetc(fc); | |
} | |
-void get_string(char *msg, char *s, int sz){ | |
+void get_string(FILE *fc, char *msg, char *s, int sz){ | |
- printf("\033[%d;1f\033[7m", HEIGHT+1); | |
- printf("%*s", WIDTH, ""); | |
- 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("%s", msg); | |
- printf("\033[0m"); | |
- fgets(s, sz, stdin); | |
+ if (!silent){ | |
+ printf("\033[%d;1f\033[7m", HEIGHT+1); | |
+ printf("%*s", WIDTH, ""); | |
+ 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("%s", msg); | |
+ printf("\033[0m"); | |
+ } | |
+ fgets(s, sz, fc); | |
s[strlen(s)-1] = '\0'; | |
tcsetattr(0, TCSANOW, &t2); | |
- fflush(stdout); | |
+ if (!silent) | |
+ fflush(stdout); | |
} | |
int is_yes(char c){ | |
@@ -186,6 +209,8 @@ int is_yes(char c){ | |
/*** Screen management ***/ | |
void show_cursor(){ | |
+ if (silent) | |
+ return; | |
printf("\033[%d;%df", y+1, x+1); | |
fflush(stdout); | |
} | |
@@ -201,12 +226,16 @@ void set_xy(int x, int y, char c){ | |
void draw_xy(int x, int y, char c){ | |
/* FIXME: check if x and y are valid!!!! */ | |
+ if (silent) | |
+ return; | |
printf("\033[%d;%df",y+1,x+1); | |
putchar(c); | |
fflush(stdout); | |
} | |
void update_current(){ | |
+ if (silent) | |
+ return; | |
printf("\033[%d'%df",y+1,x+1); | |
putchar(screen[y][x]); | |
fflush(stdout); | |
@@ -261,6 +290,8 @@ void reset_styles(){ | |
void redraw(){ | |
int i; | |
+ if (silent) | |
+ return; | |
printf("\033[2J\033[1;1H"); | |
for (i=0;i<HEIGHT;i++){ | |
fprintf(stdout,"%s\n",screen[i]); | |
@@ -366,6 +397,8 @@ int progr_y(int dir){ | |
} | |
void set_video(int v){ | |
+ if (silent) | |
+ return; | |
printf("\033[%dm", v); | |
fflush(stdout); | |
} | |
@@ -436,12 +469,12 @@ int change_style(char c){ | |
/***** text, box, arrows *****/ | |
-void get_text(){ | |
+void get_text(FILE *fc){ | |
char c; | |
int orig_x = x; | |
redraw(); | |
- while((c=getchar())!=EOF && c != 27){ | |
+ while((c=fgetc(fc))!=EOF && c != 27){ | |
if(c=='\n'){ | |
set_cur(BG); | |
y += 1; | |
@@ -493,13 +526,13 @@ void draw_box(int x1, int y1, int fix){ | |
show_cursor(); | |
} | |
-void get_box(){ | |
+void get_box(FILE *fc){ | |
char c; | |
int orig_x=x, orig_y=y; | |
redraw(); | |
step = 1; | |
draw_box(x,y,NOFIX); | |
- while((c=getchar())!=EOF && c != 27 && c!= 'b' && c != '\n'){ | |
+ while((c=fgetc(fc))!=EOF && c != 27 && c!= 'b' && c != '\n'){ | |
if (change_style(c)) | |
goto update_box; | |
if (!move_around(c)) | |
@@ -560,7 +593,7 @@ void draw_arrow(int x, int y, char *a, int a_len, int fix){ | |
show_cursor(); | |
} | |
-void get_arrow(){ | |
+void get_arrow(FILE *fc){ | |
char c; | |
int orig_x=x, orig_y=y, arrow_len; | |
@@ -577,7 +610,7 @@ void get_arrow(){ | |
redraw(); | |
step = 1; | |
draw_arrow(x,y, arrow, 0, NOFIX); | |
- while((c=getchar())!=EOF && c != 27 && c!= 'a' && c != '\n'){ | |
+ while((c=fgetc(fc))!=EOF && c != 27 && c!= 'a' && c != '\n'){ | |
if (change_style(c)) | |
goto update_arrow; | |
if (!move_around(c)) | |
@@ -625,12 +658,12 @@ void do_delete(int x1, int y1){ | |
} | |
-void delete(){ | |
+void delete(FILE *fc){ | |
char c; | |
int orig_x = x, orig_y = y; | |
status_bar(); | |
show_cursor(); | |
- while((c=getchar())!=EOF && c!=27 && c!= 'x' && c != '\n'){ | |
+ while((c=fgetc(fc))!=EOF && c!=27 && c!= 'x' && c != '\n'){ | |
if (!move_around(c)) continue; | |
check_bound(); | |
do_delete(orig_x, orig_y); | |
@@ -647,65 +680,65 @@ void delete(){ | |
/*** File management ***/ | |
-void write_file(){ | |
- FILE *f; | |
+void write_file(FILE *fc){ | |
+ FILE *fout; | |
int i; | |
if (!fname[0] || force_new){ | |
- get_string("Write to: ", fname, 255); | |
- if ((f=fopen(fname, "r"))!=NULL){ | |
- if (!is_yes(get_key("File exists. Overwrite [y/n]?")) … | |
- fclose(f); | |
+ get_string(fc, "Write to: ", fname, 255); | |
+ if ((fout=fopen(fname, "r"))!=NULL){ | |
+ if (!is_yes(get_key(fc,"File exists. Overwrite [y/n]?"… | |
+ fclose(fout); | |
return; | |
} | |
- fclose(f); | |
+ fclose(fout); | |
} | |
} | |
- if((f=fopen(fname, "w"))==NULL){ | |
- get_key("Error opening file."); | |
+ if((fout=fopen(fname, "w"))==NULL){ | |
+ get_key(fc, "Error opening file."); | |
return; | |
} | |
for (i=0; i<HEIGHT; i++){ | |
- fprintf(f, "%s\n", screen[i]); | |
+ fprintf(fout, "%s\n", screen[i]); | |
} | |
- fclose(f); | |
+ fclose(fout); | |
modified = 0; | |
- get_key("File saved."); | |
+ get_key(fc, "File saved."); | |
} | |
-void check_modified(){ | |
+void check_modified(FILE *fc){ | |
if (modified){ | |
- if (!is_yes(get_key("Unsaved changes. Write to file [y/n]?")) … | |
+ if (!is_yes(get_key(fc, "Unsaved changes. Write to file [y/n]?… | |
return; | |
} | |
write_file(0); | |
} | |
} | |
-void load_file(){ | |
+void load_file(FILE *fc){ | |
char newfname[256]; | |
- FILE *f; | |
+ FILE *fin; | |
int i; | |
- get_string("Load file: ", newfname, 255); | |
- if ((f=fopen(newfname, "r")) != NULL){ | |
+ get_string(fc, "Load file: ", newfname, 255); | |
+ if ((fin=fopen(newfname, "r")) != NULL){ | |
i = 0; | |
- while((fgets(screen[i], WIDTH+2, f)) != NULL && i<HEIGHT) | |
+ while((fgets(screen[i], WIDTH+2, fin)) != NULL && i<HEIGHT) | |
screen[i++][WIDTH-1]='\0'; | |
for(;i<HEIGHT; i++){ | |
erase_line(screen[i]); | |
} | |
- fclose(f); | |
+ fclose(fin); | |
} | |
strcpy(fname, newfname); | |
modified=0; | |
redraw(); | |
} | |
-void new_file(){ | |
- check_modified(); | |
+void new_file(FILE *fc){ | |
+ check_modified(fc); | |
erase_screen(); | |
go_to(HOME); | |
redraw(); | |
@@ -716,7 +749,7 @@ void new_file(){ | |
/*** Visual ***/ | |
-void visual_box(){ | |
+void visual_box(FILE *fc){ | |
int orig_x =x, orig_y = y; | |
char c, f = BG; | |
@@ -724,10 +757,10 @@ void visual_box(){ | |
step = 1; | |
set_video(VIDEO_REV); | |
draw_box(x,y,NOFIX); | |
- while((c=getchar())!=EOF && c != 27 && c!= 'v' && c != '\n'){ | |
+ while((c=fgetc(fc))!=EOF && c != 27 && c!= 'v' && c != '\n'){ | |
if (!move_around(c)) switch(c){ | |
case 'f':/* fill */ | |
- f = get_key("fill char: "); /** FALLTHROUGH **/ | |
+ f = get_key(fc, "fill char: "); /** FALLTHROUG… | |
case 'x':/* erase */ | |
erase_box(orig_x, orig_y, f); | |
modified = 1; | |
@@ -805,50 +838,50 @@ void init(){ | |
/*** Commands ***/ | |
-void commands(){ | |
+void commands(FILE *fc){ | |
char c; | |
- while((c=getchar())!=EOF){ | |
+ while((c=fgetc(fc))!=EOF){ | |
if (!change_style(c) && !move_around(c)){ | |
switch(c){ | |
case 'i': | |
state = TEXT; | |
- get_text(); | |
+ get_text(fc); | |
break; | |
case 'R': | |
redraw(); | |
break; | |
case 'b': | |
state = BOX; | |
- get_box(); | |
+ get_box(fc); | |
break; | |
case 'a': | |
state = ARROW; | |
- get_arrow(); | |
+ get_arrow(fc); | |
break; | |
case 'W': | |
force_new = 1;/** FALLTHROUGH **/ | |
case 'w': | |
- write_file(); | |
+ write_file(fc); | |
break; | |
case 'e': | |
- check_modified();/** FALLTHROUGH **/ | |
+ check_modified(fc);/** FALLTHROUGH **/ | |
case 'E': | |
- load_file(); | |
+ load_file(fc); | |
break; | |
case 'N': | |
- new_file(); | |
+ new_file(fc); | |
break; | |
case 'x': | |
state = DEL; | |
- delete(); | |
+ delete(fc); | |
break; | |
case 'v': | |
state = VIS; | |
- visual_box(); | |
+ visual_box(fc); | |
break; | |
case 'q': | |
- check_modified();/** FALLTHROUGH **/ | |
+ check_modified(fc);/** FALLTHROUGH **/ | |
case 'Q': | |
cleanup(0); | |
exit(0); | |
@@ -867,9 +900,15 @@ void commands(){ | |
int main(int argc, char *argv[]){ | |
+ ARGBEGIN { | |
+ case 's': | |
+ silent = 1; | |
+ break; | |
+ } ARGEND; | |
+ | |
init(); | |
- commands(); | |
+ commands(stdin); | |
cleanup(0); | |
return 0; | |
} |