Handling line overflow

This commit is contained in:
Anonymus Raccoon
2020-05-19 23:44:01 +02:00
parent 8670ef9542
commit 061833f9be
4 changed files with 63 additions and 23 deletions
+1
View File
@@ -39,6 +39,7 @@ void my_endwin(my_window *window);
void my_move(my_window *window, int y, int x);
void my_getcuryx(int *y, int *x);
void my_getmaxyx(int *y, int *x);
int my_getch(void);
void my_addstr(my_window *window, const char *str);
+17 -14
View File
@@ -10,12 +10,20 @@
#include <stdio.h>
#include <unistd.h>
#include <sys/ioctl.h>
#include <signal.h>
my_window *stdwin;
void on_resize(int sig, siginfo_t *info, void *context)
{
my_getmaxyx(&stdwin->h, &stdwin->w);
}
my_window *my_initwin(void)
{
my_window *window = calloc(1, sizeof(*window));
struct termios term;
struct winsize size;
struct sigaction sa;
if (!window)
return (NULL);
@@ -24,23 +32,16 @@ my_window *my_initwin(void)
term.c_iflag &= ~(IXON | ICRNL);
term.c_lflag &= ~(ECHO | ICANON | ISIG | IEXTEN);
tcsetattr(0, TCSANOW, &term);
ioctl(STDOUT_FILENO, TIOCGWINSZ, &size);
window->h = size.ws_row;
window->w = size.ws_col;
sa.sa_sigaction = &on_resize;
sigemptyset(&sa.sa_mask);
sa.sa_flags = SA_RESTART | SA_SIGINFO;
sigaction(SIGWINCH, &sa, NULL);
my_getmaxyx(&window->h, &window->w);
my_getcuryx(&window->y, &window->x);
stdwin = window;
return (window);
}
void my_getcuryx(int *y, int *x)
{
my_refresh();
printf("\x1B[6n");
fflush(stdout);
scanf("\x1B[%d;%dR", y, x);
*y -= 1;
*x -= 1;
}
void my_endwin(my_window *window)
{
if (!window)
@@ -55,6 +56,8 @@ void my_move(my_window *window, int y, int x)
y = window->y;
if (x == NO_MOVE)
x = window->x;
window->x = x;
window->y = y;
printf("\x1B[%d;%dH", y + 1, x + 1);
}
+27 -3
View File
@@ -8,21 +8,45 @@
#include "my_ncurses.h"
#include <stdio.h>
#include <ncurses.h>
#include <unistd.h>
#include <sys/ioctl.h>
void my_addstr(my_window *window, const char *str)
{
int tmp;
for (int i = 0; str[i]; i++) {
if (str[i] == '\n' || window->x > window->w) {
if (str[i] == '\n') {
window->y++;
window->x = 0;
}
if (str[i] == '\t') {
} else if (str[i] == '\t') {
tmp = TABSIZE - window->x % TABSIZE;
window->x += tmp == 0 ? TABSIZE : tmp;
} else
window->x++;
if (window->x >= window->w) {
window->y++;
window->x = 0;
}
}
printf("%s", str);
}
void my_getmaxyx(int *y, int *x)
{
struct winsize size;
ioctl(STDOUT_FILENO, TIOCGWINSZ, &size);
*y = size.ws_row;
*x = size.ws_col;
}
void my_getcuryx(int *y, int *x)
{
my_refresh();
printf("\x1B[6n");
fflush(stdout);
scanf("\x1B[%d;%dR", y, x);
*y -= 1;
*x -= 1;
}
+18 -6
View File
@@ -27,7 +27,7 @@ int process_key(int key, buffer_t *buffer, env_t *env)
return (self_insert_command(key, buffer, env));
}
int buffer_get_display_pos(buffer_t *buffer)
int buf_getx(buffer_t *buffer, env_t *env)
{
int pos = buffer->startx;
int tmp;
@@ -38,10 +38,26 @@ int buffer_get_display_pos(buffer_t *buffer)
pos += tmp == 0 ? TABSIZE : tmp;
} else
pos++;
if (pos >= env->window->w)
pos = 0;
}
return pos;
}
void shell_refresh(buffer_t *buffer, env_t *env)
{
static int oldbuffer_pos = 0;
int y = env->window->y - (oldbuffer_pos + buffer->startx) / env->window->w;
int newy = y + (buffer->pos + buffer->startx) / env->window->w;
if (buffer->buffer)
my_mvaddstr(env->window, y, buffer->startx, buffer->buffer);
my_clrtoeol();
my_move(env->window, newy, buf_getx(buffer, env));
my_refresh();
oldbuffer_pos = buffer->pos;
}
void start_shell(env_t *env)
{
buffer_t buffer = {.size = 0, .buffer = NULL, .pos = 0, .startx = 0};
@@ -53,11 +69,7 @@ void start_shell(env_t *env)
}
do {
if (env->window) {
if (buffer.buffer)
my_mvaddstr(env->window, NO_MOVE, buffer.startx, buffer.buffer);
my_clrtoeol();
my_move(env->window, NO_MOVE, buffer_get_display_pos(&buffer));
my_refresh();
shell_refresh(&buffer, env);
key = my_getch();
} else
key = fgetc(stdin);