Rearrange code and cleanup
This commit is contained in:
171
include/io/knot.h
Normal file
171
include/io/knot.h
Normal file
@@ -0,0 +1,171 @@
|
||||
#ifndef ROPE_H
|
||||
#define ROPE_H
|
||||
|
||||
#include "pch.h"
|
||||
#include "utils/utils.h"
|
||||
|
||||
#define MIN_CHUNK_SIZE 64 // 64 Bytes
|
||||
#define MAX_CHUNK_SIZE 1024 * 8 // 8192 Bytes (8 KiB)
|
||||
#define DEPTH(n) ((n) ? (n)->depth : 0)
|
||||
|
||||
// Rope node definition
|
||||
typedef struct Knot {
|
||||
Knot *left;
|
||||
Knot *right;
|
||||
uint8_t depth;
|
||||
uint32_t chunk_size;
|
||||
uint32_t line_count;
|
||||
uint32_t char_count;
|
||||
char data[];
|
||||
} Knot;
|
||||
|
||||
typedef struct LineIterator {
|
||||
Knot *node;
|
||||
uint8_t top;
|
||||
uint32_t offset;
|
||||
Knot *stack[64];
|
||||
char *buffer;
|
||||
size_t capacity;
|
||||
} LineIterator;
|
||||
|
||||
typedef struct LeafIterator {
|
||||
Knot *node;
|
||||
uint8_t top;
|
||||
uint32_t offset;
|
||||
uint32_t adjustment;
|
||||
Knot *stack[64];
|
||||
} LeafIterator;
|
||||
|
||||
typedef struct ByteIterator {
|
||||
LeafIterator *it;
|
||||
uint32_t offset_l;
|
||||
uint32_t offset_g;
|
||||
uint32_t char_count;
|
||||
char *data;
|
||||
} ByteIterator;
|
||||
|
||||
// Rope operations
|
||||
|
||||
// Takes lengt of string to be converted
|
||||
// to rope and returns a suitable chunk size
|
||||
// but rope should work with any positive chunk size
|
||||
uint32_t optimal_chunk_size(uint64_t length);
|
||||
|
||||
// Takes a string (no need for null termination) and returns a rope
|
||||
// len is the length of the string, and chunk size is the size of each chunk
|
||||
// load does not free or consume the string.
|
||||
// and the str can be freed after load has been run.
|
||||
Knot *load(char *str, uint32_t len, uint32_t chunk_size);
|
||||
|
||||
// Balances the rope and returns the root
|
||||
// n is no longer valid / do not free
|
||||
// As rope is balanced by other functions
|
||||
// this is not to be used directly
|
||||
Knot *balance(Knot *n);
|
||||
|
||||
// Concatenates two ropes and returns the joined root
|
||||
// Balances the ropes too, if needed
|
||||
// left and right are no longer valid / do not free
|
||||
// ! left and right should have the same chunk size !
|
||||
Knot *concat(Knot *left, Knot *right);
|
||||
|
||||
// Used to insert text into the rope
|
||||
// node (the rope being inserted into) is no longer valid after call
|
||||
// instead use return value as the new node
|
||||
// offset is the position of the insertion relative to the start of the rope
|
||||
// str is the string to be inserted (no need for null termination)
|
||||
// len is the length of the string
|
||||
Knot *insert(Knot *node, uint32_t offset, char *str, uint32_t len);
|
||||
|
||||
// Similar to insert but for deletion
|
||||
// node (the rope being deleted from) is no longer valid after call
|
||||
// instead use return value as the new node
|
||||
// offset is the position of the deletion relative to the start of the rope
|
||||
// len is the length of the deletion
|
||||
Knot *erase(Knot *node, uint32_t offset, uint32_t len);
|
||||
|
||||
// Used to read a string from the rope
|
||||
// root is the rope to be read from
|
||||
// offset is the position of the read relative to the start of the rope
|
||||
// len is the length of the read
|
||||
// returns a null terminated string, should be freed by the caller
|
||||
char *read(Knot *root, uint32_t offset, uint32_t len);
|
||||
|
||||
// Used to split the rope into left and right ropes
|
||||
// node is the rope to be split (it is no longer valid after call / do not free)
|
||||
// offset is the position of the split relative to the start of the rope
|
||||
// left and right are pointers set to the root of that side of the split
|
||||
void split(Knot *node, uint32_t offset, Knot **left, Knot **right);
|
||||
|
||||
// Used to convert a byte offset to a line number that contains that byte
|
||||
uint32_t byte_to_line(Knot *node, uint32_t offset, uint32_t *out_col);
|
||||
|
||||
// Used to convert a line number to a byte offset (start of the line)
|
||||
// also sets out_len to the length of the line
|
||||
uint32_t line_to_byte(Knot *node, uint32_t line, uint32_t *out_len);
|
||||
|
||||
// Used to start a line iterator from the start_line number
|
||||
// root is the root of the rope
|
||||
// returned iterator must be freed after iteration is done
|
||||
LineIterator *begin_l_iter(Knot *root, uint32_t start_line);
|
||||
|
||||
// Each subsequent call returns the next line as a null terminated string
|
||||
// `it` is the iterator returned from begin_l_iter
|
||||
// After getting the necessary lines free the iterator (no need to go upto the
|
||||
// end) returns null if there are no more lines All return strings `must` be
|
||||
// freed by the caller
|
||||
char *next_line(LineIterator *it, uint32_t *out_len);
|
||||
|
||||
// Returns the previous line as a null terminated string
|
||||
// `it` is the iterator returned from begin_l_iter
|
||||
// it can be used to iterate backwards
|
||||
// and can be used along with next_line
|
||||
// doing prev_line then next_line or vice versa will return the same line
|
||||
// `out_len` is set to the length of the returned string
|
||||
char *prev_line(LineIterator *it, uint32_t *out_len);
|
||||
|
||||
// Used to start an iterator over leaf data
|
||||
// root is the root of the rope
|
||||
// the caller must free the iterator after use
|
||||
// start_offset is the byte from which the iterator should start
|
||||
LeafIterator *begin_k_iter(Knot *root, uint32_t start_offset);
|
||||
|
||||
// Returns the next leaf data as a null terminated string
|
||||
// `it` is the iterator returned from begin_k_iter
|
||||
// ! Strings returned must never be freed by the caller !
|
||||
// to mutate the string a copy must be made
|
||||
// `out_len` is set to the length of the returned string
|
||||
char *next_leaf(LeafIterator *it, uint32_t *out_len);
|
||||
|
||||
// Used to start an iterator over byte data (one byte at a time)
|
||||
// Uses leaf iterator internally
|
||||
// root is the root of the rope, the caller must free the iterator after use
|
||||
ByteIterator *begin_b_iter(Knot *root);
|
||||
|
||||
// Returns the next byte from the iterator
|
||||
// Returns '\0' if there are no more bytes left
|
||||
// `it` is the iterator returned from begin_b_iter
|
||||
char next_byte(ByteIterator *it);
|
||||
|
||||
// Returns a leaf data as a null terminated string
|
||||
// root is the root of the rope
|
||||
// start_offset is the byte from which the leaf data should start
|
||||
// `out_len` is set to the length of the returned string
|
||||
// return value must never be freed
|
||||
char *leaf_from_offset(Knot *root, uint32_t start_offset, uint32_t *out_len);
|
||||
|
||||
// Used to search for a pattern in the rope
|
||||
// Pattern is a null terminated string representing a regular expression (DFA
|
||||
// compliant) I.e some forms of backtracking etc. are not supported
|
||||
// root is the root of the rope to be searched
|
||||
// Returns a vector of pairs of start and length offsets (in bytes)
|
||||
std::vector<std::pair<size_t, size_t>> search_rope(Knot *root,
|
||||
const char *pattern);
|
||||
|
||||
// Helper function to free the rope
|
||||
// root is the root of the rope
|
||||
// the root is no longer valid after call
|
||||
// This must be called only once when the rope is no longer needed
|
||||
void free_rope(Knot *root);
|
||||
|
||||
#endif // ROPE_H
|
||||
101
include/io/ui.h
Normal file
101
include/io/ui.h
Normal file
@@ -0,0 +1,101 @@
|
||||
#ifndef UI_H
|
||||
#define UI_H
|
||||
|
||||
#include "pch.h"
|
||||
#include "utils/utils.h"
|
||||
|
||||
#define KEY_CHAR 0
|
||||
#define KEY_SPECIAL 1
|
||||
#define KEY_MOUSE 2
|
||||
#define KEY_PASTE 3
|
||||
#define KEY_NONE 4
|
||||
|
||||
#define KEY_UP 0
|
||||
#define KEY_DOWN 1
|
||||
#define KEY_LEFT 2
|
||||
#define KEY_RIGHT 3
|
||||
#define KEY_DELETE 4
|
||||
|
||||
#define KEY_ESC '\x1b'
|
||||
|
||||
#define PRESS 0
|
||||
#define RELEASE 1
|
||||
#define DRAG 2
|
||||
#define SCROLL 3
|
||||
|
||||
#define LEFT_BTN 0
|
||||
#define MIDDLE_BTN 1
|
||||
#define RIGHT_BTN 2
|
||||
#define SCROLL_BTN 3
|
||||
#define NONE_BTN 4
|
||||
|
||||
#define SCROLL_UP 0
|
||||
#define SCROLL_DOWN 1
|
||||
#define SCROLL_LEFT 2
|
||||
#define SCROLL_RIGHT 3
|
||||
|
||||
#define ALT 1
|
||||
#define CNTRL 2
|
||||
#define CNTRL_ALT 3
|
||||
#define SHIFT 4
|
||||
|
||||
#define DEFAULT 0
|
||||
#define BLOCK 2
|
||||
#define UNDERLINE 4
|
||||
#define CURSOR 6
|
||||
|
||||
enum CellFlags : uint8_t {
|
||||
CF_NONE = 0,
|
||||
CF_ITALIC = 1 << 0,
|
||||
CF_BOLD = 1 << 1,
|
||||
CF_UNDERLINE = 1 << 2
|
||||
};
|
||||
|
||||
struct ScreenCell {
|
||||
std::string utf8 = std::string("");
|
||||
uint8_t width = 1;
|
||||
uint32_t fg = 0;
|
||||
uint32_t bg = 0;
|
||||
uint8_t flags = CF_NONE;
|
||||
uint32_t ul_color = 0;
|
||||
};
|
||||
|
||||
struct KeyEvent {
|
||||
uint8_t key_type;
|
||||
|
||||
char *c;
|
||||
uint32_t len;
|
||||
|
||||
uint8_t special_key;
|
||||
uint8_t special_modifier;
|
||||
|
||||
uint8_t mouse_x;
|
||||
uint8_t mouse_y;
|
||||
uint8_t mouse_button;
|
||||
uint8_t mouse_state;
|
||||
uint8_t mouse_direction;
|
||||
uint8_t mouse_modifier;
|
||||
};
|
||||
|
||||
extern uint32_t rows, cols;
|
||||
extern std::vector<ScreenCell> screen;
|
||||
extern std::vector<ScreenCell> old_screen;
|
||||
extern std::mutex screen_mutex;
|
||||
|
||||
Coord start_screen();
|
||||
void end_screen();
|
||||
void update(uint32_t row, uint32_t col, std::string utf8, uint32_t fg,
|
||||
uint32_t bg, uint8_t flags);
|
||||
void update(uint32_t row, uint32_t col, const char *utf8, uint32_t fg,
|
||||
uint32_t bg, uint8_t flags);
|
||||
void update(uint32_t row, uint32_t col, std::string utf8, uint32_t fg,
|
||||
uint32_t bg, uint8_t flags, uint32_t ul_color);
|
||||
void update(uint32_t row, uint32_t col, const char *utf8, uint32_t fg,
|
||||
uint32_t bg, uint8_t flags, uint32_t ul_color);
|
||||
void set_cursor(int row, int col, int type, bool show_cursor_param);
|
||||
void render();
|
||||
Coord get_size();
|
||||
|
||||
KeyEvent read_key();
|
||||
|
||||
#endif
|
||||
Reference in New Issue
Block a user