Cleanup and ui bar
This commit is contained in:
@@ -26,16 +26,16 @@ Editor *new_editor(const char *filename_arg, Coord position, Coord size) {
|
||||
}
|
||||
editor->root = load(str, len, optimal_chunk_size(len));
|
||||
free(str);
|
||||
Language language = language_for_file(filename.c_str());
|
||||
if (language.name != "unknown" && len <= (1024 * 128)) {
|
||||
editor->lang = language_for_file(filename.c_str());
|
||||
if (editor->lang.name != "unknown" && len <= (1024 * 128)) {
|
||||
editor->ts.parser = ts_parser_new();
|
||||
editor->ts.language = language.fn();
|
||||
editor->ts.language = editor->lang.fn();
|
||||
ts_parser_set_language(editor->ts.parser, editor->ts.language);
|
||||
editor->ts.query_file =
|
||||
get_exe_dir() + "/../grammar/" + language.name + ".scm";
|
||||
get_exe_dir() + "/../grammar/" + editor->lang.name + ".scm";
|
||||
}
|
||||
if (len <= (1024 * 28))
|
||||
request_add_to_lsp(language, editor);
|
||||
request_add_to_lsp(editor->lang, editor);
|
||||
return editor;
|
||||
}
|
||||
|
||||
|
||||
@@ -649,15 +649,6 @@ void handle_editor_event(Editor *editor, KeyEvent event) {
|
||||
}
|
||||
mode = NORMAL;
|
||||
break;
|
||||
case RUNNER:
|
||||
if (event.key_type == KEY_CHAR && event.len == 1) {
|
||||
switch (event.c[0]) {
|
||||
case 0x1B:
|
||||
mode = NORMAL;
|
||||
break;
|
||||
}
|
||||
}
|
||||
break;
|
||||
}
|
||||
ensure_scroll(editor);
|
||||
if ((event.key_type == KEY_CHAR || event.key_type == KEY_PASTE) && event.c)
|
||||
|
||||
@@ -42,11 +42,8 @@ void editor_lsp_handle(Editor *editor, json msg) {
|
||||
auto pos = message.find('\n');
|
||||
message =
|
||||
(pos == std::string::npos) ? message : message.substr(0, pos);
|
||||
std::string uri =
|
||||
percent_decode(rel["location"]["uri"].get<std::string>());
|
||||
auto pos2 = uri.find_last_of('/');
|
||||
if (pos2 != std::string::npos)
|
||||
uri = uri.substr(pos2 + 1);
|
||||
std::string uri = filename_from_path(
|
||||
percent_decode(rel["location"]["uri"].get<std::string>()));
|
||||
std::string row = std::to_string(
|
||||
rel["location"]["range"]["start"]["line"].get<int>());
|
||||
w.see_also.push_back(uri + ":" + row + ": " + message);
|
||||
|
||||
@@ -74,7 +74,7 @@ void editor_worker(Editor *editor) {
|
||||
exit(ENOMEM);
|
||||
std::shared_lock lockk(editor->knot_mtx);
|
||||
std::vector<Match> results =
|
||||
search_rope(editor->root, "(?:0x|#)[0-9a-fA-F]{6}");
|
||||
search_rope(editor->root, "(?:0x|#)[0-9a-fA-F]{6,8}\\b");
|
||||
if (results.size() > limit) {
|
||||
limit = results.size() + 50;
|
||||
free(hl_s);
|
||||
@@ -91,7 +91,7 @@ void editor_worker(Editor *editor) {
|
||||
s.start = results[i].start;
|
||||
s.end = results[i].end;
|
||||
int x = results[i].text[0] == '#' ? 1 : 2;
|
||||
uint32_t bg = HEX(results[i].text.substr(x));
|
||||
uint32_t bg = HEX(results[i].text.substr(x, 6));
|
||||
uint8_t r = bg >> 16;
|
||||
uint8_t g = (bg >> 8) & 0xFF;
|
||||
uint8_t b = bg & 0xFF;
|
||||
|
||||
38
src/main.cc
38
src/main.cc
@@ -3,13 +3,15 @@
|
||||
#include "io/sysio.h"
|
||||
#include "lsp/lsp.h"
|
||||
#include "ts/ts.h"
|
||||
#include "ui/bar.h"
|
||||
#include "utils/utils.h"
|
||||
|
||||
std::atomic<bool> running{true};
|
||||
Queue<KeyEvent> event_queue;
|
||||
std::vector<Editor *> editors;
|
||||
|
||||
uint8_t current_editor = 0;
|
||||
uint8_t mode = NORMAL;
|
||||
std::atomic<uint8_t> mode = NORMAL;
|
||||
|
||||
void background_worker() {
|
||||
while (running)
|
||||
@@ -58,7 +60,10 @@ int main(int argc, char *argv[]) {
|
||||
Coord screen = start_screen();
|
||||
const char *filename = (argc > 1) ? argv[1] : "";
|
||||
|
||||
Editor *editor = new_editor(filename, {0, 0}, screen);
|
||||
system(("bash " + get_exe_dir() + "/../scripts/init.sh").c_str());
|
||||
|
||||
Editor *editor = new_editor(filename, {0, 0}, {screen.row - 2, screen.col});
|
||||
Bar bar(screen);
|
||||
|
||||
if (!editor) {
|
||||
end_screen();
|
||||
@@ -76,20 +81,25 @@ int main(int argc, char *argv[]) {
|
||||
while (running) {
|
||||
KeyEvent event;
|
||||
while (event_queue.pop(event)) {
|
||||
if (event.key_type == KEY_MOUSE) {
|
||||
Editor *target = editor_at(event.mouse_x, event.mouse_y);
|
||||
if (!target)
|
||||
continue;
|
||||
if (event.mouse_state == PRESS)
|
||||
current_editor = index_of(target);
|
||||
event.mouse_x -= target->position.col;
|
||||
event.mouse_y -= target->position.row;
|
||||
throttle(4ms, handle_editor_event, target, event);
|
||||
if (mode != RUNNER) {
|
||||
if (event.key_type == KEY_MOUSE) {
|
||||
Editor *target = editor_at(event.mouse_x, event.mouse_y);
|
||||
if (!target)
|
||||
continue;
|
||||
if (event.mouse_state == PRESS)
|
||||
current_editor = index_of(target);
|
||||
event.mouse_x -= target->position.col;
|
||||
event.mouse_y -= target->position.row;
|
||||
handle_editor_event(target, event);
|
||||
} else {
|
||||
handle_editor_event(editors[current_editor], event);
|
||||
}
|
||||
} else {
|
||||
throttle(4ms, handle_editor_event, editors[current_editor], event);
|
||||
bar.handle(event);
|
||||
}
|
||||
}
|
||||
throttle(4ms, render_editor, editors[current_editor]);
|
||||
render_editor(editors[current_editor]);
|
||||
bar.render();
|
||||
throttle(4ms, render);
|
||||
}
|
||||
|
||||
@@ -102,6 +112,8 @@ int main(int argc, char *argv[]) {
|
||||
if (lsp_thread.joinable())
|
||||
lsp_thread.join();
|
||||
|
||||
system(("bash " + get_exe_dir() + "/../scripts/exit.sh").c_str());
|
||||
|
||||
end_screen();
|
||||
|
||||
for (auto editor : editors)
|
||||
|
||||
@@ -1 +1,96 @@
|
||||
#include "ui/bar.h"
|
||||
#include "io/sysio.h"
|
||||
#include "main.h"
|
||||
|
||||
void Bar::render() {
|
||||
Editor *editor = editors[current_editor];
|
||||
uint32_t row = screen.row - 2;
|
||||
uint32_t col = 0;
|
||||
uint32_t width = screen.col;
|
||||
uint32_t color = 0;
|
||||
uint32_t black = 0x0b0e14;
|
||||
uint32_t grey = 0x33363c;
|
||||
uint32_t dark_grey = 0x24272d;
|
||||
uint32_t name_color = 0xced4df;
|
||||
uint32_t lang_color = editor->lang.color;
|
||||
const char *symbol = " ";
|
||||
const char *name = "EDITOR";
|
||||
switch (mode) {
|
||||
case NORMAL:
|
||||
color = 0x82AAFF;
|
||||
symbol = " ";
|
||||
name = "NORMAL";
|
||||
break;
|
||||
case INSERT:
|
||||
color = 0xFF8F40;
|
||||
symbol = " ";
|
||||
name = "INSERT";
|
||||
break;
|
||||
case SELECT:
|
||||
color = 0x9ADE7A;
|
||||
symbol = " ";
|
||||
name = "SELECT";
|
||||
break;
|
||||
case RUNNER:
|
||||
color = 0xFFD700;
|
||||
symbol = " ";
|
||||
name = "RUNNER";
|
||||
break;
|
||||
case JUMPER:
|
||||
color = 0xF29CC3;
|
||||
symbol = " ";
|
||||
name = "JUMPER";
|
||||
break;
|
||||
}
|
||||
update(row, col, " ", black, color, CF_BOLD);
|
||||
update(row, ++col, symbol, black, color, CF_BOLD);
|
||||
update(row, ++col, "\x1b", black, color, CF_BOLD);
|
||||
update(row, ++col, " ", black, color, CF_BOLD);
|
||||
for (uint32_t i = 0; i < 6; i++)
|
||||
update(row, ++col, {name[i], 0}, black, color, CF_BOLD);
|
||||
update(row, ++col, " ", black, color, CF_BOLD);
|
||||
update(row, ++col, "◗", color, grey, CF_BOLD);
|
||||
update(row, ++col, "◗", grey, dark_grey, CF_BOLD);
|
||||
update(row, ++col, " ", name_color, dark_grey, CF_BOLD);
|
||||
update(row, ++col, editor->lang.symbol, lang_color, dark_grey, 0);
|
||||
update(row, ++col, "\x1b", lang_color, dark_grey, 0);
|
||||
update(row, ++col, " ", name_color, dark_grey, CF_BOLD);
|
||||
std::string filename = filename_from_path(editor->filename);
|
||||
for (uint32_t i = 0; i < filename.length(); i++)
|
||||
update(row, ++col, {filename[i], 0}, name_color, dark_grey, CF_BOLD);
|
||||
update(row, ++col, " ", name_color, dark_grey, CF_BOLD);
|
||||
update(row, ++col, "◗", dark_grey, 1, CF_BOLD);
|
||||
}
|
||||
|
||||
void Bar::handle(KeyEvent event) {
|
||||
if (event.key_type == KEY_CHAR && event.len == 1) {
|
||||
if (event.c[0] == 0x1B) {
|
||||
mode = NORMAL;
|
||||
} else if (event.c[0] == '\n' || event.c[0] == '\r') {
|
||||
// execute command while stripping starting `[:;]`
|
||||
mode = NORMAL;
|
||||
command = "";
|
||||
} else if (isprint((unsigned char)(event.c[0]))) {
|
||||
} else if (event.c[0] == 0x7F || event.c[0] == 0x08) { // backspace
|
||||
}
|
||||
} else if (event.key_type == KEY_SPECIAL) {
|
||||
switch (event.special_key) {
|
||||
case KEY_LEFT:
|
||||
if (command.length() > 0)
|
||||
cursor--;
|
||||
break;
|
||||
case KEY_RIGHT:
|
||||
if (cursor < command.length())
|
||||
cursor++;
|
||||
break;
|
||||
case KEY_UP:
|
||||
// TODO: history
|
||||
break;
|
||||
case KEY_DOWN:
|
||||
// TODO: history
|
||||
break;
|
||||
}
|
||||
} else if (event.key_type == KEY_PASTE) {
|
||||
} else if (event.key_type == KEY_MOUSE) {
|
||||
}
|
||||
}
|
||||
|
||||
@@ -23,6 +23,13 @@ std::string path_to_file_uri(const std::string &path_str) {
|
||||
return "file://" + percent_encode(path_abs(path_str));
|
||||
}
|
||||
|
||||
std::string filename_from_path(const std::string &path) {
|
||||
auto pos = path.find_last_of('/');
|
||||
if (pos == std::string::npos)
|
||||
return path;
|
||||
return path.substr(pos + 1);
|
||||
}
|
||||
|
||||
std::string get_exe_dir() {
|
||||
char exe_path[PATH_MAX];
|
||||
ssize_t count = readlink("/proc/self/exe", exe_path, PATH_MAX);
|
||||
|
||||
Reference in New Issue
Block a user