frontend: line-ed: add missing tty function implementations for linux & darwin
This commit is contained in:
@@ -1,9 +1,11 @@
|
|||||||
|
#include "../../../line-ed/tty.h"
|
||||||
|
|
||||||
|
#include <stdio.h>
|
||||||
|
#include <stdlib.h>
|
||||||
|
#include <string.h>
|
||||||
#include <sys/ioctl.h>
|
#include <sys/ioctl.h>
|
||||||
#include <termios.h>
|
#include <termios.h>
|
||||||
#include <unistd.h>
|
#include <unistd.h>
|
||||||
#include <string.h>
|
|
||||||
#include <stdio.h>
|
|
||||||
#include "tty.h"
|
|
||||||
|
|
||||||
#define ANSI_BOLD_ON "1"
|
#define ANSI_BOLD_ON "1"
|
||||||
#define ANSI_BOLD_OFF "22"
|
#define ANSI_BOLD_OFF "22"
|
||||||
@@ -136,7 +138,8 @@ void s_tty_reset_vmode(struct s_tty *tty)
|
|||||||
tty->t_vmode.v_attrib = TTY_ATTRIB_NORMAL;
|
tty->t_vmode.v_attrib = TTY_ATTRIB_NORMAL;
|
||||||
}
|
}
|
||||||
|
|
||||||
static int compare_colour(const struct s_tty_colour *a, const struct s_tty_colour *b)
|
static int compare_colour(
|
||||||
|
const struct s_tty_colour *a, const struct s_tty_colour *b)
|
||||||
{
|
{
|
||||||
if (a->c_mode != b->c_mode) {
|
if (a->c_mode != b->c_mode) {
|
||||||
return -1;
|
return -1;
|
||||||
@@ -200,44 +203,47 @@ static void put_ansi_attrib(struct s_tty *tty, const char *s)
|
|||||||
tty->t_mcount++;
|
tty->t_mcount++;
|
||||||
}
|
}
|
||||||
|
|
||||||
static void set_attrib(struct s_tty *tty, enum s_tty_attrib old, enum s_tty_attrib new)
|
static void set_attrib(
|
||||||
|
struct s_tty *tty, enum s_tty_attrib old, enum s_tty_attrib new)
|
||||||
{
|
{
|
||||||
if (old == new) {
|
if (old == new) {
|
||||||
return;
|
return;
|
||||||
}
|
}
|
||||||
|
|
||||||
/* Bold ON */
|
/* Bold ON */
|
||||||
if (!(old & TTY_ATTRIB_BOLD) && new & TTY_ATTRIB_BOLD) {
|
if (!(old & TTY_ATTRIB_BOLD) && new &TTY_ATTRIB_BOLD) {
|
||||||
put_ansi_attrib(tty, ANSI_BOLD_ON);
|
put_ansi_attrib(tty, ANSI_BOLD_ON);
|
||||||
}
|
}
|
||||||
|
|
||||||
/* Bold OFF */
|
/* Bold OFF */
|
||||||
if (old & TTY_ATTRIB_BOLD && !(new & TTY_ATTRIB_BOLD)) {
|
if (old & TTY_ATTRIB_BOLD && !(new &TTY_ATTRIB_BOLD)) {
|
||||||
put_ansi_attrib(tty, ANSI_BOLD_OFF);
|
put_ansi_attrib(tty, ANSI_BOLD_OFF);
|
||||||
}
|
}
|
||||||
|
|
||||||
/* Underline ON */
|
/* Underline ON */
|
||||||
if (!(old & TTY_ATTRIB_UNDERLINE) && new & TTY_ATTRIB_UNDERLINE) {
|
if (!(old & TTY_ATTRIB_UNDERLINE) && new &TTY_ATTRIB_UNDERLINE) {
|
||||||
put_ansi_attrib(tty, ANSI_UNDERLINE_ON);
|
put_ansi_attrib(tty, ANSI_UNDERLINE_ON);
|
||||||
}
|
}
|
||||||
|
|
||||||
/* Underline OFF */
|
/* Underline OFF */
|
||||||
if (old & TTY_ATTRIB_UNDERLINE && !(new & TTY_ATTRIB_UNDERLINE)) {
|
if (old & TTY_ATTRIB_UNDERLINE && !(new &TTY_ATTRIB_UNDERLINE)) {
|
||||||
put_ansi_attrib(tty, ANSI_UNDERLINE_OFF);
|
put_ansi_attrib(tty, ANSI_UNDERLINE_OFF);
|
||||||
}
|
}
|
||||||
|
|
||||||
/* Italic ON */
|
/* Italic ON */
|
||||||
if (!(old & TTY_ATTRIB_ITALIC) && new & TTY_ATTRIB_ITALIC) {
|
if (!(old & TTY_ATTRIB_ITALIC) && new &TTY_ATTRIB_ITALIC) {
|
||||||
put_ansi_attrib(tty, ANSI_ITALIC_ON);
|
put_ansi_attrib(tty, ANSI_ITALIC_ON);
|
||||||
}
|
}
|
||||||
|
|
||||||
/* Italic OFF */
|
/* Italic OFF */
|
||||||
if (old & TTY_ATTRIB_ITALIC && !(new & TTY_ATTRIB_ITALIC)) {
|
if (old & TTY_ATTRIB_ITALIC && !(new &TTY_ATTRIB_ITALIC)) {
|
||||||
put_ansi_attrib(tty, ANSI_ITALIC_OFF);
|
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)
|
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) {
|
if (compare_colour(old, new) == 0) {
|
||||||
return;
|
return;
|
||||||
@@ -271,7 +277,9 @@ static void set_fg(struct s_tty *tty, const struct s_tty_colour *old, const stru
|
|||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
static void set_bg(struct s_tty *tty, const struct s_tty_colour *old, const struct s_tty_colour *new)
|
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) {
|
if (compare_colour(old, new) == 0) {
|
||||||
return;
|
return;
|
||||||
@@ -382,3 +390,72 @@ s_keycode s_tty_read_key(struct s_tty *tty)
|
|||||||
|
|
||||||
return c;
|
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();
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|||||||
@@ -1,9 +1,11 @@
|
|||||||
|
#include "../../../line-ed/tty.h"
|
||||||
|
|
||||||
|
#include <stdio.h>
|
||||||
|
#include <stdlib.h>
|
||||||
|
#include <string.h>
|
||||||
#include <sys/ioctl.h>
|
#include <sys/ioctl.h>
|
||||||
#include <termios.h>
|
#include <termios.h>
|
||||||
#include <unistd.h>
|
#include <unistd.h>
|
||||||
#include <string.h>
|
|
||||||
#include <stdio.h>
|
|
||||||
#include "tty.h"
|
|
||||||
|
|
||||||
#define ANSI_BOLD_ON "1"
|
#define ANSI_BOLD_ON "1"
|
||||||
#define ANSI_BOLD_OFF "22"
|
#define ANSI_BOLD_OFF "22"
|
||||||
@@ -136,7 +138,8 @@ void s_tty_reset_vmode(struct s_tty *tty)
|
|||||||
tty->t_vmode.v_attrib = TTY_ATTRIB_NORMAL;
|
tty->t_vmode.v_attrib = TTY_ATTRIB_NORMAL;
|
||||||
}
|
}
|
||||||
|
|
||||||
static int compare_colour(const struct s_tty_colour *a, const struct s_tty_colour *b)
|
static int compare_colour(
|
||||||
|
const struct s_tty_colour *a, const struct s_tty_colour *b)
|
||||||
{
|
{
|
||||||
if (a->c_mode != b->c_mode) {
|
if (a->c_mode != b->c_mode) {
|
||||||
return -1;
|
return -1;
|
||||||
@@ -200,44 +203,47 @@ static void put_ansi_attrib(struct s_tty *tty, const char *s)
|
|||||||
tty->t_mcount++;
|
tty->t_mcount++;
|
||||||
}
|
}
|
||||||
|
|
||||||
static void set_attrib(struct s_tty *tty, enum s_tty_attrib old, enum s_tty_attrib new)
|
static void set_attrib(
|
||||||
|
struct s_tty *tty, enum s_tty_attrib old, enum s_tty_attrib new)
|
||||||
{
|
{
|
||||||
if (old == new) {
|
if (old == new) {
|
||||||
return;
|
return;
|
||||||
}
|
}
|
||||||
|
|
||||||
/* Bold ON */
|
/* Bold ON */
|
||||||
if (!(old & TTY_ATTRIB_BOLD) && new & TTY_ATTRIB_BOLD) {
|
if (!(old & TTY_ATTRIB_BOLD) && new &TTY_ATTRIB_BOLD) {
|
||||||
put_ansi_attrib(tty, ANSI_BOLD_ON);
|
put_ansi_attrib(tty, ANSI_BOLD_ON);
|
||||||
}
|
}
|
||||||
|
|
||||||
/* Bold OFF */
|
/* Bold OFF */
|
||||||
if (old & TTY_ATTRIB_BOLD && !(new & TTY_ATTRIB_BOLD)) {
|
if (old & TTY_ATTRIB_BOLD && !(new &TTY_ATTRIB_BOLD)) {
|
||||||
put_ansi_attrib(tty, ANSI_BOLD_OFF);
|
put_ansi_attrib(tty, ANSI_BOLD_OFF);
|
||||||
}
|
}
|
||||||
|
|
||||||
/* Underline ON */
|
/* Underline ON */
|
||||||
if (!(old & TTY_ATTRIB_UNDERLINE) && new & TTY_ATTRIB_UNDERLINE) {
|
if (!(old & TTY_ATTRIB_UNDERLINE) && new &TTY_ATTRIB_UNDERLINE) {
|
||||||
put_ansi_attrib(tty, ANSI_UNDERLINE_ON);
|
put_ansi_attrib(tty, ANSI_UNDERLINE_ON);
|
||||||
}
|
}
|
||||||
|
|
||||||
/* Underline OFF */
|
/* Underline OFF */
|
||||||
if (old & TTY_ATTRIB_UNDERLINE && !(new & TTY_ATTRIB_UNDERLINE)) {
|
if (old & TTY_ATTRIB_UNDERLINE && !(new &TTY_ATTRIB_UNDERLINE)) {
|
||||||
put_ansi_attrib(tty, ANSI_UNDERLINE_OFF);
|
put_ansi_attrib(tty, ANSI_UNDERLINE_OFF);
|
||||||
}
|
}
|
||||||
|
|
||||||
/* Italic ON */
|
/* Italic ON */
|
||||||
if (!(old & TTY_ATTRIB_ITALIC) && new & TTY_ATTRIB_ITALIC) {
|
if (!(old & TTY_ATTRIB_ITALIC) && new &TTY_ATTRIB_ITALIC) {
|
||||||
put_ansi_attrib(tty, ANSI_ITALIC_ON);
|
put_ansi_attrib(tty, ANSI_ITALIC_ON);
|
||||||
}
|
}
|
||||||
|
|
||||||
/* Italic OFF */
|
/* Italic OFF */
|
||||||
if (old & TTY_ATTRIB_ITALIC && !(new & TTY_ATTRIB_ITALIC)) {
|
if (old & TTY_ATTRIB_ITALIC && !(new &TTY_ATTRIB_ITALIC)) {
|
||||||
put_ansi_attrib(tty, ANSI_ITALIC_OFF);
|
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)
|
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) {
|
if (compare_colour(old, new) == 0) {
|
||||||
return;
|
return;
|
||||||
@@ -271,7 +277,9 @@ static void set_fg(struct s_tty *tty, const struct s_tty_colour *old, const stru
|
|||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
static void set_bg(struct s_tty *tty, const struct s_tty_colour *old, const struct s_tty_colour *new)
|
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) {
|
if (compare_colour(old, new) == 0) {
|
||||||
return;
|
return;
|
||||||
@@ -382,3 +390,72 @@ s_keycode s_tty_read_key(struct s_tty *tty)
|
|||||||
|
|
||||||
return c;
|
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();
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|||||||
Reference in New Issue
Block a user