-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy patheditor.c
More file actions
184 lines (164 loc) · 5.22 KB
/
editor.c
File metadata and controls
184 lines (164 loc) · 5.22 KB
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
/* OneOS-ARM Simple Text Editor Implementation */
#include "editor.h"
#include "terminal.h"
#include "keyboard.h"
#include "vfs.h"
#include "string.h"
#include "virtio_input.h"
#define EDITOR_MAX_SIZE 4096
#define EDITOR_MAX_LINES 128
#define EDITOR_LINE_WIDTH 80
typedef struct {
char *buffer;
int size;
int capacity;
int cursor_x;
int cursor_y;
int scroll_y;
int modified;
} editor_state_t;
static editor_state_t editor = {0};
static void editor_display(void)
{
terminal_clear();
terminal_puts("=== Simple Text Editor ===\n");
terminal_puts("Ctrl+S: Save | Ctrl+Q: Quit | Arrow keys: Navigate\n");
terminal_puts("---\n");
/* Display buffer with line numbers */
int line = 0;
int pos = 0;
while (pos < editor.size && line < EDITOR_MAX_LINES) {
terminal_puthex(line + 1);
terminal_putchar(':');
terminal_putchar(' ');
/* Find end of line */
int line_start = pos;
while (pos < editor.size && editor.buffer[pos] != '\n') {
pos++;
}
/* Display line */
for (int i = line_start; i < pos; i++) {
terminal_putchar(editor.buffer[i]);
}
terminal_putchar('\n');
if (pos < editor.size && editor.buffer[pos] == '\n') {
pos++;
}
line++;
}
terminal_puts("---\n");
terminal_puts("Line ");
terminal_puthex(editor.cursor_y + 1);
terminal_puts(", Col ");
terminal_puthex(editor.cursor_x + 1);
if (editor.modified) {
terminal_puts(" *");
}
terminal_puts("\n");
terminal_render();
}
static void editor_save(const char *filename)
{
/* Simple save: write buffer to file */
int idx = vfs_find(filename, vfs_root());
if (idx < 0) {
idx = vfs_create(filename, vfs_root(), VFS_TYPE_FILE);
}
if (idx >= 0) {
vfs_write(idx, editor.buffer, editor.size);
editor.modified = 0;
terminal_puts("File saved: ");
terminal_puts(filename);
terminal_puts("\n");
} else {
terminal_puts("Error saving file!\n");
}
terminal_render();
}
void editor_open(const char *filename)
{
/* Initialize editor buffer */
editor.buffer = (char *)0x40300000; /* Use fixed memory address */
editor.capacity = EDITOR_MAX_SIZE;
editor.size = 0;
editor.cursor_x = 0;
editor.cursor_y = 0;
editor.scroll_y = 0;
editor.modified = 0;
/* Try to load file from VFS */
int idx = vfs_find(filename, vfs_root());
if (idx >= 0) {
vfs_node_t *node = vfs_get(idx);
if (node && node->type == VFS_TYPE_FILE) {
/* Read file data */
editor.size = node->size;
for (int i = 0; i < editor.size; i++) {
editor.buffer[i] = node->data[i];
}
editor.buffer[editor.size] = '\0';
/* Recalculate cursor_y based on lines */
int lines = 0;
int last_line_start = 0;
for (int i = 0; i < editor.size; i++) {
if (editor.buffer[i] == '\n') {
lines++;
last_line_start = i + 1;
}
}
editor.cursor_y = lines;
editor.cursor_x = editor.size - last_line_start;
}
}
/* Editor loop */
while (1) {
editor_display();
/* Poll for input */
uint8_t key = virtio_input_getchar();
if (!key) key = keyboard_read_nonblock();
if (key) {
if (key == 17) { /* Ctrl+Q */
if (editor.modified) {
terminal_puts("\nUnsaved changes! Ctrl+S to save, Ctrl+Q again to discard.\n");
terminal_render();
editor.modified = 0;
continue;
}
break;
}
else if (key == 19) { /* Ctrl+S */
editor_save(filename);
continue;
}
else if (key == '\b') { /* Backspace */
if (editor.size > 0) {
editor.size--;
editor.buffer[editor.size] = '\0';
editor.modified = 1;
}
}
else if (key == '\r' || key == '\n') { /* Enter */
if (editor.size < editor.capacity - 1) {
editor.buffer[editor.size++] = '\n';
editor.buffer[editor.size] = '\0';
editor.cursor_y++;
editor.cursor_x = 0;
editor.modified = 1;
}
}
else if (key >= 32 && key < 127) { /* Printable characters */
if (editor.size < editor.capacity - 1) {
editor.buffer[editor.size++] = (char)key;
editor.buffer[editor.size] = '\0';
editor.cursor_x++;
if (editor.cursor_x >= EDITOR_LINE_WIDTH) {
editor.cursor_x = 0;
editor.cursor_y++;
}
editor.modified = 1;
}
}
}
}
terminal_puts("Editor closed.\n");
terminal_render();
}