Skip to content

Commit

Permalink
Less magic, more printf
Browse files Browse the repository at this point in the history
  • Loading branch information
hifi committed Jul 26, 2016
1 parent 17e8edc commit 79ca172
Show file tree
Hide file tree
Showing 3 changed files with 25 additions and 15 deletions.
13 changes: 13 additions & 0 deletions lib/interestingtimes.c
Original file line number Diff line number Diff line change
Expand Up @@ -264,6 +264,7 @@ struct screen *scr_init(void)
return scr;
}

// TODO: make ANSI escape and UTF-8 aware
static inline char *scr_linediff(char *from, char *to, size_t len, unsigned lnum)
{
unsigned i, from_len, to_len;
Expand Down Expand Up @@ -337,3 +338,15 @@ void scr_update(struct screen *scr)

fflush(stdout);
}

// TODO: make ANSI escape and UTF-8 aware (x offset & total visible length)
void scr_printf(struct screen *scr, unsigned x, unsigned y, const char *fmt, ...)
{
va_list va;

if (x >= scr->w || y >= scr->h) return;

va_start(va, fmt);
vsnprintf(SCR_PTR(scr) + (scr->l * y), scr->l - x, fmt, va);
va_end(va);
}
1 change: 1 addition & 0 deletions lib/lib.h
Original file line number Diff line number Diff line change
Expand Up @@ -269,6 +269,7 @@ struct screen {
#define SCR_PTR(scr) (scr->buf[0])
struct screen *scr_init(void);
void scr_update(struct screen *scr);
void scr_printf(struct screen *scr, unsigned x, unsigned y, const char *fmt, ...);

// net.c
int xsocket(int domain, int type, int protocol);
Expand Down
26 changes: 11 additions & 15 deletions toys/pending/less.c
Original file line number Diff line number Diff line change
Expand Up @@ -28,27 +28,23 @@ GLOBALS(

static void redraw(void)
{
unsigned i;

for (i = 0; i < TT.scr->h - 1; i++) {
char *row = SCR_PTR(TT.scr) + (TT.scr->l * i);
int len = (int)TT.ls->idx[i + TT.y_off].len - TT.x_off;
if (len > TT.scr->l) len = TT.scr->l;

if (i + TT.y_off < TT.ls->len) {
if (TT.x_off < TT.ls->idx[i + TT.y_off].len) {
memcpy(row, (char *)TT.ls->idx[i + TT.y_off].ptr + TT.x_off, len);
row[len] = 0;
unsigned y;

for (y = 0; y < TT.scr->h - 1; y++) {
if (y + TT.y_off < TT.ls->len) {
if (TT.x_off < TT.ls->idx[y + TT.y_off].len) {
scr_printf(TT.scr, 0, y, "%.*s",
TT.ls->idx[y + TT.y_off].len - TT.x_off,
(char *)TT.ls->idx[y + TT.y_off].ptr + TT.x_off);
} else {
*row = 0;
scr_printf(TT.scr, 0, y, "");
}
} else {
memcpy(row, "~\0", 2);
scr_printf(TT.scr, 0, y, "~");
}
}

strncpy(SCR_PTR(TT.scr) + (TT.scr->l * (TT.scr->h - 1)), toybuf, TT.scr->l);

scr_printf(TT.scr, 0, TT.scr->h - 1, toybuf);
scr_update(TT.scr);
}

Expand Down

0 comments on commit 79ca172

Please sign in to comment.