-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathprint.c
196 lines (184 loc) · 5.75 KB
/
print.c
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
/* "print.c" - Gabriel Bauer (@ToCodeABluejay)
*
*Copyright (c) 2021 Gabriel Bauer (@ToCodeABluejay)
*
*Permission is hereby granted, free of charge, to any person obtaining a copy
*of this software and associated documentation files (the "Software"), to deal
*in the Software without restriction, including without limitation the rights
*to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
*copies of the Software, and to permit persons to whom the Software is
*furnished to do so, subject to the following conditions:
*
*The above copyright notice and this permission notice shall be included in all
*copies or substantial portions of the Software.
*
*THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
*IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
*FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
*AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
*LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
*OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
*SOFTWARE.
*/
#include "edit.h"
void initialize_editor()
//Initialize curses
{
initscr(); //Initialize screen and curses mode
if (!has_colors()) //Check to see if the console supports colored print, if not, then execute the following segment
{
endwin(); //Un-initialize the window
fprintf(stderr, "ERROR: This console does not support colored printing\n"); //Provide an error message
exit(1); //Exit the application
}
start_color(); //Otherwise, carry on wayward, initialize color and print the outline to the editor
init_pair(EDITOR_SCHEME, COLOR_BLACK, COLOR_WHITE); //Initialize color pair of black text on a white background (for editor)
init_pair(BOUNDARY_SCHEME, COLOR_BLACK, COLOR_CYAN); //Initialize color pair of black text on a cyan background (for top and bottom boundaries)
keypad(stdscr, true);
cbreak();
curs_set(1);
}
void border_line_print(int r, char *t, struct Window *w)
/*Print a Cyan line at row "r" with text "t", with
*restrictions of Window "w"
*/
{
attrset(COLOR_PAIR(BOUNDARY_SCHEME)); //Turn on attribute for the boundary color scheme
move(r, 0); //Set cursor to the beginning of the line for the y-position "row"
printw(t); //Print the given message
for (int i = strlen(t); i < w->width; i++) //Repeat the following code segment until the rest of the bar is full
{
printw(" "); //Print the rest without any characters in the foreground
}
move(r, 0); //Move the cursor back to the beginning of the line of the first row printed
}
void content_line_print(int r, struct Window *w, struct Cursor *c)
/*Print line at row "r" with text "t", with restrictions and
*contents of Window "w", using cursor position x,y
*/
{
long i = 0, j;
attrset(COLOR_PAIR(EDITOR_SCHEME)); //Turn on attribute for the boundary color scheme
move(r+1, 0); //Set cursor to the beginning of the line for the y-position "row" -- all being relative to editor bounds
j = get_line_number_pos(r+w->top, w->contents);
if (r==c->y)
j += (long) c->x-(c->x%w->width)-floor(c->x/w->width);
for (; i < w->width-1 && j+i < get_end_of_line(j, w->contents); i++) //Repeat the following code segment until the rest of the bar is full
{
if(w->contents[j+i]!='\t')
addch(w->contents[j+i]);
else
addch(' ');
}
if (i==w->width-1)
addch('>');
}
void print_contents(struct Window *w, struct Cursor *c, struct File *f)
//Used to print all of the regular editor contents
{
int i=0;
while (i<w->height-2)
{
content_line_print(i, w, c);
i++;
}
move(c->y-w->top+1, c->x%w->width);
}
bool msg_box(struct Window *w, char *msg)
/*A very cimple continue/cancel message
*window
*/
{
attrset(COLOR_PAIR(BOUNDARY_SCHEME));
move(0,0);
for(int i=0; i<=w->height*w->width; i++) addch(' ');
move(floor(w->height/2),0);
printw(msg);
move(floor(w->height/2)+1,0);
printw("Press enter to continue and backspace to cancel.");
switch (getch())
{
case '\n':
return true;
break;
case KEY_BACKSPACE:
return false;
break;
}
return NULL;
}
void dialog(struct Window *w, char *prompt, char *msg)
/*Prints a string msg on the top line
*and prompt on the bottom. Used equally
*for drawing the regular editor as well
*as for various prompts
*/
{
attrset(COLOR_PAIR(EDITOR_SCHEME));
move(0, 0);
for (int i = 0; i < w->height * w->width; i++) printw(" ");
border_line_print(0, msg, w);
border_line_print(w->height-1, prompt, w);
move(w->height-1, strlen(prompt));
}
void run_mode(int *m, struct Window *w, struct Cursor *c, struct File *f)
/*Prints to the console the coresponding output
*based upon the current given mode of operation
*/
{
switch (*m)
{
case EDIT_MODE:
dialog(w, "[Ctrl+c] Quit [F1] Save [F2] Save as [F3] Open [F4] New [F5] Delete line", "Unnamed Text Editor");
print_contents(w, c, f);
get_input(w, c, f);
break;
case NOT_SAVED:
if(msg_box(w, "File not saved! Do you want to continue?"))
{
strcpy(f->path, "");
*m=svdmd;
}
else
*m=EDIT_MODE;
break;
case OPEN_FILE:
dialog(w, "File path:", "Open File");
if(!dialog_input(w,f->path))
{
open_file(w,f);
*m=EDIT_MODE;
}
break;
case NEW_FILE:
dialog(w, "File path:", "New File");
if(!dialog_input(w,f->path))
{
new(w,c,f);
f->fp = fopen(f->path, "w");
if (f->fp)
{
f->ro=false;
fclose(f->fp);
}
*m=EDIT_MODE;
}
break;
case DEL_LINE:
dialog(w, "Line number:", "Delete Line");
if(!dialog_input(w,ln))
{
del_line(atoi(ln)-1, w->contents);
*m=EDIT_MODE;
}
break;
case SAVE_AS:
dialog(w, "File path:", "Save As");
if(!dialog_input(w,f->path))
{
save(w, f);
*m=EDIT_MODE;
}
break;
}
}