- Treballant en el editor de codi

This commit is contained in:
2023-07-10 22:01:39 +02:00
parent 9d2a0801e1
commit 3b4501aaec
3 changed files with 224 additions and 6 deletions

215
main.cpp
View File

@@ -3,15 +3,41 @@
#include <string>
#include <list>
std::list<std::string> code;
int current_editor = 0;
void init_terminal();
void do_terminal();
void init_code_editor();
void do_code_editor();
void save_code();
void loop() {
do_terminal();
if (btnp(KEY_TAB)) {
current_editor = (++current_editor)%2;
switch(current_editor) {
case 0:
init_terminal();
break;
case 1:
init_code_editor();
break;
}
}
switch(current_editor) {
case 0:
do_terminal();
break;
case 1:
do_code_editor();
break;
}
}
void execute_run() {
//if (current_editor == 1) save_code();
if (current_editor == 1) save_code();
}
uint8_t get_char(uint8_t key) {
@@ -31,7 +57,7 @@ uint8_t get_char(uint8_t key) {
else if (key == KEY_4) { if (mods & KMOD_SHIFT) { return '$'; } else if (mods & KMOD_RALT) { return '~'; } else { return '4'; } }
else if (key == KEY_5) { if (mods & KMOD_SHIFT) { return '%'; } else if (mods & KMOD_RALT) { return 180; } else { return '5'; } }
else if (key == KEY_6) { if (mods & KMOD_SHIFT) { return '&'; } else if (mods & KMOD_RALT) { return 173; } else { return '6'; } }
else if (key == KEY_7) { if (mods & KMOD_SHIFT) { return '/'; } else { return '7'; } }
else if (key ==KEY_7) { if (mods & KMOD_SHIFT) { return '/'; } else { return '7'; } }
else if (key == KEY_8) { if (mods & KMOD_SHIFT) { return '('; } else { return '8'; } }
else if (key == KEY_9) { if (mods & KMOD_SHIFT) { return ')'; } else { return '9'; } }
else if (key == KEY_SPACE) return 32;
@@ -71,3 +97,186 @@ void do_terminal() {
}
}
}
void save_code() {
const char* file = get_filename();
FILE *f = fopen(file, "w");
for (std::list<std::string>::iterator it = code.begin(); it != code.end(); it++) {
fprintf(f, "%s\n", (*it).c_str());
}
fclose(f);
}
void load_code() {
const char* file = get_filename();
FILE *f = fopen(file, "rb");
fseek(f, 0, SEEK_END);
long fsize = ftell(f);
fseek(f, 0, SEEK_SET); /* same as rewind(f); */
char *buffer = (char*)malloc(fsize + 1);
fread(buffer, 1, fsize, f);
fclose(f);
buffer[fsize] = 0;
int start = 0;
for (int pos=0;pos<fsize;++pos) {
if (buffer[pos] == '\n') {
buffer[pos]='\0';
code.push_back(&buffer[start]);
start=pos+1;
} else if (buffer[pos] == '\r') {
buffer[pos]='\0';
code.push_back(&buffer[start]);
start=pos+1;
if (buffer[start] == '\n') {pos++; start++;}
}
}
free(buffer);
}
std::list<std::string>::iterator ls[28];
static int col = 0;
static int line = 0;
void refresh_code_editor() {
color(COLOR_WHITE, COLOR_BLUE);
cls();
//print(" File Edit Run ",0,0);
//print(" ",0,29);
std::list<std::string>::iterator it = code.begin();
int i=0;
while (it != code.end() && i<28) {
ls[i] = it;
print((*it).c_str(), 0, i);
++i; ++it;
}
}
void init_code_editor() {
mode(0);
code.clear();
load_code();
refresh_code_editor();
}
std::string get_current_line() {
return *ls[line];
}
void move_cursor_up() {
if (line > 0) {
line--;
if (get_current_line().size() < col) col = get_current_line().size();
}
}
void move_cursor_down() {
if (line < code.size()-1) {
line++;
if (get_current_line().size() < col) col = get_current_line().size();
}
}
void move_cursor_right() {
if ( col < get_current_line().size() ) {
col++;
} else {
if (line < code.size()-1) {
col = 0;
move_cursor_down();
}
}
}
void move_cursor_left() {
if (col > 0) {
col--;
} else {
if (line > 0) {
move_cursor_up();
col = get_current_line().size();
}
}
}
void move_line_end() {
col = get_current_line().size();
}
void move_line_home() {
col = 0;
}
void split_line() {
std::string str = get_current_line();
*ls[line] = str.substr(col);
std::list<std::string>::iterator newline = ls[line]++;
code.insert(newline, str.substr(0, col));
line++; col=0;
refresh_code_editor();
}
void join_lines() {
std::string str = get_current_line();
//std::list<std::string>::iterator prevline = ls[line]--;
code.erase(ls[line]);
line--;
const int line_size = (*ls[line]).size();
*ls[line] += str;
col = line_size;
refresh_code_editor();
}
void delete_char() {
if (col == 0) {
if (line > 0) join_lines();
} else {
std::string str = get_current_line();
std::string a = str.substr(0,col-1);
std::string b = str.substr(col);
*ls[line] = a+b;
col--;
refresh_code_editor();
}
}
void supr_char() {
if ((col == (*ls[line]).size()) && (line == code.size()-1)) return;
move_cursor_right();
delete_char();
}
void add_char(uint8_t chr) {
std::string str = get_current_line();
std::string a = str.substr(0,col);
std::string b = str.substr(col);
std::string c = " "; c[0] = chr;
*ls[line] = a+c+b;
col++;
refresh_code_editor();
}
void do_code_editor() {
const uint8_t key = whichbtn();
if (key != KEY_UNKNOWN) {
if (key == KEY_RETURN or key == KEY_KP_ENTER) {
split_line();
}
else if (key == KEY_UP) move_cursor_up();
else if (key == KEY_DOWN) move_cursor_down();
else if (key == KEY_LEFT) move_cursor_left();
else if (key == KEY_RIGHT) move_cursor_right();
else if (key == KEY_BACKSPACE) delete_char();
else if (key == KEY_DELETE) supr_char();
else if (key == KEY_END) move_line_end();
else if (key == KEY_HOME) move_line_home();
else {
uint8_t chr = get_char(key);
if (chr != 0) add_char(chr);
}
}
debug_set_cursor(col, line);
}