问题描述
我正在尝试使用 Termcap
功能实现一个迷你版的 bash,现在我正在尝试读取用户的输入并在终端标准输出中重新输入。
一切正常,但是在尝试调整终端窗口大小时会出现问题,如下面的 gif 所示,当我写一个 SINGLE 行然后缩小窗口时,以及再次展开它,输出会按原样包装好。
但是当我的文本越过第一个终端行(不按 Enter 键)时,如果我执行与以前相同的步骤,则输出换行与我想要的不同。我需要的是,在缩小窗口时,第一行的最后一个字符必须与第二行字符连接,反之亦然,当我扩展窗口时,第二行的第一个字符与第一行字符连接。
有没有办法让输出的行与 bash 完全相同?(将行相互连接)
到目前为止,这是我的代码,当您想编译它时使用 -ltermcap
标志,并提前致谢。
#include <ctype.h>
#include <termcap.h>
#include <termios.h>
# include <sys/ioctl.h>
# include <signal.h>
#include <unistd.h>
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
int g_screen_width = 0;
int g_cursor_colm = 0;
int g_printed = 0;
int ft_putchar(int c)
{
int len = write(1,&c,1);
return (len);
}
int ft_isprint(int c)
{
return (c >= 32 && c < 127);
}
void update_cursor_position()
{
g_cursor_colm = g_printed % g_screen_width;
}
void update_screen_width()
{
struct winsize w;
ioctl(STDIN_FILENO,TIocgWINSZ,&w);
g_screen_width = w.ws_col;
}
void sigwinch_handler(int signo)
{
if (signo == SIGWINCH)
{
update_screen_width();
update_cursor_position();
}
}
void move_cursor_to_colum(int col)
{
char *ch_cap;
ch_cap = tgetstr("ch",NULL);
tputs(tgoto(ch_cap,col),1,ft_putchar);
}
void move_cursor_down_vertically()
{
char *do_cap;
do_cap = tgetstr("do",NULL);
tputs(do_cap,ft_putchar);
}
void move_cursor_to_next_line()
{
move_cursor_to_colum(0);
move_cursor_down_vertically();
}
void enable_raw_mode()
{
struct termios raw;
tcgetattr(STDIN_FILENO,&raw);
raw.c_lflag &= ~(ECHO | ICANON);
tcsetattr(STDIN_FILENO,TCSAFLUSH,&raw);
}
void disable_raw_mode(struct termios old_termios_state)
{
tcsetattr(STDIN_FILENO,&old_termios_state);
}
int main()
{
struct termios original_termios_state;
char *term_type = getenv("TERM");
char c;
char *line;
tgetent(NULL,term_type);
tcgetattr(STDIN_FILENO,&original_termios_state);
enable_raw_mode();
update_screen_width();
signal(SIGWINCH,sigwinch_handler);
while (read(STDIN_FILENO,1))
{
if (ft_isprint(c))
{
ft_putchar(c);
g_printed++;
g_cursor_colm++;
if (g_cursor_colm == g_screen_width)
{
g_cursor_colm = 0;
move_cursor_to_next_line();
}
}
else if (c == 27) // ESC
{
disable_raw_mode(original_termios_state);
exit(0);
}
}
disable_raw_mode(original_termios_state);
return (0);
}
解决方法
您的终端会记住实际跳行的位置(您在 move_cursor_down_vertically()
中使用 cursor_down 明确执行的跳行),而不是终端由于宽度有限而自行换行。
所以:
- 要么根本不移动(向下)光标,而是仅对一个虚拟文本行进行所有处理,让终端根据其宽度包装该行(这可能是最简单的解决方案);
- 或者当您收到 SIGWINCH 信号时,您自己在
sigwinch_handler()
中重绘屏幕。