Major update
- Add an IO system to hijack a bit of the terminal. - Other minor fixes.
This commit is contained in:
+16
-1
@@ -4,11 +4,25 @@
|
||||
#include "internal/buffer/buffer.h"
|
||||
#include "internal/commands/commands.h"
|
||||
#include "internal/commands/suffixes.h"
|
||||
#include "internal/io/io.h"
|
||||
#include "internal/marks/marks.h"
|
||||
#include "internal/theme/theme.h"
|
||||
#include "pch.h"
|
||||
|
||||
namespace bed {
|
||||
struct Suggestion {
|
||||
enum : uint8_t {
|
||||
Command,
|
||||
Address,
|
||||
Suffix,
|
||||
History,
|
||||
Regex
|
||||
} type;
|
||||
std::string_view name;
|
||||
std::string_view desc;
|
||||
std::string completion;
|
||||
};
|
||||
|
||||
struct BEd {
|
||||
internal::trie::Trie<internal::commands::Command> commands;
|
||||
internal::commands::Command no_op;
|
||||
@@ -16,6 +30,7 @@ struct BEd {
|
||||
std::array<std::optional<internal::commands::Suffix>, 26> suffixes;
|
||||
internal::theme::Theme theme;
|
||||
std::unordered_map<std::string, internal::syntax::Language> languages;
|
||||
internal::io::IO &io;
|
||||
|
||||
bool help_mode = false;
|
||||
std::string last_help = "";
|
||||
@@ -27,7 +42,7 @@ struct BEd {
|
||||
std::unordered_map<std::string, internal::buffer::Buffer *> buffers;
|
||||
internal::buffer::Buffer *active;
|
||||
|
||||
BEd(std::vector<std::string> args);
|
||||
BEd(std::vector<std::string> args, internal::io::IO &io);
|
||||
~BEd();
|
||||
void handle(std::string_view cmd, bool eof);
|
||||
void run();
|
||||
|
||||
@@ -14,5 +14,18 @@ struct ed_error : std::runtime_error {
|
||||
ed_error(std::string msg) : std::runtime_error(msg) {}
|
||||
};
|
||||
|
||||
struct Highlight {
|
||||
enum : uint8_t {
|
||||
None = 0,
|
||||
Bold = 1 << 0,
|
||||
Italic = 1 << 1,
|
||||
Strikethrough = 1 << 2,
|
||||
Underline = 1 << 3,
|
||||
};
|
||||
uint32_t fg;
|
||||
uint32_t bg;
|
||||
uint8_t flags;
|
||||
};
|
||||
|
||||
struct BEd;
|
||||
} // namespace bed
|
||||
|
||||
@@ -7,4 +7,20 @@ enum struct Direction : uint8_t {
|
||||
Forward,
|
||||
Backward
|
||||
};
|
||||
|
||||
inline static bool write_all(int fd, const void *data, size_t len) {
|
||||
const char *p = (const char *)data;
|
||||
while (len > 0) {
|
||||
ssize_t n = write(fd, p, len);
|
||||
if (n > 0) {
|
||||
p += n;
|
||||
len -= (size_t)n;
|
||||
continue;
|
||||
}
|
||||
if (n == -1 && errno == EINTR)
|
||||
continue;
|
||||
return false;
|
||||
}
|
||||
return true;
|
||||
}
|
||||
} // namespace bed::internal
|
||||
|
||||
@@ -0,0 +1,81 @@
|
||||
#pragma once
|
||||
|
||||
#include "definitions.h"
|
||||
#include "pch.h"
|
||||
|
||||
namespace bed::internal::io {
|
||||
struct KeyEvent {
|
||||
enum struct KeyType {
|
||||
NONE,
|
||||
CHAR,
|
||||
SPECIAL,
|
||||
MOUSE,
|
||||
PASTE
|
||||
};
|
||||
enum struct SpecialKey {
|
||||
UP,
|
||||
DOWN,
|
||||
LEFT,
|
||||
RIGHT,
|
||||
DELETE,
|
||||
UNKNOWN
|
||||
};
|
||||
enum struct Modifier {
|
||||
NONE,
|
||||
SHIFT,
|
||||
ALT,
|
||||
CTRL,
|
||||
CTRL_ALT
|
||||
};
|
||||
enum struct MouseState {
|
||||
PRESS,
|
||||
RELEASE
|
||||
};
|
||||
|
||||
KeyType type = KeyType::NONE;
|
||||
std::string text;
|
||||
|
||||
SpecialKey special_key = SpecialKey::UNKNOWN;
|
||||
Modifier modifier = Modifier::NONE;
|
||||
|
||||
MouseState mouse_state = MouseState::PRESS;
|
||||
uint16_t mouse_x = 0;
|
||||
uint16_t mouse_y = 0;
|
||||
};
|
||||
|
||||
struct IO {
|
||||
IO();
|
||||
~IO();
|
||||
|
||||
IO(const IO &) = delete;
|
||||
IO &operator=(const IO &) = delete;
|
||||
|
||||
void enable_mouse();
|
||||
void disable_mouse();
|
||||
|
||||
std::pair<uint16_t, uint16_t> terminal_size();
|
||||
std::pair<uint16_t, uint16_t> cursor_position();
|
||||
void move_cursor(uint16_t row, uint16_t col);
|
||||
|
||||
std::pair<std::string, bool> get_command(BEd &);
|
||||
|
||||
KeyEvent read_key();
|
||||
|
||||
private:
|
||||
static termios orig_termios;
|
||||
static bool cleaned;
|
||||
static void cleanup();
|
||||
|
||||
std::deque<char> input_queue;
|
||||
|
||||
bool get_next_byte(char &out);
|
||||
void enqueue_bytes(const std::string &bytes);
|
||||
static int utf8_seq_len(uint8_t byte);
|
||||
|
||||
std::string read_next_unit();
|
||||
bool read_bracketed_paste(std::string &out);
|
||||
|
||||
KeyEvent parse_mouse(const std::string &buf);
|
||||
KeyEvent parse_escape(const std::string &buf);
|
||||
};
|
||||
} // namespace bed::internal::io
|
||||
@@ -5,19 +5,6 @@
|
||||
#include "pch.h"
|
||||
|
||||
namespace bed::internal::theme {
|
||||
struct Highlight {
|
||||
enum : uint8_t {
|
||||
None = 0,
|
||||
Bold = 1 << 0,
|
||||
Italic = 1 << 1,
|
||||
Strikethrough = 1 << 2,
|
||||
Underline = 1 << 3,
|
||||
};
|
||||
uint32_t fg;
|
||||
uint32_t bg;
|
||||
uint8_t flags;
|
||||
};
|
||||
|
||||
struct Theme {
|
||||
std::array<Highlight, internal::syntax::Token::Count> hl;
|
||||
|
||||
|
||||
Reference in New Issue
Block a user