This commit is contained in:
Night Kaly 2024-11-12 11:46:13 +00:00
parent e91304b1e9
commit f3cd805695
Signed by: night0721
SSH key fingerprint: SHA256:B/hgVwUoBpx5vdNsXl9w8XwZljA9766uk6T4ubZp5HM

36
vip.c
View file

@ -17,6 +17,7 @@
#include "config.h" #include "config.h"
int cat_mode = 0;
struct termios newt, oldt; struct termios newt, oldt;
int rows, cols; int rows, cols;
editor_t editor[9]; editor_t editor[9];
@ -174,14 +175,16 @@ void refresh_screen(void)
cur_editor->coloff = cur_editor->rx - cols + 1; cur_editor->coloff = cur_editor->rx - cols + 1;
} }
if (!cat_mode)
bprintf("\033H\033[2 q"); bprintf("\033H\033[2 q");
draw_rows(); draw_rows();
if (!cat_mode) {
draw_status_bar(); draw_status_bar();
move_cursor((cur_editor->y - cur_editor->rowoff) + 1, move_cursor((cur_editor->y - cur_editor->rowoff) + 1,
(cur_editor->rx - cur_editor->coloff) + 1); (cur_editor->rx - cur_editor->coloff) + 1);
} }
}
void move_xy(int key) void move_xy(int key)
{ {
@ -560,8 +563,9 @@ void save_file(void)
void draw_rows(void) void draw_rows(void)
{ {
for (int y = 0; y < rows - 1; y++) { for (int y = 0; y < cur_editor->rows; y++) {
move_cursor(y + 1, 1); if (!cat_mode && y > rows - 2) return;
if (!cat_mode) move_cursor(y + 1, 1);
int filerow = y + cur_editor->rowoff; int filerow = y + cur_editor->rowoff;
if (filerow >= cur_editor->rows) { if (filerow >= cur_editor->rows) {
if (cur_editor->rows == 0 && y == rows / 2) { if (cur_editor->rows == 0 && y == rows / 2) {
@ -655,7 +659,7 @@ void draw_rows(void)
bprintf(WHITE_BG); bprintf(WHITE_BG);
} }
bprintf("\033[K"); bprintf("\033[K\n");
} }
} }
@ -823,7 +827,7 @@ void update_highlight(row_t *row)
} }
if (in_escape) { if (in_escape) {
if (c > 47 && c < 58 || c == 'n' || c == 't' || c == 'r') { if ((c > 47 && c < 58) || c == 'n' || c == 't' || c == 'r') {
row->hl[i] = ESCAPE; row->hl[i] = ESCAPE;
i++; i++;
prev_sep = 0; prev_sep = 0;
@ -1025,10 +1029,15 @@ int main(int argc, char **argv)
exit(1); exit(1);
} }
bprintf("\033[?1049h\033[2J\033[2q");
if (tcgetattr(STDIN_FILENO, &oldt) == -1) { if (tcgetattr(STDIN_FILENO, &oldt) == -1) {
die("tcgetattr"); die("tcgetattr");
} }
if (argc > 2 && !strcmp(argv[1], "-c")) {
cat_mode = 1;
} else {
bprintf("\033[?1049h\033[2J\033[2q");
newt = oldt; newt = oldt;
/* Disable canonical mode and echo */ /* Disable canonical mode and echo */
newt.c_iflag &= ~(BRKINT | ICRNL | INPCK | ISTRIP | IXON); newt.c_iflag &= ~(BRKINT | ICRNL | INPCK | ISTRIP | IXON);
@ -1039,10 +1048,20 @@ int main(int argc, char **argv)
if (tcsetattr(STDIN_FILENO, TCSAFLUSH, &newt) == -1) { if (tcsetattr(STDIN_FILENO, TCSAFLUSH, &newt) == -1) {
die("tcsetattr"); die("tcsetattr");
} }
}
for (int i = 0; i < argc; i++) { for (int i = 0; i < argc; i++) {
/* Create tabs for each arg */ /* Create tabs for each arg */
if (argv[i][0] != '-') {
init_editor(i == 0 ? NULL : argv[i]); init_editor(i == 0 ? NULL : argv[i]);
} }
}
if (cat_mode) {
cleanup();
refresh_screen();
return 0;
}
while (1) { while (1) {
refresh_screen(); refresh_screen();
@ -1284,6 +1303,7 @@ void cleanup(void)
if (tcsetattr(STDIN_FILENO, TCSAFLUSH, &oldt) == -1) { if (tcsetattr(STDIN_FILENO, TCSAFLUSH, &oldt) == -1) {
die("tcsetattr"); die("tcsetattr");
} }
if (!cat_mode)
bprintf("\033[2J\033[?1049l"); bprintf("\033[2J\033[?1049l");
} }
@ -1354,5 +1374,9 @@ void bprintf(const char *fmt, ...)
vsnprintf(buffer, sizeof(buffer), fmt, args); vsnprintf(buffer, sizeof(buffer), fmt, args);
va_end(args); va_end(args);
if (cat_mode) {
printf("%s", buffer);
} else {
write(STDOUT_FILENO, buffer, strlen(buffer)); write(STDOUT_FILENO, buffer, strlen(buffer));
} }
}