frontend: switch to new bluelib tty interface

This commit is contained in:
2024-11-20 22:14:35 +00:00
parent 92f407ac09
commit 17ee2b6b57
14 changed files with 86 additions and 1666 deletions

View File

@@ -64,11 +64,10 @@ struct hl_range *get_next_hl_range(struct hl_range *range)
return range;
}
int apply_hl_range(
struct hl_range *range, struct s_tty *tty, unsigned int x, unsigned int y)
int apply_hl_range(struct hl_range *range, b_tty *tty, unsigned int x, unsigned int y)
{
if (!range) {
s_tty_reset_vmode(tty);
b_tty_reset_vmode(tty);
return 0;
}
@@ -76,11 +75,11 @@ int apply_hl_range(
int cmp_end = compare_coords(x, y, range->h_end_x, range->h_end_y);
if (cmp_start < 0) {
s_tty_reset_vmode(tty);
b_tty_reset_vmode(tty);
}
if (cmp_start >= 0 && cmp_end <= 0) {
s_tty_set_vmode(tty, &range->h_vmode);
b_tty_set_vmode(tty, &range->h_vmode);
}
if (cmp_end == 1) {
@@ -92,7 +91,7 @@ int apply_hl_range(
struct hl_range *create_highlight(
unsigned long start_x, unsigned long start_y, unsigned long end_x,
unsigned long end_y, const struct s_tty_vmode *vmode)
unsigned long end_y, const b_tty_vmode *vmode)
{
struct hl_range *out = malloc(sizeof *out);
if (!out) {
@@ -181,7 +180,7 @@ static void move_start_to_meet_end(
void line_ed_put_highlight(
struct line_ed *ed, unsigned long start_x, unsigned long start_y,
unsigned long end_x, unsigned long end_y, const struct s_tty_vmode *vmode)
unsigned long end_x, unsigned long end_y, const struct b_tty_vmode *vmode)
{
struct hl_range *highlight
= create_highlight(start_x, start_y, end_x, end_y, vmode);

View File

@@ -1,7 +1,7 @@
#ifndef LINE_ED_HL_RANGE_H_
#define LINE_ED_HL_RANGE_H_
#include "tty.h"
#include <blue/term/tty.h>
#include <blue/core/queue.h>
@@ -20,7 +20,7 @@ enum hl_range_comparison {
struct hl_range {
unsigned int h_start_x, h_start_y;
unsigned int h_end_x, h_end_y;
struct s_tty_vmode h_vmode;
b_tty_vmode h_vmode;
b_queue_entry h_entry;
};
@@ -30,11 +30,11 @@ extern struct hl_range *get_hl_range(
struct line_ed *ed, unsigned int x, unsigned int y);
extern struct hl_range *get_next_hl_range(struct hl_range *range);
extern int apply_hl_range(
struct hl_range *range, struct s_tty *tty, unsigned int x, unsigned int y);
struct hl_range *range, b_tty *tty, unsigned int x, unsigned int y);
extern struct hl_range *create_highlight(
unsigned long start_x, unsigned long start_y, unsigned long end_x,
unsigned long end_y, const struct s_tty_vmode *vmode);
unsigned long end_y, const struct b_tty_vmode *vmode);
extern enum hl_range_comparison compare_hl_ranges(
const struct hl_range *a, const struct hl_range *b);

View File

@@ -12,7 +12,7 @@ void line_ed_remove_hook(struct line_ed *ed, struct line_ed_hook *hook)
b_queue_delete(&ed->l_hooks, &hook->hook_entry);
}
void hook_keypress(struct line_ed *ed, s_keycode key)
void hook_keypress(struct line_ed *ed, b_keycode key)
{
b_queue_iterator it;
b_queue_foreach (&it, &ed->l_hooks) {

View File

@@ -1,7 +1,7 @@
#ifndef LINE_ED_HOOK_H_
#define LINE_ED_HOOK_H_
#include "tty.h"
#include <blue/term/tty.h>
enum hook_id {
HOOK_KEYPRESS,
@@ -10,7 +10,7 @@ enum hook_id {
struct line_ed;
extern void hook_keypress(struct line_ed *ed, s_keycode key);
extern void hook_keypress(struct line_ed *ed, b_keycode key);
extern void hook_buffer_modified(struct line_ed *ed);
#endif

View File

@@ -113,7 +113,7 @@ void cursor_left(struct line_ed *ed)
{
if (ed->l_cursor_x != 0) {
//fputs("\010", stdout);
s_tty_move_cursor_x(ed->l_tty, TTY_POS_CURSOR, -1);
b_tty_move_cursor_x(ed->l_tty, B_TTY_POS_CURSOR, -1);
fflush(stdout);
ed->l_cursor_x--;
ed->l_buf_ptr--;
@@ -135,8 +135,8 @@ void cursor_left(struct line_ed *ed)
ed->l_cursor_x = len - 1;
//printf("\033[A\033[%dG", len + prompt_len);
s_tty_move_cursor_y(ed->l_tty, TTY_POS_CURSOR, -1);
s_tty_move_cursor_x(ed->l_tty, TTY_POS_START, len + prompt_len);
b_tty_move_cursor_y(ed->l_tty, B_TTY_POS_CURSOR, -1);
b_tty_move_cursor_x(ed->l_tty, B_TTY_POS_START, len + prompt_len);
fflush(stdout);
}
@@ -151,7 +151,7 @@ void cursor_right(struct line_ed *ed)
ed->l_cursor_x++;
ed->l_buf_ptr++;
//fputs("\033[C", stdout);
s_tty_move_cursor_x(ed->l_tty, TTY_POS_CURSOR, 1);
b_tty_move_cursor_x(ed->l_tty, B_TTY_POS_CURSOR, 1);
fflush(stdout);
return;
}
@@ -165,8 +165,8 @@ void cursor_right(struct line_ed *ed)
ed->l_buf_ptr++;
//printf("\033[B\033[G");
s_tty_move_cursor_y(ed->l_tty, TTY_POS_CURSOR, 1);
s_tty_move_cursor_x(ed->l_tty, TTY_POS_START, 0);
b_tty_move_cursor_y(ed->l_tty, B_TTY_POS_CURSOR, 1);
b_tty_move_cursor_x(ed->l_tty, B_TTY_POS_START, 0);
fflush(stdout);
}
@@ -178,13 +178,13 @@ void arrow_up(struct line_ed *ed)
if (ed->l_cursor_y > 0) {
//printf("\033[%uA", ed->l_cursor_y);
s_tty_move_cursor_y(ed->l_tty, TTY_POS_CURSOR, ed->l_cursor_y);
b_tty_move_cursor_y(ed->l_tty, B_TTY_POS_CURSOR, ed->l_cursor_y);
}
//printf("\033[%zuG\033[J", prompt_length(ed, PROMPT_MAIN) + 1);
s_tty_move_cursor_x(
ed->l_tty, TTY_POS_START, prompt_length(ed, PROMPT_MAIN));
s_tty_clear(ed->l_tty, TTY_CLEAR_SCREEN | TTY_CLEAR_FROM_CURSOR);
b_tty_move_cursor_x(
ed->l_tty, B_TTY_POS_START, prompt_length(ed, PROMPT_MAIN));
b_tty_clear(ed->l_tty, B_TTY_CLEAR_SCREEN | B_TTY_CLEAR_FROM_CURSOR);
save_buf_to_history(ed);
ed->l_history_pos--;
@@ -202,12 +202,12 @@ void arrow_down(struct line_ed *ed)
if (ed->l_cursor_y > 0) {
//printf("\033[%uA", ed->l_cursor_y);
s_tty_move_cursor_y(ed->l_tty, TTY_POS_CURSOR, ed->l_cursor_y);
b_tty_move_cursor_y(ed->l_tty, B_TTY_POS_CURSOR, ed->l_cursor_y);
}
//printf("\033[%zuG\033[J", prompt_length(ed, PROMPT_MAIN) + 1);
s_tty_move_cursor_x(
ed->l_tty, TTY_POS_START, prompt_length(ed, PROMPT_MAIN) + 1);
b_tty_move_cursor_x(
ed->l_tty, B_TTY_POS_START, prompt_length(ed, PROMPT_MAIN) + 1);
save_buf_to_history(ed);
ed->l_history_pos++;

View File

@@ -1,10 +1,8 @@
#include "line-ed.h"
#include "history.h"
#include "hook.h"
#include "input.h"
#include "prompt.h"
#include "tty.h"
#include <ctype.h>
#include <stdio.h>
@@ -12,6 +10,7 @@
#include <string.h>
#include <wchar.h>
#include <wctype.h>
#include <blue/term/tty.h>
#define LINE_ED_FROM_LEX_SOURCE(p) \
((struct line_ed *)((char *)p - offsetof(struct line_ed, l_line_source)))
@@ -56,7 +55,7 @@ struct line_ed *line_ed_create(void)
return NULL;
}
out->l_tty = s_get_tty();
out->l_tty = b_stdtty;
out->l_buf_end = out->l_buf + LINE_MAX;
out->l_buf_ptr = out->l_buf;
out->l_line_end = out->l_buf;
@@ -181,8 +180,8 @@ long line_ed_readline(struct line_ed *ed, char *out, size_t max)
append_to_index = ed->l_history_pos;
}
struct s_tty *tty = ed->l_tty;
s_tty_set_raw(tty);
b_tty *tty = ed->l_tty;
b_tty_set_mode(tty, B_TTY_RAW);
show_prompt(ed);
for (int i = 0; ed->l_buf[i]; i++) {
@@ -198,10 +197,10 @@ long line_ed_readline(struct line_ed *ed, char *out, size_t max)
bool eof = false;
while (!end) {
s_keycode key = s_tty_read_key(tty);
b_keycode key = b_tty_read_key(tty);
hook_keypress(ed, key);
if (key == S_TTY_CTRL_KEY('d')) {
if (key == B_TTY_CTRL_KEY('d')) {
if (!input_is_empty(ed)) {
continue;
}
@@ -210,13 +209,13 @@ long line_ed_readline(struct line_ed *ed, char *out, size_t max)
break;
}
if (key & S_MOD_CTRL) {
if (key & B_MOD_CTRL) {
continue;
}
switch (key) {
case S_KEY_RETURN:
s_tty_reset_vmode(tty);
case B_KEY_RETURN:
b_tty_reset_vmode(tty);
if (ed->l_line_end > ed->l_buf
&& *(ed->l_line_end - 1) != '\\') {
end = true;
@@ -243,19 +242,19 @@ long line_ed_readline(struct line_ed *ed, char *out, size_t max)
// fputs("\033[G\n", stdout);
show_prompt(ed);
break;
case S_KEY_BACKSPACE:
case B_KEY_BACKSPACE:
backspace(ed);
break;
case S_KEY_ARROW_LEFT:
case B_KEY_ARROW_LEFT:
cursor_left(ed);
break;
case S_KEY_ARROW_RIGHT:
case B_KEY_ARROW_RIGHT:
cursor_right(ed);
break;
case S_KEY_ARROW_UP:
case B_KEY_ARROW_UP:
arrow_up(ed);
break;
case S_KEY_ARROW_DOWN:
case B_KEY_ARROW_DOWN:
arrow_down(ed);
break;
default:
@@ -266,7 +265,7 @@ long line_ed_readline(struct line_ed *ed, char *out, size_t max)
}
}
s_tty_set_canon(tty);
b_tty_set_mode(tty, B_TTY_CANONICAL);
fputc('\n', stdout);
if (*ed->l_buf == '\0') {

View File

@@ -3,20 +3,19 @@
#define LINE_MAX 4096
#include "tty.h"
#include <blue/term/tty.h>
#include <blue/core/queue.h>
#include <blue/object/array.h>
#include <ivy/line-source.h>
#include <stddef.h>
struct s_tty;
struct s_tty_vmode;
struct b_tty_vmode;
struct line_ed;
struct line_ed_hook {
void (*hook_keypress)(struct line_ed *, struct line_ed_hook *, s_keycode);
void (*hook_keypress)(struct line_ed *, struct line_ed_hook *, b_keycode);
void (*hook_buffer_modified)(struct line_ed *, struct line_ed_hook *);
b_queue_entry hook_entry;
};
@@ -58,7 +57,7 @@ struct line_ed {
struct ivy_line_source l_line_source;
/* pointer to tty interface */
struct s_tty *l_tty;
b_tty *l_tty;
/* the lexical scope that we are currently in.
* this is provided by components further up the input pipeline,
@@ -83,7 +82,7 @@ extern void line_ed_set_scope_type(struct line_ed *ed, const char *scope_type);
extern void line_ed_put_highlight(
struct line_ed *ed, unsigned long start_x, unsigned long start_y,
unsigned long end_x, unsigned long end_y, const struct s_tty_vmode *vmode);
unsigned long end_x, unsigned long end_y, const struct b_tty_vmode *vmode);
extern void line_ed_clear_highlights(struct line_ed *ed);
extern void line_ed_print_highlights(struct line_ed *ed);

View File

@@ -5,7 +5,7 @@
#include "buffer.h"
#include "cursor.h"
#include "hl-range.h"
#include "tty.h"
#include <blue/term/tty.h>
/* prints the provided string to the terminal, applying any relevant highlight ranges.
* this function prints all characters in `s` until it encounters a null char (\0) or
@@ -16,12 +16,12 @@
*/
void print_text(struct line_ed *ed, unsigned int x, unsigned int y, const char *s)
{
struct s_tty *tty = s_get_tty();
b_tty *tty = ed->l_tty;
struct hl_range *cur_range = get_hl_range(ed, x, y);
for (unsigned int i = 0; s[i] != '\n' && s[i] != '\0'; i++) {
if (!cur_range) {
s_tty_reset_vmode(tty);
b_tty_reset_vmode(tty);
fputc(s[i], stdout);
continue;
}
@@ -96,18 +96,18 @@ void put_refresh(struct line_ed *ed, struct refresh_state *state)
size_t line_len = strcspn(line_buf, "\n");
/* the index of the first char in line_buf that needs to be reprinted */
unsigned int start_x = state->r_prev_cursor_x;
int start_x = state->r_prev_cursor_x;
/* the distance between the first char to be reprinted and the end
* of the line.
* the physical cursor will be moved back by this amount after the
* line is reprinted. */
unsigned int cursor_rdelta = line_len - start_x;
int cursor_rdelta = (int)(line_len - start_x);
if (ed->l_flags & LINE_ED_FULL_REPRINT) {
if (start_x) {
//fprintf(stdout, "\033[%uD", start_x);
s_tty_move_cursor_x(ed->l_tty, TTY_POS_CURSOR, -start_x);
b_tty_move_cursor_x(ed->l_tty, B_TTY_POS_CURSOR, -start_x);
}
start_x = 0;
@@ -119,7 +119,7 @@ void put_refresh(struct line_ed *ed, struct refresh_state *state)
* so that the physical cursor will be placed AFTER the character that
* was just inserted. */
cursor_rdelta--;
s_tty_move_cursor_x(ed->l_tty, TTY_POS_CURSOR, -cursor_rdelta);
b_tty_move_cursor_x(ed->l_tty, B_TTY_POS_CURSOR, -cursor_rdelta);
#if 0
for (unsigned int i = 0; i < cursor_rdelta; i++) {
@@ -162,21 +162,21 @@ void backspace_nl_refresh(struct line_ed *ed, struct refresh_state *state)
* has just been moved up. from here, clear this line and the rest
* from the screen. */
//fputs("\033[J", stdout);
s_tty_clear(ed->l_tty, TTY_CLEAR_SCREEN | TTY_CLEAR_FROM_CURSOR);
b_tty_clear(ed->l_tty, B_TTY_CLEAR_SCREEN | B_TTY_CLEAR_FROM_CURSOR);
if (ed->l_flags & LINE_ED_FULL_REPRINT) {
/* next, move the physical cursor up and to the beginning of the previous line */
unsigned int tmp_x;
line_ed_coords_to_physical_coords(ed, 0, ed->l_cursor_y, &tmp_x, NULL);
s_tty_move_cursor_y(ed->l_tty, TTY_POS_CURSOR, -1);
s_tty_move_cursor_x(ed->l_tty, TTY_POS_START, tmp_x);
b_tty_move_cursor_y(ed->l_tty, B_TTY_POS_CURSOR, -1);
b_tty_move_cursor_x(ed->l_tty, B_TTY_POS_START, tmp_x);
//fprintf(stdout, "\033[A\033[%uG", tmp_x + 1);
start_x = 0;
} else {
/* next, move the physical cursor up and to the end of the previous line */
//fprintf(stdout, "\033[A\033[%uG", new_x);
s_tty_move_cursor_y(ed->l_tty, TTY_POS_CURSOR, -1);
s_tty_move_cursor_x(ed->l_tty, TTY_POS_START, new_x);
b_tty_move_cursor_y(ed->l_tty, B_TTY_POS_CURSOR, -1);
b_tty_move_cursor_x(ed->l_tty, B_TTY_POS_START, new_x);
}
/* now reprint all of the buffer lines, starting with the first of the
@@ -202,11 +202,11 @@ void backspace_nl_refresh(struct line_ed *ed, struct refresh_state *state)
* were concatenated. */
if (ydiff) {
//fprintf(stdout, "\033[%uA", ydiff);
s_tty_move_cursor_y(ed->l_tty, TTY_POS_CURSOR, ydiff);
b_tty_move_cursor_y(ed->l_tty, B_TTY_POS_CURSOR, ydiff);
}
//fprintf(stdout, "\033[%uG", new_x);
s_tty_move_cursor_x(ed->l_tty, TTY_POS_START, new_x);
b_tty_move_cursor_x(ed->l_tty, B_TTY_POS_START, new_x);
fflush(stdout);
}
@@ -220,26 +220,26 @@ void backspace_simple_refresh(struct line_ed *ed, struct refresh_state *state)
size_t line_len = strcspn(line_buf, "\n");
/* the index of the first char in line_buf that needs to be reprinted */
unsigned int start_x = ed->l_cursor_x;
int start_x = ed->l_cursor_x;
//get_data_cursor_position(ed, &start_x, NULL);
/* the distance between the first char to be reprinted and the end
* of the line.
* the physical cursor will be moved back by this amount after the
* line is reprinted. */
unsigned int cursor_rdelta = line_len - start_x;
int cursor_rdelta = (int)(line_len - start_x);
if (ed->l_flags & LINE_ED_FULL_REPRINT) {
if (start_x) {
//fprintf(stdout, "\033[%uD", start_x);
s_tty_move_cursor_x(ed->l_tty, TTY_POS_CURSOR, -start_x);
b_tty_move_cursor_x(ed->l_tty, B_TTY_POS_CURSOR, -start_x);
}
start_x = 0;
}
//fputc('\010', stdout);
s_tty_move_cursor_x(ed->l_tty, TTY_POS_CURSOR, -1);
b_tty_move_cursor_x(ed->l_tty, B_TTY_POS_CURSOR, -1);
print_text(ed, start_x, ed->l_cursor_y, line_buf + start_x);
fputc(' ', stdout);
@@ -248,7 +248,7 @@ void backspace_simple_refresh(struct line_ed *ed, struct refresh_state *state)
* the fact that backspace was just pressed) */
cursor_rdelta++;
s_tty_move_cursor_x(ed->l_tty, TTY_POS_CURSOR, -cursor_rdelta);
b_tty_move_cursor_x(ed->l_tty, B_TTY_POS_CURSOR, -cursor_rdelta);
#if 0
for (unsigned int i = 0; i < cursor_rdelta; i++) {

View File

@@ -1,160 +0,0 @@
#ifndef DARWIN_TTY_H_
#define DARWIN_TTY_H_
#include <stdbool.h>
#include <stdint.h>
#define S_TTY_CTRL_KEY(c) ((c) | S_MOD_CTRL)
struct s_tty;
#define MAKE_VMODE(fg, bg, a) \
{ \
.v_fg = fg, \
.v_bg = bg, \
.v_attrib = (a) \
}
#define MAKE_COLOUR_DEFAULT(v) \
{ \
.c_mode = TTY_COLOUR_NONE, \
}
#define MAKE_COLOUR_16(v) \
{ \
.c_mode = TTY_COLOUR_16, \
.c_16 = { \
.value = (v) \
} \
}
#define MAKE_COLOUR_256(v) \
{ \
.c_mode = TTY_COLOUR_256, \
.c_256 = { \
.value = (v) \
} \
}
#define MAKE_COLOUR_TRUE(cr, cg, cb) \
{ \
.c_mode = TTY_COLOUR_TRUE, \
.c_true = { \
.r = (cr), \
.g = (cg), \
.b = (cb) \
} \
}
typedef uint32_t s_keycode;
/* codepoints U+F0000 to U+FFFFD are reserved areas in Unicode, free for
* application use. store special keycodes here */
enum s_keys {
S_KEY_ARROW_LEFT = 0xF0000,
S_KEY_ARROW_RIGHT,
S_KEY_ARROW_UP,
S_KEY_ARROW_DOWN,
S_KEY_BACKSPACE,
S_KEY_RETURN,
S_MOD_CTRL = 0x10000000,
S_KEY_EOF = 0xFFFFFFFF,
};
enum s_tty_colour16 {
TTY_COLOUR16_BLACK = 0,
TTY_COLOUR16_RED,
TTY_COLOUR16_GREEN,
TTY_COLOUR16_YELLOW,
TTY_COLOUR16_BLUE,
TTY_COLOUR16_MAGENTA,
TTY_COLOUR16_CYAN,
TTY_COLOUR16_WHITE,
TTY_COLOUR16_GREY,
TTY_COLOUR16_BRIGHT_RED,
TTY_COLOUR16_BRIGHT_GREEN,
TTY_COLOUR16_BRIGHT_YELLOW,
TTY_COLOUR16_BRIGHT_BLUE,
TTY_COLOUR16_BRIGHT_MAGENTA,
TTY_COLOUR16_BRIGHT_CYAN,
TTY_COLOUR16_BRIGHT_WHITE,
};
enum s_tty_colour_mode {
TTY_COLOUR_NONE = 0,
TTY_COLOUR_16,
TTY_COLOUR_256,
TTY_COLOUR_TRUE,
};
enum s_tty_position_base {
TTY_POS_START,
TTY_POS_CURSOR,
};
enum s_tty_attrib {
TTY_ATTRIB_NORMAL = 0x00u,
TTY_ATTRIB_BOLD = 0x01u,
TTY_ATTRIB_UNDERLINE = 0x02u,
TTY_ATTRIB_ITALIC = 0x04u,
};
enum s_tty_clear_mode {
TTY_CLEAR_LINE = 0x01u,
TTY_CLEAR_SCREEN = 0x02,
TTY_CLEAR_TO_CURSOR = 0x0100u,
TTY_CLEAR_FROM_CURSOR = 0x0200u,
TTY_CLEAR_ALL = 0x0400u,
};
struct s_tty_colour {
enum s_tty_colour_mode c_mode;
union {
struct {
uint8_t value;
} c_16;
struct {
uint8_t value;
} c_256;
struct {
uint8_t r;
uint8_t g;
uint8_t b;
} c_true;
};
};
struct s_tty_vmode {
enum s_tty_attrib v_attrib;
struct s_tty_colour v_fg;
struct s_tty_colour v_bg;
};
static inline unsigned int s_keycode_get_key(s_keycode v)
{
return v & 0x0FFFFFFF;
}
extern struct s_tty *s_get_tty(void);
extern bool s_tty_is_interactive(const struct s_tty *tty);
extern void s_tty_set_raw(struct s_tty *tty);
extern void s_tty_set_canon(struct s_tty *tty);
extern void s_tty_reset_vmode(struct s_tty *tty);
extern void s_tty_set_vmode(struct s_tty *tty, const struct s_tty_vmode *vmode);
extern s_keycode s_tty_read_key(struct s_tty *tty);
extern void s_tty_move_cursor_x(
struct s_tty *tty, enum s_tty_position_base base, int pos);
extern void s_tty_move_cursor_y(
struct s_tty *tty, enum s_tty_position_base base, int pos);
extern void s_tty_clear(struct s_tty *tty, enum s_tty_clear_mode mode);
#endif