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

@@ -9,7 +9,6 @@
int repl(const b_command *cmd, const b_arglist *args, const b_array *_)
{
char buf[1024];
struct line_ed *ed = line_ed_create();
line_ed_set_flags(ed, LINE_ED_REMOVE_CONTINUATIONS);

View File

@@ -7,41 +7,41 @@ extern void print_lex_token(struct ivy_token *tok)
{
switch (tok->t_type) {
case IVY_TOK_KEYWORD:
b_fputs("[magenta]", stdout);
b_puts("[magenta]");
break;
case IVY_TOK_SYMBOL:
b_fputs("[blue]", stdout);
b_puts("[blue]");
break;
case IVY_TOK_ATOM:
b_fputs("[yellow]", stdout);
b_puts("[yellow]");
break;
case IVY_TOK_INT:
case IVY_TOK_DOUBLE:
b_fputs("[yellow]", stdout);
b_puts("[yellow]");
break;
case IVY_TOK_LABEL:
b_fputs("[red]", stdout);
b_puts("[red]");
break;
case IVY_TOK_IDENT:
b_fputs("[cyan]", stdout);
b_puts("[cyan]");
break;
case IVY_TOK_STRING:
b_fputs("[green]", stdout);
b_puts("[green]");
break;
case IVY_TOK_STR_START:
b_fputs("[green]", stdout);
b_puts("[green]");
break;
case IVY_TOK_STR_END:
b_fputs("[green]", stdout);
b_puts("[green]");
break;
case IVY_TOK_LINEFEED:
b_fputs("[bright,black]", stdout);
b_puts("[dark_grey]");
break;
default:
break;
}
printf("%s", ivy_lex_token_type_to_string(tok->t_type));
b_puts(ivy_lex_token_type_to_string(tok->t_type));
switch (tok->t_type) {
case IVY_TOK_IDENT:
@@ -66,39 +66,39 @@ extern void print_lex_token(struct ivy_token *tok)
break;
}
b_fputs("[reset]\n", stdout);
b_puts("[reset]\n");
}
extern void print_asm_lex_token(struct ivy_asm_token *tok)
{
switch (tok->t_type) {
case IVY_ASM_TOK_KEYWORD:
b_fputs("[magenta]", stdout);
b_puts("[magenta]");
break;
case IVY_ASM_TOK_SYMBOL:
b_fputs("[blue]", stdout);
b_puts("[blue]");
break;
case IVY_ASM_TOK_INT:
case IVY_ASM_TOK_DOUBLE:
b_fputs("[yellow]", stdout);
b_puts("[yellow]");
break;
case IVY_ASM_TOK_LABEL:
b_fputs("[red]", stdout);
b_puts("[red]");
break;
case IVY_ASM_TOK_IDENT:
b_fputs("[cyan]", stdout);
b_puts("[cyan]");
break;
case IVY_ASM_TOK_STRING:
b_fputs("[green]", stdout);
b_puts("[green]");
break;
case IVY_ASM_TOK_LINEFEED:
b_fputs("[bright,black]", stdout);
b_puts("[dark_grey]");
break;
default:
break;
}
printf("%s", ivy_asm_token_type_to_string(tok->t_type));
b_puts(ivy_asm_token_type_to_string(tok->t_type));
switch (tok->t_type) {
case IVY_ASM_TOK_IDENT:
@@ -126,5 +126,5 @@ extern void print_asm_lex_token(struct ivy_asm_token *tok)
break;
}
b_fputs("[reset]\n", stdout);
b_puts("[reset]\n");
}

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

View File

@@ -1,461 +0,0 @@
#include "../../../line-ed/tty.h"
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <sys/ioctl.h>
#include <termios.h>
#include <unistd.h>
#define ANSI_BOLD_ON "1"
#define ANSI_BOLD_OFF "22"
#define ANSI_ITALIC_ON "3"
#define ANSI_ITALIC_OFF "23"
#define ANSI_UNDERLINE_ON "4"
#define ANSI_UNDERLINE_OFF "24"
#define ANSI_DEFAULTCOLOUR_FG "39"
#define ANSI_DEFAULTCOLOUR_BG "49"
#define ANSI_256COLOUR_FG "38;5"
#define ANSI_256COLOUR_BG "48;5"
#define ANSI_TRUECOLOUR_FG "38;2"
#define ANSI_TRUECOLOUR_BG "48;2"
enum tty_flags {
TTY_INIT = 0x01u,
TTY_INTERACTIVE = 0x02u,
};
struct s_tty {
FILE *t_in, *t_out;
enum tty_flags t_flags;
struct termios t_ios_raw, t_ios_default;
struct s_tty_vmode t_vmode;
unsigned char t_mcount;
};
static struct s_tty tty = {};
static const char *ansi_colour16_fg[] = {
[TTY_COLOUR16_BLACK] = "30",
[TTY_COLOUR16_RED] = "31",
[TTY_COLOUR16_GREEN] = "32",
[TTY_COLOUR16_YELLOW] = "33",
[TTY_COLOUR16_BLUE] = "34",
[TTY_COLOUR16_MAGENTA] = "35",
[TTY_COLOUR16_CYAN] = "36",
[TTY_COLOUR16_WHITE] = "37",
[TTY_COLOUR16_GREY] = "90",
[TTY_COLOUR16_BRIGHT_RED] = "91",
[TTY_COLOUR16_BRIGHT_GREEN] = "92",
[TTY_COLOUR16_BRIGHT_YELLOW] = "93",
[TTY_COLOUR16_BRIGHT_BLUE] = "94",
[TTY_COLOUR16_BRIGHT_MAGENTA] = "95",
[TTY_COLOUR16_BRIGHT_CYAN] = "96",
[TTY_COLOUR16_BRIGHT_WHITE] = "97",
};
static const char *ansi_colour16_bg[] = {
[TTY_COLOUR16_BLACK] = "40",
[TTY_COLOUR16_RED] = "41",
[TTY_COLOUR16_GREEN] = "42",
[TTY_COLOUR16_YELLOW] = "43",
[TTY_COLOUR16_BLUE] = "44",
[TTY_COLOUR16_MAGENTA] = "45",
[TTY_COLOUR16_CYAN] = "46",
[TTY_COLOUR16_WHITE] = "47",
[TTY_COLOUR16_GREY] = "100",
[TTY_COLOUR16_BRIGHT_RED] = "101",
[TTY_COLOUR16_BRIGHT_GREEN] = "102",
[TTY_COLOUR16_BRIGHT_YELLOW] = "103",
[TTY_COLOUR16_BRIGHT_BLUE] = "104",
[TTY_COLOUR16_BRIGHT_MAGENTA] = "105",
[TTY_COLOUR16_BRIGHT_CYAN] = "106",
[TTY_COLOUR16_BRIGHT_WHITE] = "107",
};
static void init_tty(struct s_tty *tty, FILE *in, FILE *out)
{
tty->t_in = in;
tty->t_out = out;
int fd = fileno(in);
if (isatty(fd)) {
tty->t_flags |= TTY_INTERACTIVE;
}
tcgetattr(fd, &tty->t_ios_default);
memcpy(&tty->t_ios_raw, &tty->t_ios_default, sizeof tty->t_ios_raw);
tty->t_ios_raw.c_iflag &= ~(ICRNL | IXON);
tty->t_ios_raw.c_oflag &= ~(OPOST);
tty->t_ios_raw.c_lflag &= ~(ECHO | ICANON | IEXTEN);
tty->t_flags |= TTY_INIT;
}
struct s_tty *s_get_tty(void)
{
if (!(tty.t_flags & TTY_INIT)) {
init_tty(&tty, stdin, stdout);
}
return &tty;
}
bool s_tty_is_interactive(const struct s_tty *tty)
{
return (tty->t_flags & TTY_INTERACTIVE) == TTY_INTERACTIVE;
}
void s_tty_set_raw(struct s_tty *tty)
{
tcsetattr(STDIN_FILENO, TCSAFLUSH, &tty->t_ios_raw);
}
void s_tty_set_canon(struct s_tty *tty)
{
tcsetattr(STDIN_FILENO, TCSAFLUSH, &tty->t_ios_default);
}
void s_tty_reset_vmode(struct s_tty *tty)
{
if (tty->t_vmode.v_fg.c_mode == TTY_COLOUR_NONE
&& tty->t_vmode.v_bg.c_mode == TTY_COLOUR_NONE
&& tty->t_vmode.v_attrib == TTY_ATTRIB_NORMAL) {
return;
}
fprintf(tty->t_out, "\033[0;39;49m");
memset(&tty->t_vmode, 0x0, sizeof tty->t_vmode);
tty->t_vmode.v_fg.c_mode = TTY_COLOUR_NONE;
tty->t_vmode.v_bg.c_mode = TTY_COLOUR_NONE;
tty->t_vmode.v_attrib = TTY_ATTRIB_NORMAL;
}
static int compare_colour(
const struct s_tty_colour *a, const struct s_tty_colour *b)
{
if (a->c_mode != b->c_mode) {
return -1;
}
switch (a->c_mode) {
case TTY_COLOUR_16:
if (a->c_16.value != b->c_16.value) {
return -1;
}
break;
case TTY_COLOUR_256:
if (a->c_256.value != b->c_256.value) {
return -1;
}
break;
case TTY_COLOUR_TRUE:
if (a->c_true.r != b->c_true.r) {
return -1;
}
if (a->c_true.g != b->c_true.g) {
return -1;
}
if (a->c_true.b != b->c_true.b) {
return -1;
}
break;
default:
break;
}
return 0;
}
static int compare_vmode(const struct s_tty_vmode *a, const struct s_tty_vmode *b)
{
if (a->v_attrib != b->v_attrib) {
return -1;
}
if (compare_colour(&a->v_fg, &b->v_fg) != 0) {
return -1;
}
if (compare_colour(&a->v_bg, &b->v_bg) != 0) {
return -1;
}
return 0;
}
static void put_ansi_attrib(struct s_tty *tty, const char *s)
{
if (tty->t_mcount > 0) {
fputs(";", tty->t_out);
}
fputs(s, tty->t_out);
tty->t_mcount++;
}
static void set_attrib(
struct s_tty *tty, enum s_tty_attrib old, enum s_tty_attrib new)
{
if (old == new) {
return;
}
/* Bold ON */
if (!(old & TTY_ATTRIB_BOLD) && new &TTY_ATTRIB_BOLD) {
put_ansi_attrib(tty, ANSI_BOLD_ON);
}
/* Bold OFF */
if (old & TTY_ATTRIB_BOLD && !(new &TTY_ATTRIB_BOLD)) {
put_ansi_attrib(tty, ANSI_BOLD_OFF);
}
/* Underline ON */
if (!(old & TTY_ATTRIB_UNDERLINE) && new &TTY_ATTRIB_UNDERLINE) {
put_ansi_attrib(tty, ANSI_UNDERLINE_ON);
}
/* Underline OFF */
if (old & TTY_ATTRIB_UNDERLINE && !(new &TTY_ATTRIB_UNDERLINE)) {
put_ansi_attrib(tty, ANSI_UNDERLINE_OFF);
}
/* Italic ON */
if (!(old & TTY_ATTRIB_ITALIC) && new &TTY_ATTRIB_ITALIC) {
put_ansi_attrib(tty, ANSI_ITALIC_ON);
}
/* Italic OFF */
if (old & TTY_ATTRIB_ITALIC && !(new &TTY_ATTRIB_ITALIC)) {
put_ansi_attrib(tty, ANSI_ITALIC_OFF);
}
}
static void set_fg(
struct s_tty *tty, const struct s_tty_colour *old,
const struct s_tty_colour *new)
{
if (compare_colour(old, new) == 0) {
return;
}
char buf[8];
switch (new->c_mode) {
case TTY_COLOUR_NONE:
put_ansi_attrib(tty, ANSI_DEFAULTCOLOUR_FG);
break;
case TTY_COLOUR_16:
put_ansi_attrib(tty, ansi_colour16_fg[new->c_16.value]);
break;
case TTY_COLOUR_256:
put_ansi_attrib(tty, ANSI_256COLOUR_FG);
snprintf(buf, sizeof buf, "%u", new->c_256.value);
put_ansi_attrib(tty, buf);
break;
case TTY_COLOUR_TRUE:
put_ansi_attrib(tty, ANSI_TRUECOLOUR_FG);
snprintf(buf, sizeof buf, "%u", new->c_true.r);
put_ansi_attrib(tty, buf);
snprintf(buf, sizeof buf, "%u", new->c_true.g);
put_ansi_attrib(tty, buf);
snprintf(buf, sizeof buf, "%u", new->c_true.b);
put_ansi_attrib(tty, buf);
break;
default:
break;
}
}
static void set_bg(
struct s_tty *tty, const struct s_tty_colour *old,
const struct s_tty_colour *new)
{
if (compare_colour(old, new) == 0) {
return;
}
char buf[8];
switch (new->c_mode) {
case TTY_COLOUR_NONE:
put_ansi_attrib(tty, ANSI_DEFAULTCOLOUR_BG);
break;
case TTY_COLOUR_16:
put_ansi_attrib(tty, ansi_colour16_bg[new->c_16.value]);
break;
case TTY_COLOUR_256:
put_ansi_attrib(tty, ANSI_256COLOUR_BG);
snprintf(buf, sizeof buf, "%u", new->c_256.value);
put_ansi_attrib(tty, buf);
break;
case TTY_COLOUR_TRUE:
put_ansi_attrib(tty, ANSI_TRUECOLOUR_BG);
snprintf(buf, sizeof buf, "%u", new->c_true.r);
put_ansi_attrib(tty, buf);
snprintf(buf, sizeof buf, "%u", new->c_true.g);
put_ansi_attrib(tty, buf);
snprintf(buf, sizeof buf, "%u", new->c_true.b);
put_ansi_attrib(tty, buf);
break;
default:
break;
}
}
void s_tty_set_vmode(struct s_tty *tty, const struct s_tty_vmode *vmode)
{
if (compare_vmode(&tty->t_vmode, vmode) == 0) {
return;
}
tty->t_mcount = 0;
fprintf(tty->t_out, "\033[");
set_attrib(tty, tty->t_vmode.v_attrib, vmode->v_attrib);
set_fg(tty, &tty->t_vmode.v_fg, &vmode->v_fg);
set_bg(tty, &tty->t_vmode.v_bg, &vmode->v_bg);
fprintf(tty->t_out, "m");
memcpy(&tty->t_vmode, vmode, sizeof *vmode);
}
s_keycode s_tty_read_key(struct s_tty *tty)
{
char c;
int v;
int fd = fileno(tty->t_in);
while (1) {
v = read(fd, &c, 1);
if (v < 1) {
return S_KEY_EOF;
}
if (c == '\r' || c == '\n') {
return S_KEY_RETURN;
}
if (c == '\b' || c == 0x7F) {
return S_KEY_BACKSPACE;
}
if (c >= 1 && c <= 26) {
return S_TTY_CTRL_KEY(c + 'a' - 1);
}
if (c != 0x1b) {
return c;
}
v = read(fd, &c, 1);
if (v < 1) {
return S_KEY_EOF;
}
if (c != '[') {
continue;
}
v = read(fd, &c, 1);
if (v < 1) {
return S_KEY_EOF;
}
switch (c) {
case 'A':
return S_KEY_ARROW_UP;
case 'B':
return S_KEY_ARROW_DOWN;
case 'C':
return S_KEY_ARROW_RIGHT;
case 'D':
return S_KEY_ARROW_LEFT;
default:
continue;
}
}
return c;
}
void s_tty_move_cursor_x(struct s_tty *tty, enum s_tty_position_base base, int pos)
{
if (base == TTY_POS_CURSOR && pos < 0 && pos >= -4) {
for (int i = 0; i > pos; i--) {
fputc('\b', tty->t_out);
}
return;
}
if (base == TTY_POS_START) {
if (pos == 0) {
fputs("\033[G", tty->t_out);
} else {
fprintf(tty->t_out, "\033[%dG", pos + 1);
}
} else {
if (pos > 1) {
fprintf(tty->t_out, "\033[%dC", pos);
} else if (pos == 1) {
fputs("\033[C", tty->t_out);
} else if (pos == -1) {
fputs("\033[D", tty->t_out);
} else if (pos < -1) {
fprintf(tty->t_out, "\033[%dD", -pos);
}
}
}
void s_tty_move_cursor_y(struct s_tty *tty, enum s_tty_position_base base, int pos)
{
if (base == TTY_POS_START) {
/* we don't need this functionality right now */
abort();
}
if (pos > 1) {
fprintf(tty->t_out, "\033[%dB", pos);
} else if (pos == 1) {
fputs("\033[B", tty->t_out);
} else if (pos == -1) {
fputs("\033[A", tty->t_out);
} else if (pos < -1) {
fprintf(tty->t_out, "\033[%dA", -pos);
}
}
void s_tty_clear(struct s_tty *tty, enum s_tty_clear_mode mode)
{
const char *arg;
if (mode & TTY_CLEAR_ALL) {
arg = "2";
} else if (mode & TTY_CLEAR_TO_CURSOR) {
arg = "1";
} else if (mode & TTY_CLEAR_FROM_CURSOR) {
arg = "";
} else {
abort();
}
if (mode & TTY_CLEAR_SCREEN) {
fprintf(tty->t_out, "\033[%sJ", arg);
} else if (mode & TTY_CLEAR_LINE) {
fprintf(tty->t_out, "\033[%sK", arg);
} else {
abort();
}
}

View File

@@ -1,461 +0,0 @@
#include "../../../line-ed/tty.h"
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <sys/ioctl.h>
#include <termios.h>
#include <unistd.h>
#define ANSI_BOLD_ON "1"
#define ANSI_BOLD_OFF "22"
#define ANSI_ITALIC_ON "3"
#define ANSI_ITALIC_OFF "23"
#define ANSI_UNDERLINE_ON "4"
#define ANSI_UNDERLINE_OFF "24"
#define ANSI_DEFAULTCOLOUR_FG "39"
#define ANSI_DEFAULTCOLOUR_BG "49"
#define ANSI_256COLOUR_FG "38;5"
#define ANSI_256COLOUR_BG "48;5"
#define ANSI_TRUECOLOUR_FG "38;2"
#define ANSI_TRUECOLOUR_BG "48;2"
enum tty_flags {
TTY_INIT = 0x01u,
TTY_INTERACTIVE = 0x02u,
};
struct s_tty {
FILE *t_in, *t_out;
enum tty_flags t_flags;
struct termios t_ios_raw, t_ios_default;
struct s_tty_vmode t_vmode;
unsigned char t_mcount;
};
static struct s_tty tty = {};
static const char *ansi_colour16_fg[] = {
[TTY_COLOUR16_BLACK] = "30",
[TTY_COLOUR16_RED] = "31",
[TTY_COLOUR16_GREEN] = "32",
[TTY_COLOUR16_YELLOW] = "33",
[TTY_COLOUR16_BLUE] = "34",
[TTY_COLOUR16_MAGENTA] = "35",
[TTY_COLOUR16_CYAN] = "36",
[TTY_COLOUR16_WHITE] = "37",
[TTY_COLOUR16_GREY] = "90",
[TTY_COLOUR16_BRIGHT_RED] = "91",
[TTY_COLOUR16_BRIGHT_GREEN] = "92",
[TTY_COLOUR16_BRIGHT_YELLOW] = "93",
[TTY_COLOUR16_BRIGHT_BLUE] = "94",
[TTY_COLOUR16_BRIGHT_MAGENTA] = "95",
[TTY_COLOUR16_BRIGHT_CYAN] = "96",
[TTY_COLOUR16_BRIGHT_WHITE] = "97",
};
static const char *ansi_colour16_bg[] = {
[TTY_COLOUR16_BLACK] = "40",
[TTY_COLOUR16_RED] = "41",
[TTY_COLOUR16_GREEN] = "42",
[TTY_COLOUR16_YELLOW] = "43",
[TTY_COLOUR16_BLUE] = "44",
[TTY_COLOUR16_MAGENTA] = "45",
[TTY_COLOUR16_CYAN] = "46",
[TTY_COLOUR16_WHITE] = "47",
[TTY_COLOUR16_GREY] = "100",
[TTY_COLOUR16_BRIGHT_RED] = "101",
[TTY_COLOUR16_BRIGHT_GREEN] = "102",
[TTY_COLOUR16_BRIGHT_YELLOW] = "103",
[TTY_COLOUR16_BRIGHT_BLUE] = "104",
[TTY_COLOUR16_BRIGHT_MAGENTA] = "105",
[TTY_COLOUR16_BRIGHT_CYAN] = "106",
[TTY_COLOUR16_BRIGHT_WHITE] = "107",
};
static void init_tty(struct s_tty *tty, FILE *in, FILE *out)
{
tty->t_in = in;
tty->t_out = out;
int fd = fileno(in);
if (isatty(fd)) {
tty->t_flags |= TTY_INTERACTIVE;
}
tcgetattr(fd, &tty->t_ios_default);
memcpy(&tty->t_ios_raw, &tty->t_ios_default, sizeof tty->t_ios_raw);
tty->t_ios_raw.c_iflag &= ~(ICRNL | IXON);
tty->t_ios_raw.c_oflag &= ~(OPOST);
tty->t_ios_raw.c_lflag &= ~(ECHO | ICANON | IEXTEN);
tty->t_flags |= TTY_INIT;
}
struct s_tty *s_get_tty(void)
{
if (!(tty.t_flags & TTY_INIT)) {
init_tty(&tty, stdin, stdout);
}
return &tty;
}
bool s_tty_is_interactive(const struct s_tty *tty)
{
return (tty->t_flags & TTY_INTERACTIVE) == TTY_INTERACTIVE;
}
void s_tty_set_raw(struct s_tty *tty)
{
tcsetattr(STDIN_FILENO, TCSAFLUSH, &tty->t_ios_raw);
}
void s_tty_set_canon(struct s_tty *tty)
{
tcsetattr(STDIN_FILENO, TCSAFLUSH, &tty->t_ios_default);
}
void s_tty_reset_vmode(struct s_tty *tty)
{
if (tty->t_vmode.v_fg.c_mode == TTY_COLOUR_NONE
&& tty->t_vmode.v_bg.c_mode == TTY_COLOUR_NONE
&& tty->t_vmode.v_attrib == TTY_ATTRIB_NORMAL) {
return;
}
fprintf(tty->t_out, "\033[0;39;49m");
memset(&tty->t_vmode, 0x0, sizeof tty->t_vmode);
tty->t_vmode.v_fg.c_mode = TTY_COLOUR_NONE;
tty->t_vmode.v_bg.c_mode = TTY_COLOUR_NONE;
tty->t_vmode.v_attrib = TTY_ATTRIB_NORMAL;
}
static int compare_colour(
const struct s_tty_colour *a, const struct s_tty_colour *b)
{
if (a->c_mode != b->c_mode) {
return -1;
}
switch (a->c_mode) {
case TTY_COLOUR_16:
if (a->c_16.value != b->c_16.value) {
return -1;
}
break;
case TTY_COLOUR_256:
if (a->c_256.value != b->c_256.value) {
return -1;
}
break;
case TTY_COLOUR_TRUE:
if (a->c_true.r != b->c_true.r) {
return -1;
}
if (a->c_true.g != b->c_true.g) {
return -1;
}
if (a->c_true.b != b->c_true.b) {
return -1;
}
break;
default:
break;
}
return 0;
}
static int compare_vmode(const struct s_tty_vmode *a, const struct s_tty_vmode *b)
{
if (a->v_attrib != b->v_attrib) {
return -1;
}
if (compare_colour(&a->v_fg, &b->v_fg) != 0) {
return -1;
}
if (compare_colour(&a->v_bg, &b->v_bg) != 0) {
return -1;
}
return 0;
}
static void put_ansi_attrib(struct s_tty *tty, const char *s)
{
if (tty->t_mcount > 0) {
fputs(";", tty->t_out);
}
fputs(s, tty->t_out);
tty->t_mcount++;
}
static void set_attrib(
struct s_tty *tty, enum s_tty_attrib old, enum s_tty_attrib new)
{
if (old == new) {
return;
}
/* Bold ON */
if (!(old & TTY_ATTRIB_BOLD) && new &TTY_ATTRIB_BOLD) {
put_ansi_attrib(tty, ANSI_BOLD_ON);
}
/* Bold OFF */
if (old & TTY_ATTRIB_BOLD && !(new &TTY_ATTRIB_BOLD)) {
put_ansi_attrib(tty, ANSI_BOLD_OFF);
}
/* Underline ON */
if (!(old & TTY_ATTRIB_UNDERLINE) && new &TTY_ATTRIB_UNDERLINE) {
put_ansi_attrib(tty, ANSI_UNDERLINE_ON);
}
/* Underline OFF */
if (old & TTY_ATTRIB_UNDERLINE && !(new &TTY_ATTRIB_UNDERLINE)) {
put_ansi_attrib(tty, ANSI_UNDERLINE_OFF);
}
/* Italic ON */
if (!(old & TTY_ATTRIB_ITALIC) && new &TTY_ATTRIB_ITALIC) {
put_ansi_attrib(tty, ANSI_ITALIC_ON);
}
/* Italic OFF */
if (old & TTY_ATTRIB_ITALIC && !(new &TTY_ATTRIB_ITALIC)) {
put_ansi_attrib(tty, ANSI_ITALIC_OFF);
}
}
static void set_fg(
struct s_tty *tty, const struct s_tty_colour *old,
const struct s_tty_colour *new)
{
if (compare_colour(old, new) == 0) {
return;
}
char buf[8];
switch (new->c_mode) {
case TTY_COLOUR_NONE:
put_ansi_attrib(tty, ANSI_DEFAULTCOLOUR_FG);
break;
case TTY_COLOUR_16:
put_ansi_attrib(tty, ansi_colour16_fg[new->c_16.value]);
break;
case TTY_COLOUR_256:
put_ansi_attrib(tty, ANSI_256COLOUR_FG);
snprintf(buf, sizeof buf, "%u", new->c_256.value);
put_ansi_attrib(tty, buf);
break;
case TTY_COLOUR_TRUE:
put_ansi_attrib(tty, ANSI_TRUECOLOUR_FG);
snprintf(buf, sizeof buf, "%u", new->c_true.r);
put_ansi_attrib(tty, buf);
snprintf(buf, sizeof buf, "%u", new->c_true.g);
put_ansi_attrib(tty, buf);
snprintf(buf, sizeof buf, "%u", new->c_true.b);
put_ansi_attrib(tty, buf);
break;
default:
break;
}
}
static void set_bg(
struct s_tty *tty, const struct s_tty_colour *old,
const struct s_tty_colour *new)
{
if (compare_colour(old, new) == 0) {
return;
}
char buf[8];
switch (new->c_mode) {
case TTY_COLOUR_NONE:
put_ansi_attrib(tty, ANSI_DEFAULTCOLOUR_BG);
break;
case TTY_COLOUR_16:
put_ansi_attrib(tty, ansi_colour16_bg[new->c_16.value]);
break;
case TTY_COLOUR_256:
put_ansi_attrib(tty, ANSI_256COLOUR_BG);
snprintf(buf, sizeof buf, "%u", new->c_256.value);
put_ansi_attrib(tty, buf);
break;
case TTY_COLOUR_TRUE:
put_ansi_attrib(tty, ANSI_TRUECOLOUR_BG);
snprintf(buf, sizeof buf, "%u", new->c_true.r);
put_ansi_attrib(tty, buf);
snprintf(buf, sizeof buf, "%u", new->c_true.g);
put_ansi_attrib(tty, buf);
snprintf(buf, sizeof buf, "%u", new->c_true.b);
put_ansi_attrib(tty, buf);
break;
default:
break;
}
}
void s_tty_set_vmode(struct s_tty *tty, const struct s_tty_vmode *vmode)
{
if (compare_vmode(&tty->t_vmode, vmode) == 0) {
return;
}
tty->t_mcount = 0;
fprintf(tty->t_out, "\033[");
set_attrib(tty, tty->t_vmode.v_attrib, vmode->v_attrib);
set_fg(tty, &tty->t_vmode.v_fg, &vmode->v_fg);
set_bg(tty, &tty->t_vmode.v_bg, &vmode->v_bg);
fprintf(tty->t_out, "m");
memcpy(&tty->t_vmode, vmode, sizeof *vmode);
}
s_keycode s_tty_read_key(struct s_tty *tty)
{
char c;
int v;
int fd = fileno(tty->t_in);
while (1) {
v = read(fd, &c, 1);
if (v < 1) {
return S_KEY_EOF;
}
if (c == '\r' || c == '\n') {
return S_KEY_RETURN;
}
if (c == '\b' || c == 0x7F) {
return S_KEY_BACKSPACE;
}
if (c >= 1 && c <= 26) {
return S_TTY_CTRL_KEY(c + 'a' - 1);
}
if (c != 0x1b) {
return c;
}
v = read(fd, &c, 1);
if (v < 1) {
return S_KEY_EOF;
}
if (c != '[') {
continue;
}
v = read(fd, &c, 1);
if (v < 1) {
return S_KEY_EOF;
}
switch (c) {
case 'A':
return S_KEY_ARROW_UP;
case 'B':
return S_KEY_ARROW_DOWN;
case 'C':
return S_KEY_ARROW_RIGHT;
case 'D':
return S_KEY_ARROW_LEFT;
default:
continue;
}
}
return c;
}
void s_tty_move_cursor_x(struct s_tty *tty, enum s_tty_position_base base, int pos)
{
if (base == TTY_POS_CURSOR && pos < 0 && pos >= -4) {
for (int i = 0; i > pos; i--) {
fputc('\b', tty->t_out);
}
return;
}
if (base == TTY_POS_START) {
if (pos == 0) {
fputs("\033[G", tty->t_out);
} else {
fprintf(tty->t_out, "\033[%dG", pos + 1);
}
} else {
if (pos > 1) {
fprintf(tty->t_out, "\033[%dC", pos);
} else if (pos == 1) {
fputs("\033[C", tty->t_out);
} else if (pos == -1) {
fputs("\033[D", tty->t_out);
} else if (pos < -1) {
fprintf(tty->t_out, "\033[%dD", -pos);
}
}
}
void s_tty_move_cursor_y(struct s_tty *tty, enum s_tty_position_base base, int pos)
{
if (base == TTY_POS_START) {
/* we don't need this functionality right now */
abort();
}
if (pos > 1) {
fprintf(tty->t_out, "\033[%dB", pos);
} else if (pos == 1) {
fputs("\033[B", tty->t_out);
} else if (pos == -1) {
fputs("\033[A", tty->t_out);
} else if (pos < -1) {
fprintf(tty->t_out, "\033[%dA", -pos);
}
}
void s_tty_clear(struct s_tty *tty, enum s_tty_clear_mode mode)
{
const char *arg;
if (mode & TTY_CLEAR_ALL) {
arg = "2";
} else if (mode & TTY_CLEAR_TO_CURSOR) {
arg = "1";
} else if (mode & TTY_CLEAR_FROM_CURSOR) {
arg = "";
} else {
abort();
}
if (mode & TTY_CLEAR_SCREEN) {
fprintf(tty->t_out, "\033[%sJ", arg);
} else if (mode & TTY_CLEAR_LINE) {
fprintf(tty->t_out, "\033[%sK", arg);
} else {
abort();
}
}

View File

@@ -1,494 +0,0 @@
#include "../../../line-ed/tty.h"
#include <stdio.h>
#include <string.h>
#include <Windows.h>
#include <io.h>
#define ANSI_BOLD_ON "1"
#define ANSI_BOLD_OFF "22"
#define ANSI_ITALIC_ON "3"
#define ANSI_ITALIC_OFF "23"
#define ANSI_UNDERLINE_ON "4"
#define ANSI_UNDERLINE_OFF "24"
#define ANSI_DEFAULTCOLOUR_FG "39"
#define ANSI_DEFAULTCOLOUR_BG "49"
#define ANSI_256COLOUR_FG "38;5"
#define ANSI_256COLOUR_BG "48;5"
#define ANSI_TRUECOLOUR_FG "38;2"
#define ANSI_TRUECOLOUR_BG "48;2"
enum tty_flags {
TTY_INIT = 0x01u,
TTY_INTERACTIVE = 0x02u,
};
struct s_tty {
HANDLE t_in, t_out;
DWORD t_canon_mode_in, t_canon_mode_out;
enum s_key_code t_repeat_key;
unsigned int t_repeat_count;
enum tty_flags t_flags;
struct s_tty_vmode t_vmode;
unsigned char t_mcount;
};
static struct s_tty tty = {0};
static WORD ansi_colour16_fg[] = {
[TTY_COLOUR16_BLACK] = 0,
[TTY_COLOUR16_RED] = FOREGROUND_RED,
[TTY_COLOUR16_GREEN] = FOREGROUND_GREEN,
[TTY_COLOUR16_YELLOW] = FOREGROUND_RED | FOREGROUND_GREEN,
[TTY_COLOUR16_BLUE] = FOREGROUND_BLUE,
[TTY_COLOUR16_MAGENTA] = FOREGROUND_RED | FOREGROUND_BLUE,
[TTY_COLOUR16_CYAN] = FOREGROUND_GREEN | FOREGROUND_BLUE,
[TTY_COLOUR16_WHITE] = FOREGROUND_RED | FOREGROUND_GREEN | FOREGROUND_BLUE,
[TTY_COLOUR16_GREY] = FOREGROUND_INTENSITY,
[TTY_COLOUR16_BRIGHT_RED] = FOREGROUND_RED | FOREGROUND_INTENSITY,
[TTY_COLOUR16_BRIGHT_GREEN] = FOREGROUND_GREEN | FOREGROUND_INTENSITY,
[TTY_COLOUR16_BRIGHT_YELLOW] = FOREGROUND_RED | FOREGROUND_GREEN | FOREGROUND_INTENSITY,
[TTY_COLOUR16_BRIGHT_BLUE] = FOREGROUND_BLUE | FOREGROUND_INTENSITY,
[TTY_COLOUR16_BRIGHT_MAGENTA] = FOREGROUND_RED | FOREGROUND_BLUE | FOREGROUND_INTENSITY,
[TTY_COLOUR16_BRIGHT_CYAN] = FOREGROUND_GREEN | FOREGROUND_BLUE | FOREGROUND_INTENSITY,
[TTY_COLOUR16_BRIGHT_WHITE] = FOREGROUND_RED | FOREGROUND_GREEN | FOREGROUND_BLUE | FOREGROUND_INTENSITY,
};
static WORD ansi_colour16_bg[] = {
[TTY_COLOUR16_BLACK] = 0,
[TTY_COLOUR16_RED] = BACKGROUND_RED,
[TTY_COLOUR16_GREEN] = BACKGROUND_GREEN,
[TTY_COLOUR16_YELLOW] = BACKGROUND_RED | BACKGROUND_GREEN,
[TTY_COLOUR16_BLUE] = BACKGROUND_BLUE,
[TTY_COLOUR16_MAGENTA] = BACKGROUND_RED | BACKGROUND_BLUE,
[TTY_COLOUR16_CYAN] = BACKGROUND_GREEN | BACKGROUND_BLUE,
[TTY_COLOUR16_WHITE] = BACKGROUND_RED | BACKGROUND_GREEN | BACKGROUND_BLUE,
[TTY_COLOUR16_GREY] = BACKGROUND_INTENSITY,
[TTY_COLOUR16_BRIGHT_RED] = BACKGROUND_RED | BACKGROUND_INTENSITY,
[TTY_COLOUR16_BRIGHT_GREEN] = BACKGROUND_GREEN | BACKGROUND_INTENSITY,
[TTY_COLOUR16_BRIGHT_YELLOW] = BACKGROUND_RED | BACKGROUND_GREEN | BACKGROUND_INTENSITY,
[TTY_COLOUR16_BRIGHT_BLUE] = BACKGROUND_BLUE | BACKGROUND_INTENSITY,
[TTY_COLOUR16_BRIGHT_MAGENTA] = BACKGROUND_RED | BACKGROUND_BLUE | BACKGROUND_INTENSITY,
[TTY_COLOUR16_BRIGHT_CYAN] = BACKGROUND_GREEN | BACKGROUND_BLUE | BACKGROUND_INTENSITY,
[TTY_COLOUR16_BRIGHT_WHITE] = BACKGROUND_RED | BACKGROUND_GREEN | BACKGROUND_BLUE | BACKGROUND_INTENSITY,
};
static void init_tty(struct s_tty *tty, FILE *in, FILE *out)
{
HANDLE in_handle = (HANDLE)_get_osfhandle(fileno(in));
HANDLE out_handle = (HANDLE)_get_osfhandle(fileno(out));
tty->t_in = in_handle;
tty->t_out = out_handle;
tty->t_flags |= TTY_INIT;
DWORD mode = 0;
if (GetConsoleMode(in_handle, &mode)) {
tty->t_flags |= TTY_INTERACTIVE;
tty->t_canon_mode_in = mode;
}
if (GetConsoleMode(out_handle, &mode)) {
tty->t_canon_mode_out = mode;
}
}
struct s_tty *s_get_tty(void)
{
if (!(tty.t_flags & TTY_INIT)) {
init_tty(&tty, stdin, stdout);
}
return &tty;
}
bool s_tty_is_interactive(const struct s_tty *tty)
{
return (tty->t_flags & TTY_INTERACTIVE) == TTY_INTERACTIVE;
}
void s_tty_set_raw(struct s_tty *tty)
{
DWORD mode = tty->t_canon_mode_in;
mode &= ~(ENABLE_ECHO_INPUT | ENABLE_INSERT_MODE | ENABLE_LINE_INPUT | ENABLE_QUICK_EDIT_MODE | ENABLE_WINDOW_INPUT);
mode |= (ENABLE_PROCESSED_INPUT);
SetConsoleMode(tty->t_in, mode);
mode = tty->t_canon_mode_out;
//mode &= ~(ENABLE_PROCESSED_OUTPUT);
SetConsoleMode(tty->t_out, mode);
}
void s_tty_set_canon(struct s_tty *tty)
{
SetConsoleMode(tty->t_in, tty->t_canon_mode_in);
SetConsoleMode(tty->t_out, tty->t_canon_mode_out);
}
void s_tty_reset_vmode(struct s_tty *tty)
{
if (tty->t_vmode.v_fg.c_mode == TTY_COLOUR_NONE
&& tty->t_vmode.v_bg.c_mode == TTY_COLOUR_NONE
&& tty->t_vmode.v_attrib == TTY_ATTRIB_NORMAL) {
return;
}
WORD attrib = FOREGROUND_RED | FOREGROUND_GREEN | FOREGROUND_BLUE;
SetConsoleTextAttribute(tty->t_out, attrib);
memset(&tty->t_vmode, 0x0, sizeof tty->t_vmode);
tty->t_vmode.v_fg.c_mode = TTY_COLOUR_NONE;
tty->t_vmode.v_bg.c_mode = TTY_COLOUR_NONE;
tty->t_vmode.v_attrib = TTY_ATTRIB_NORMAL;
}
static int compare_colour(
const struct s_tty_colour *a, const struct s_tty_colour *b)
{
if (a->c_mode != b->c_mode) {
return -1;
}
switch (a->c_mode) {
case TTY_COLOUR_16:
if (a->c_16.value != b->c_16.value) {
return -1;
}
break;
case TTY_COLOUR_256:
if (a->c_256.value != b->c_256.value) {
return -1;
}
break;
case TTY_COLOUR_TRUE:
if (a->c_true.r != b->c_true.r) {
return -1;
}
if (a->c_true.g != b->c_true.g) {
return -1;
}
if (a->c_true.b != b->c_true.b) {
return -1;
}
break;
default:
break;
}
return 0;
}
static int compare_vmode(const struct s_tty_vmode *a, const struct s_tty_vmode *b)
{
if (a->v_attrib != b->v_attrib) {
return -1;
}
if (compare_colour(&a->v_fg, &b->v_fg) != 0) {
return -1;
}
if (compare_colour(&a->v_bg, &b->v_bg) != 0) {
return -1;
}
return 0;
}
static void put_ansi_attrib(struct s_tty *tty, const char *s)
{
if (tty->t_mcount > 0) {
fputs(";", tty->t_out);
}
fputs(s, tty->t_out);
tty->t_mcount++;
}
static void set_attrib(WORD *attrp, enum s_tty_attrib old, enum s_tty_attrib new)
{
if (old == new) {
return;
}
WORD attrib = *attrp;
/* Bold ON */
if (!(old & TTY_ATTRIB_BOLD) && new & TTY_ATTRIB_BOLD) {
attrib |= FOREGROUND_INTENSITY;
}
/* Bold OFF */
if (old & TTY_ATTRIB_BOLD && !(new & TTY_ATTRIB_BOLD)) {
attrib &= ~FOREGROUND_INTENSITY;
}
/* Underline ON */
if (!(old & TTY_ATTRIB_UNDERLINE) && new & TTY_ATTRIB_UNDERLINE) {
attrib |= COMMON_LVB_UNDERSCORE;
}
/* Underline OFF */
if (old & TTY_ATTRIB_UNDERLINE && !(new & TTY_ATTRIB_UNDERLINE)) {
attrib &= ~COMMON_LVB_UNDERSCORE;
}
/* Italic ON */
if (!(old & TTY_ATTRIB_ITALIC) && new & TTY_ATTRIB_ITALIC) {
/* not supported */
}
/* Italic OFF */
if (old & TTY_ATTRIB_ITALIC && !(new & TTY_ATTRIB_ITALIC)) {
/* not supported */
}
*attrp = attrib;
}
static void set_fg(
WORD *attrp, const struct s_tty_colour *old,
const struct s_tty_colour *new)
{
if (compare_colour(old, new) == 0) {
return;
}
WORD attrib = *attrp;
attrib
&= ~(FOREGROUND_RED | FOREGROUND_GREEN | FOREGROUND_BLUE
| FOREGROUND_INTENSITY);
switch (new->c_mode) {
case TTY_COLOUR_16:
attrib |= ansi_colour16_fg[new->c_16.value];
break;
default:
attrib |= FOREGROUND_RED | FOREGROUND_GREEN | FOREGROUND_BLUE;
break;
}
*attrp = attrib;
}
static void set_bg(
WORD *attrp, const struct s_tty_colour *old,
const struct s_tty_colour *new)
{
if (compare_colour(old, new) == 0) {
return;
}
WORD attrib = *attrp;
attrib
&= ~(BACKGROUND_RED | BACKGROUND_GREEN | BACKGROUND_BLUE
| BACKGROUND_INTENSITY);
switch (new->c_mode) {
case TTY_COLOUR_16:
attrib |= ansi_colour16_bg[new->c_16.value];
break;
default:
attrib |= BACKGROUND_RED | BACKGROUND_GREEN | BACKGROUND_BLUE;
break;
}
}
void s_tty_set_vmode(struct s_tty *tty, const struct s_tty_vmode *vmode)
{
if (compare_vmode(&tty->t_vmode, vmode) == 0) {
return;
}
tty->t_mcount = 0;
WORD attrib = 0;
set_attrib(&attrib, tty->t_vmode.v_attrib, vmode->v_attrib);
set_fg(&attrib, &tty->t_vmode.v_fg, &vmode->v_fg);
set_bg(&attrib, &tty->t_vmode.v_bg, &vmode->v_bg);
SetConsoleTextAttribute(tty->t_out, attrib);
memcpy(&tty->t_vmode, vmode, sizeof *vmode);
}
s_keycode s_tty_read_key(struct s_tty *tty)
{
if (tty->t_repeat_count > 0) {
tty->t_repeat_count--;
return tty->t_repeat_key;
}
INPUT_RECORD d;
HANDLE in = tty->t_in;
BOOL status = TRUE;
CONSOLE_READCONSOLE_CONTROL ctrl = {0};
DWORD nr_read = 0;
while (1) {
status = ReadConsoleInputA(in, &d, 1, &nr_read);
if (status == FALSE) {
return S_KEY_EOF;
}
if (d.EventType != KEY_EVENT) {
continue;
}
if (!d.Event.KeyEvent.bKeyDown) {
continue;
}
s_keycode key = 0;
switch (d.Event.KeyEvent.wVirtualKeyCode) {
case VK_CONTROL:
case VK_RCONTROL:
case VK_SHIFT:
case VK_RSHIFT:
case VK_MENU:
continue;
case VK_UP:
key = S_KEY_ARROW_UP;
break;
case VK_DOWN:
key = S_KEY_ARROW_DOWN;
break;
case VK_LEFT:
key = S_KEY_ARROW_LEFT;
break;
case VK_RIGHT:
key = S_KEY_ARROW_RIGHT;
break;
case VK_BACK:
key = S_KEY_BACKSPACE;
break;
case VK_RETURN:
key = S_KEY_RETURN;
break;
default:
if (d.Event.KeyEvent.uChar.UnicodeChar == 0) {
continue;
}
key = d.Event.KeyEvent.uChar.AsciiChar;
break;
}
if (d.Event.KeyEvent.dwControlKeyState & LEFT_CTRL_PRESSED) {
key = S_TTY_CTRL_KEY('a' + key - 1);
}
if (d.Event.KeyEvent.wRepeatCount > 1) {
tty->t_repeat_count = d.Event.KeyEvent.wRepeatCount - 1;
tty->t_repeat_key = key;
}
return key;
}
return S_KEY_EOF;
}
void s_tty_move_cursor_x(
struct s_tty* tty, enum s_tty_position_base base, int pos)
{
CONSOLE_SCREEN_BUFFER_INFO console = {0};
GetConsoleScreenBufferInfo(tty->t_out, &console);
COORD cursor_pos;
cursor_pos.Y = console.dwCursorPosition.Y;
switch (base) {
case TTY_POS_CURSOR:
cursor_pos.X = console.dwCursorPosition.X + pos;
break;
case TTY_POS_START:
cursor_pos.X = pos;
break;
default:
break;
}
SetConsoleCursorPosition(tty->t_out, cursor_pos);
}
void s_tty_move_cursor_y(struct s_tty *tty, enum s_tty_position_base base, int pos)
{
CONSOLE_SCREEN_BUFFER_INFO console = {0};
GetConsoleScreenBufferInfo(tty->t_out, &console);
COORD cursor_pos;
cursor_pos.X = console.dwCursorPosition.X;
switch (base) {
case TTY_POS_CURSOR:
cursor_pos.Y = console.dwCursorPosition.Y + pos;
break;
case TTY_POS_START:
cursor_pos.Y = pos;
break;
default:
break;
}
SetConsoleCursorPosition(tty->t_out, cursor_pos);
}
void s_tty_clear(struct s_tty *tty, enum s_tty_clear_mode mode)
{
CONSOLE_SCREEN_BUFFER_INFO console = {0};
GetConsoleScreenBufferInfo(tty->t_out, &console);
WCHAR fill = L' ';
DWORD length = 0;
COORD start;
DWORD all_length = 0, line_length = 0;
if (mode & TTY_CLEAR_ALL) {
line_length = console.dwSize.X;
all_length = line_length * console.dwSize.Y;
} else if (mode & TTY_CLEAR_FROM_CURSOR) {
line_length = console.dwSize.X - console.dwCursorPosition.X + 1;
all_length = line_length + ((console.dwSize.Y - console.dwCursorPosition.Y) * console.dwSize.X);
} else if (mode & TTY_CLEAR_TO_CURSOR) {
line_length = console.dwCursorPosition.X;
all_length = line_length
+ ((console.dwCursorPosition.Y - 1) * console.dwSize.X);
} else {
abort();
}
if (mode & TTY_CLEAR_SCREEN) {
length = all_length;
if ((mode & TTY_CLEAR_ALL) || (mode & TTY_CLEAR_TO_CURSOR)) {
start.X = 0;
start.Y = 0;
} else if (mode & TTY_CLEAR_FROM_CURSOR) {
start = console.dwCursorPosition;
}
} else if (mode & TTY_CLEAR_LINE) {
length = line_length;
if ((mode & TTY_CLEAR_ALL) || (mode & TTY_CLEAR_TO_CURSOR)) {
start.X = 0;
start.Y = console.dwCursorPosition.Y;
} else if (mode & TTY_CLEAR_FROM_CURSOR) {
start = console.dwCursorPosition;
}
} else {
abort();
}
DWORD nr_written = 0;
FillConsoleOutputCharacterW(tty->t_out, fill, length, start, &nr_written);
}