term: extend tty interface with more features
This commit is contained in:
@@ -1,43 +1,10 @@
|
||||
#ifndef BLUELIB_PRINT_H_
|
||||
#define BLUELIB_PRINT_H_
|
||||
#ifndef BLUELIB_TERM_H_
|
||||
#define BLUELIB_TERM_H_
|
||||
|
||||
#include <blue/core/status.h>
|
||||
#include <stdio.h>
|
||||
|
||||
#define b_i(...) b_print(B_PRINT_I, __VA_ARGS__)
|
||||
#define b_warn(...) b_print(B_PRINT_WARN, __VA_ARGS__)
|
||||
#define b_err(...) b_print(B_PRINT_ERR, __VA_ARGS__)
|
||||
|
||||
typedef enum b_paragraph_format_flags {
|
||||
B_PARAGRAPH_DONT_INDENT_FIRST_LINE = 0x01u,
|
||||
B_PARAGRAPH_HYPHENATE = 0x02u,
|
||||
B_PARAGRAPH_DOUBLE_LINE_BREAK = 0x04u,
|
||||
B_PARAGRAPH_MULTI_LINE_BREAK = 0x08u,
|
||||
} b_paragraph_format_flags;
|
||||
|
||||
typedef struct b_paragraph_format {
|
||||
b_paragraph_format_flags p_flags;
|
||||
|
||||
unsigned int p_left_margin;
|
||||
unsigned int p_right_margin;
|
||||
unsigned int p_line_length;
|
||||
} b_paragraph_format;
|
||||
|
||||
typedef enum b_print_format {
|
||||
B_PRINT_NORMAL = 0,
|
||||
B_PRINT_I,
|
||||
B_PRINT_WARN,
|
||||
B_PRINT_ERR,
|
||||
} b_print_format;
|
||||
|
||||
BLUE_API b_status b_term_get_dimensions(FILE *fp, unsigned int *w, unsigned int *h);
|
||||
|
||||
BLUE_API b_status b_print(b_print_format format, const char *str, ...);
|
||||
BLUE_API b_status b_print_paragraph(
|
||||
const char *str, FILE *fp, b_paragraph_format *format);
|
||||
|
||||
BLUE_API int b_fputs(const char *str, FILE *fp);
|
||||
BLUE_API int b_printf(const char *format, ...);
|
||||
BLUE_API int b_fprintf(FILE *fp, const char *format, ...);
|
||||
#include <blue/term/tty.h>
|
||||
#include <blue/term/print.h>
|
||||
|
||||
#endif
|
||||
|
||||
45
term/include/blue/term/print.h
Normal file
45
term/include/blue/term/print.h
Normal file
@@ -0,0 +1,45 @@
|
||||
#ifndef BLUELIB_TERM_PRINT_H_
|
||||
#define BLUELIB_TERM_PRINT_H_
|
||||
|
||||
#include <blue/core/misc.h>
|
||||
#include <blue/core/status.h>
|
||||
#include <blue/term/tty.h>
|
||||
#include <stdarg.h>
|
||||
|
||||
#define b_i(...) b_print(B_PRINT_I, __VA_ARGS__)
|
||||
#define b_warn(...) b_print(B_PRINT_WARN, __VA_ARGS__)
|
||||
#define b_err(...) b_print(B_PRINT_ERR, __VA_ARGS__)
|
||||
|
||||
struct b_tty;
|
||||
|
||||
typedef enum b_paragraph_format_flags {
|
||||
B_PARAGRAPH_DONT_INDENT_FIRST_LINE = 0x01u,
|
||||
B_PARAGRAPH_HYPHENATE = 0x02u,
|
||||
B_PARAGRAPH_DOUBLE_LINE_BREAK = 0x04u,
|
||||
B_PARAGRAPH_MULTI_LINE_BREAK = 0x08u,
|
||||
} b_paragraph_format_flags;
|
||||
|
||||
typedef struct b_paragraph_format {
|
||||
b_paragraph_format_flags p_flags;
|
||||
|
||||
unsigned int p_left_margin;
|
||||
unsigned int p_right_margin;
|
||||
unsigned int p_line_length;
|
||||
} b_paragraph_format;
|
||||
|
||||
typedef enum b_print_format {
|
||||
B_PRINT_NORMAL = 0,
|
||||
B_PRINT_I,
|
||||
B_PRINT_WARN,
|
||||
B_PRINT_ERR,
|
||||
} b_print_format;
|
||||
|
||||
BLUE_API b_status b_print(b_print_format format, const char *str, ...);
|
||||
BLUE_API b_status b_print_paragraph(
|
||||
const char *str, struct b_tty *tty, b_paragraph_format *format);
|
||||
|
||||
BLUE_API int b_putc(char c);
|
||||
BLUE_API int b_puts(const char* s);
|
||||
BLUE_API int b_printf(const char* format, ...);
|
||||
|
||||
#endif
|
||||
179
term/include/blue/term/tty.h
Normal file
179
term/include/blue/term/tty.h
Normal file
@@ -0,0 +1,179 @@
|
||||
#ifndef BLUELIB_TERM_TTY_H_
|
||||
#define BLUELIB_TERM_TTY_H_
|
||||
|
||||
#include <blue/core/status.h>
|
||||
#include <blue/core/misc.h>
|
||||
#include <stdint.h>
|
||||
#include <stdbool.h>
|
||||
|
||||
#define b_stdtty (z__b_tty_get_std())
|
||||
#define b_stderr (z__b_tty_get_err())
|
||||
|
||||
#define B_TTY_CTRL_KEY(c) ((c) | B_MOD_CTRL)
|
||||
|
||||
#define B_MAKE_VMODE(fg, bg, a) \
|
||||
{ \
|
||||
.v_fg = fg, .v_bg = bg, .v_attrib = (a) \
|
||||
}
|
||||
|
||||
#define B_MAKE_COLOUR_DEFAULT(v) \
|
||||
{ \
|
||||
.c_mode = TTY_COLOUR_NONE, \
|
||||
}
|
||||
|
||||
#define B_MAKE_COLOUR_16(v) \
|
||||
{ \
|
||||
.c_mode = TTY_COLOUR_16, .c_16 = {.value = (v) } \
|
||||
}
|
||||
|
||||
#define B_MAKE_COLOUR_256(v) \
|
||||
{ \
|
||||
.c_mode = TTY_COLOUR_256, .c_256 = {.value = (v) } \
|
||||
}
|
||||
|
||||
#define B_MAKE_COLOUR_TRUE(cr, cg, cb) \
|
||||
{ \
|
||||
.c_mode = TTY_COLOUR_TRUE, .c_true \
|
||||
= {.r = (cr), \
|
||||
.g = (cg), \
|
||||
.b = (cb) } \
|
||||
}
|
||||
|
||||
typedef struct b_tty b_tty;
|
||||
typedef uint32_t b_keycode;
|
||||
|
||||
/* codepoints U+F0000 to U+FFFFD are reserved areas in Unicode, free for
|
||||
* application use. store special keycodes here */
|
||||
enum {
|
||||
B_KEY_ARROW_LEFT = 0xF0000,
|
||||
B_KEY_ARROW_RIGHT,
|
||||
B_KEY_ARROW_UP,
|
||||
B_KEY_ARROW_DOWN,
|
||||
B_KEY_BACKSPACE,
|
||||
B_KEY_RETURN,
|
||||
|
||||
B_MOD_CTRL = 0x10000000,
|
||||
|
||||
B_KEY_EOF = 0xFFFFFFFF,
|
||||
};
|
||||
|
||||
typedef enum b_tty_colour16 {
|
||||
B_TTY_COLOUR16_BLACK = 0,
|
||||
B_TTY_COLOUR16_RED,
|
||||
B_TTY_COLOUR16_GREEN,
|
||||
B_TTY_COLOUR16_YELLOW,
|
||||
B_TTY_COLOUR16_BLUE,
|
||||
B_TTY_COLOUR16_MAGENTA,
|
||||
B_TTY_COLOUR16_CYAN,
|
||||
B_TTY_COLOUR16_WHITE,
|
||||
B_TTY_COLOUR16_BRIGHT_BLACK,
|
||||
B_TTY_COLOUR16_BRIGHT_RED,
|
||||
B_TTY_COLOUR16_BRIGHT_GREEN,
|
||||
B_TTY_COLOUR16_BRIGHT_YELLOW,
|
||||
B_TTY_COLOUR16_BRIGHT_BLUE,
|
||||
B_TTY_COLOUR16_BRIGHT_MAGENTA,
|
||||
B_TTY_COLOUR16_BRIGHT_CYAN,
|
||||
B_TTY_COLOUR16_BRIGHT_WHITE,
|
||||
} b_tty_colour16;
|
||||
|
||||
typedef enum b_tty_colour_mode {
|
||||
B_TTY_COLOUR_NONE = 0,
|
||||
B_TTY_COLOUR_16,
|
||||
B_TTY_COLOUR_256,
|
||||
B_TTY_COLOUR_TRUE,
|
||||
} b_tty_colour_mode;
|
||||
|
||||
typedef enum b_tty_position_base {
|
||||
B_TTY_POS_START,
|
||||
B_TTY_POS_CURSOR,
|
||||
} b_tty_position_base;
|
||||
|
||||
typedef enum b_tty_attrib {
|
||||
B_TTY_ATTRIB_NORMAL = 0x00u,
|
||||
B_TTY_ATTRIB_BOLD = 0x01u,
|
||||
B_TTY_ATTRIB_UNDERLINE = 0x02u,
|
||||
B_TTY_ATTRIB_ITALIC = 0x04u,
|
||||
B_TTY_ATTRIB_INVERT = 0x08u,
|
||||
} b_tty_attrib;
|
||||
|
||||
typedef enum b_tty_clear_mode {
|
||||
B_TTY_CLEAR_LINE = 0x01u,
|
||||
B_TTY_CLEAR_SCREEN = 0x02,
|
||||
|
||||
B_TTY_CLEAR_TO_CURSOR = 0x0100u,
|
||||
B_TTY_CLEAR_FROM_CURSOR = 0x0200u,
|
||||
B_TTY_CLEAR_ALL = 0x0400u,
|
||||
} b_tty_clear_mode;
|
||||
|
||||
typedef enum b_tty_mode {
|
||||
B_TTY_CANONICAL = 0x00u,
|
||||
B_TTY_RAW = 0x01u,
|
||||
} b_tty_mode;
|
||||
|
||||
typedef enum b_tty_print_flags {
|
||||
B_TTY_DISABLE_FORMATTING = 0x01u,
|
||||
B_TTY_DISABLE_INTERPOLATED_FORMATTING = 0x02u,
|
||||
} b_tty_print_flags;
|
||||
|
||||
typedef struct b_tty_colour {
|
||||
enum b_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;
|
||||
};
|
||||
} b_tty_colour;
|
||||
|
||||
typedef struct b_tty_vmode {
|
||||
enum b_tty_attrib v_attrib;
|
||||
struct b_tty_colour v_fg;
|
||||
struct b_tty_colour v_bg;
|
||||
} b_tty_vmode;
|
||||
|
||||
BLUE_API b_tty *z__b_tty_get_std(void);
|
||||
BLUE_API b_tty *z__b_tty_get_err(void);
|
||||
|
||||
static inline unsigned int b_keycode_get_key(b_keycode v)
|
||||
{
|
||||
return v & 0x0FFFFFFF;
|
||||
}
|
||||
|
||||
BLUE_API bool b_tty_is_interactive(const struct b_tty *tty);
|
||||
BLUE_API void b_tty_set_mode(struct b_tty *tty, enum b_tty_mode mode);
|
||||
BLUE_API void b_tty_set_vmode(struct b_tty *tty, const struct b_tty_vmode *vmode);
|
||||
BLUE_API void b_tty_reset_vmode(struct b_tty *tty);
|
||||
|
||||
BLUE_API enum b_status b_tty_get_dimensions(
|
||||
struct b_tty *tty, unsigned int *w, unsigned int *h);
|
||||
BLUE_API enum b_status b_tty_get_cursor_position(
|
||||
struct b_tty *tty, unsigned int *x, unsigned int *y);
|
||||
|
||||
BLUE_API b_keycode b_tty_read_key(struct b_tty *tty);
|
||||
|
||||
BLUE_API void b_tty_move_cursor_x(
|
||||
struct b_tty *tty, enum b_tty_position_base base, int pos);
|
||||
BLUE_API void b_tty_move_cursor_y(
|
||||
struct b_tty *tty, enum b_tty_position_base base, int pos);
|
||||
BLUE_API void b_tty_clear(struct b_tty *tty, enum b_tty_clear_mode mode);
|
||||
|
||||
BLUE_API int b_tty_putc(
|
||||
struct b_tty *tty, enum b_tty_print_flags flags, char c);
|
||||
BLUE_API int b_tty_puts(
|
||||
struct b_tty *tty, enum b_tty_print_flags flags, const char *s);
|
||||
BLUE_API int b_tty_printf(
|
||||
struct b_tty *tty, enum b_tty_print_flags flags, const char *s, ...);
|
||||
BLUE_API int b_tty_vprintf(
|
||||
struct b_tty *tty, enum b_tty_print_flags flags, const char *s, va_list args);
|
||||
|
||||
#endif
|
||||
Reference in New Issue
Block a user