Skip to content

Commit 2bcd616

Browse files
committed
added tabs to edit
1 parent 7b7b294 commit 2bcd616

4 files changed

Lines changed: 43 additions & 7 deletions

File tree

user/edit/include/edit.h

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -8,7 +8,8 @@ typedef struct edit_state {
88
char* input_buffer;
99
bool is_edited;
1010
bool is_in_insert_mode;
11-
11+
bool show_tab_char;
12+
1213
unsigned int ln_cnt;
1314
unsigned int char_cnt;
1415

user/edit/input.c

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -129,6 +129,10 @@ bool listen_input(edit_state_t* state) {
129129
move_down(state);
130130
break;
131131

132+
case 't':
133+
state->show_tab_char = !state->show_tab_char;
134+
break;
135+
132136
case '+':
133137
state->file = freopen(state->file_name, "w", state->file);
134138
fseek(state->file, 0, SEEK_SET);

user/edit/main.c

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -54,6 +54,12 @@ int main(int argc, char* argv[], char* envp[]) {
5454

5555
idx++;
5656
}
57+
58+
if (file_name == NULL) {
59+
printf("Error: No file name provided\n");
60+
print_usage(argv[0]);
61+
return 1;
62+
}
5763

5864

5965
if (!getenv("NOSYX")) {
@@ -72,6 +78,7 @@ int main(int argc, char* argv[], char* envp[]) {
7278
state.is_edited = false;
7379
state.is_in_insert_mode = true;
7480
state.read_only = read_only;
81+
state.show_tab_char = false;
7582

7683
state.file = fopen(file_name, "r");
7784
if (state.file == NULL) {

user/edit/render.c

Lines changed: 30 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -4,6 +4,8 @@
44
#include <string.h>
55
#include <stdlib.h>
66

7+
#define TAB_WIDTH 4
8+
79
syntax_header_t* syntax = NULL;
810

911

@@ -29,6 +31,14 @@ void rerender_color(edit_state_t* state) {
2931
}
3032
}
3133

34+
void draw_line_number(int cur_y, int current_line) {
35+
char buff[16] = { 0 };
36+
memset(buff, 0, sizeof(buff));
37+
sprintf(buff, "%d.", current_line);
38+
draw_string(0, cur_y, buff, BACKGROUND_BLACK | FOREGROUND_LIGHTGRAY);
39+
}
40+
41+
3242
void render_tui(edit_state_t* state) {
3343
start_frame();
3444

@@ -86,9 +96,7 @@ void render_tui(edit_state_t* state) {
8696
if ((j >= start_line && j < start_line + viewport_height) && already_drawn <= viewport_height) {
8797
if (!initial_line_drawn) {
8898
initial_line_drawn = true;
89-
memset(buff, 0, 512);
90-
sprintf(buff, "%d.", current_line);
91-
draw_string(0, cur_y, buff, BACKGROUND_BLACK | FOREGROUND_LIGHTGRAY);
99+
draw_line_number(cur_y, current_line);
92100
}
93101

94102
if (i == state->buffer_idx) {
@@ -97,6 +105,24 @@ void render_tui(edit_state_t* state) {
97105
cursor_drawn = true;
98106
}
99107

108+
if (state->input_buffer[i] == '\t') {
109+
int col = cur_x - space_to_draw;
110+
int next_tab_stop = ((col / TAB_WIDTH) + 1) * TAB_WIDTH;
111+
int spaces = next_tab_stop - col;
112+
113+
for (int s = 0; s < spaces; s++) {
114+
if (cur_x >= width) {
115+
cur_y++;
116+
cur_x = space_to_draw;
117+
already_drawn++;
118+
draw_line_number(cur_y, current_line);
119+
}
120+
draw_char(cur_x, cur_y, state->show_tab_char ? '.' : ' ', BACKGROUND_BLACK | FOREGROUND_LIGHTGRAY);
121+
cur_x++;
122+
}
123+
cur_x--;
124+
}
125+
100126
if (state->input_buffer[i] >= 0x20 && state->input_buffer[i] <= 0x7E) {
101127
draw_char(cur_x, cur_y, state->input_buffer[i], BACKGROUND_BLACK | (color ? color_translation[color[i]] : FOREGROUND_WHITE));
102128
}
@@ -109,9 +135,7 @@ void render_tui(edit_state_t* state) {
109135
j++;
110136
cur_x = space_to_draw;
111137
cur_y++;
112-
memset(buff, 0, 512);
113-
sprintf(buff, "%d.", current_line);
114-
draw_string(0, cur_y, buff, BACKGROUND_BLACK | FOREGROUND_LIGHTGRAY);
138+
draw_line_number(cur_y, current_line);
115139
} else if (cur_x == width - 1) {
116140
cur_y++;
117141
cur_x = space_to_draw;

0 commit comments

Comments
 (0)