From: Mike Lowis Date: Mon, 4 Jan 2016 13:25:21 +0000 (+0000) Subject: Implemented first-pass at horizontal scrolling with cursor X-Git-Url: https://git.mdlowis.com/?a=commitdiff_plain;h=b27eafb9f707b27cc42d9d3c0db1cfa92f8472cb;p=projs%2Ftide.git Implemented first-pass at horizontal scrolling with cursor --- diff --git a/source/main.c b/source/main.c index 8f0ebfa..d9ba650 100644 --- a/source/main.c +++ b/source/main.c @@ -26,12 +26,18 @@ typedef struct { int y; } Pos; +typedef struct { + Line* line; + int offset; +} FilePos; + /* Globals *****************************************************************************/ static bool ScreenDirty = true; -static File Curr_File = { .name = NULL, .first = NULL, .last = NULL }; +static File Curr_File = { .name = NULL, .start = NULL, .first = NULL, .last = NULL }; static Pos Curr = { .x = 0, .y = 0 }; static Pos Max = { .x = 0, .y = 0 }; +static FilePos Loc = { .line = NULL, .offset = 0 }; /* Declarations *****************************************************************************/ @@ -88,10 +94,12 @@ static void load(char* fname) while (!feof(file)) { Line* line = (Line*)ecalloc(1,sizeof(Line)); line->text = efreadline(file); + line->length = strlen(line->text); if (Curr_File.first == NULL) { Curr_File.start = line; Curr_File.first = line; Curr_File.last = line; + Loc.line = line; } else { Curr_File.last->next = line; line->prev = Curr_File.last; @@ -106,14 +114,21 @@ static void input(int ch) switch (ch) { case KEY_LEFT: case 'h': - Curr.x = (Curr.x-1 < 0) ? 0 : Curr.x-1; + Curr.x--; + if (Curr.x < 0) { + Curr.x = 0; + Loc.offset--; + if (Loc.offset < 0) + Loc.offset = 0; + ScreenDirty = true; + } break; case KEY_DOWN: case 'j': Curr.y++; - if (Curr.y > Max.y) { - Curr.y = Max.y; + if (Curr.y >= Max.y) { + Curr.y = Max.y-1; if (Curr_File.start->next) { Curr_File.start = Curr_File.start->next; ScreenDirty = true; @@ -135,7 +150,14 @@ static void input(int ch) case KEY_RIGHT: case 'l': - Curr.x = (Curr.x+1 > Max.x) ? Max.x : Curr.x+1; + Curr.x++; + if (Curr.x >= Max.x) { + Curr.x = Max.x-1; + Loc.offset++; + if (Loc.offset >= Loc.line->length-1) + Loc.offset = Loc.line->length-2; + ScreenDirty = true; + } break; } } @@ -151,7 +173,9 @@ static void edit(void) clear(); Line* line = Curr_File.start; for (int i = 0; (i < Max.y) && line; i++, line = line->next) { - mvprintw(i, 0, "%s", line->text); + if (line->length > Loc.offset) { + mvprintw(i, 0, "%s", &(line->text[Loc.offset])); + } } refresh(); ScreenDirty = false;