Commit 4b9da9a6 authored by mbionchi's avatar mbionchi

fix minor bug, add a primitive log facility, fix scroll over telnet

parent 8706a652
bin_PROGRAMS = telnetsite
telnetsite_SOURCES = site.c data.c data.h scroll.c scroll.h render.c render.h
telnetsite_SOURCES = site.c data.c data.h scroll.c scroll.h render.c render.h log.c log.h
telnetsite_LDADD = ${ncurses_LIBS}
AM_CFLAGS = -Isrc/
/*
* This file is part of telnet-site.
*
* telnet-site is free software: you can redistribute it and/or modify
* it under the terms of the GNU General Public License as published by
* the Free Software Foundation, either version 3 of the License, or
* (at your option) any later version.
*
* telnet-site is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
* GNU General Public License for more details.
*
* You should have received a copy of the GNU General Public License
* along with telnet-site. If not, see <https://www.gnu.org/licenses/>.
*/
#include "data.h"
#include <stdlib.h>
#include <stddef.h>
......@@ -89,9 +106,11 @@ struct section **read_sections(DIR *dir, char *dirname, int follow_links, size_t
base = realloc(base, *nmemb*sizeof(struct section*));
}
base[i] = malloc(sizeof(struct section));
char *title = index(dirent->d_name, '-')+1;
char *title = index(dirent->d_name, '-');
if (!title) {
title = dirent->d_name;
} else {
title++;
}
size_t title_len = strlen(title);
base[i]->title = strcpy(malloc((title_len+1)*sizeof(char)), title);
......
......@@ -35,6 +35,7 @@ struct section {
void print_lines(struct line *lines);
void dump_sections(struct section **sections, size_t n_sections);
struct line *read_lines(FILE *fp);
struct frame *read_frames(FILE *fp);
struct section **read_sections(DIR *dir, char *dirname, int follow_links, size_t *nmemb);
struct line *flow_content(struct line *lines, int width);
void free_lines(struct line *lines);
......
......@@ -15,6 +15,7 @@
* along with telnet-site. If not, see <https://www.gnu.org/licenses/>.
*/
#include "log.h"
#include "render.h"
#include "data.h"
#include <string.h>
......@@ -23,26 +24,34 @@
void render_index(WINDOW *window, struct section **sections, size_t n_sections) {
int maxx, maxy;
getmaxyx(window, maxy, maxx);
scrollok(window, 0);
for (size_t i=0; i<n_sections && i<maxy-1; i++) {
size_t title_len = strlen(sections[i]->title);
mvwprintw(window, i+1, maxx-title_len, "%s", sections[i]->title);
}
scrollok(window, 1);
}
void render_separator(WINDOW *window, int selected_index) {
int maxx, maxy;
getmaxyx(window, maxy, maxx);
scrollok(window, 0);
for (int i=0; i<maxy; i++) {
mvwprintw(window, i, 0, "%s", i==selected_index?SEPARATOR_SELECTED:SEPARATOR_REGULAR);
}
scrollok(window, 1);
}
void render_content(WINDOW *window, struct line *lines) {
int maxx, maxy;
getmaxyx(window, maxy, maxx);
scrollok(window, 0);
int cursor_y = 0;
struct line *iter = lines;
while (iter) {
while (iter && cursor_y < maxy) {
mvwprintw(window, cursor_y, 0, "%s", iter->line);
cursor_y++;
iter = iter->next;
}
scrollok(window, 1);
}
......@@ -15,6 +15,7 @@
* along with telnet-site. If not, see <https://www.gnu.org/licenses/>.
*/
#include "log.h"
#include "scroll.h"
#include "render.h"
#include "data.h"
......
......@@ -25,6 +25,7 @@
#include <dirent.h>
#include <string.h>
#include <sys/stat.h>
#include <signal.h>
size_t get_index_width(struct section **sections, size_t n_sections) {
size_t max_len = 0;
......@@ -38,6 +39,9 @@ size_t get_index_width(struct section **sections, size_t n_sections) {
return max_len;
}
void winch_handler(int signo) {
}
/*
* NOTE:
* scrollok(window, 0) needs to be called every time before
......@@ -51,6 +55,7 @@ size_t get_index_width(struct section **sections, size_t n_sections) {
* - error handling everywhere
*/
int main(int argc, char **argv) {
signal(SIGWINCH, winch_handler);
int selected_index = 0;
int scroll = 0;
size_t n_sections;
......@@ -89,12 +94,12 @@ int main(int argc, char **argv) {
for (int i=0; i<content_rows-1 && content_bot->next != NULL; i++) {
content_bot = content_bot->next;
}
render_index(index_win, sections, n_sections);
render_separator(separator_win, selected_index+1);
render_content(content_win, content_head);
make_scrollable(index_win);
make_scrollable(separator_win);
make_scrollable(content_win);
render_index(index_win, sections, n_sections);
render_separator(separator_win, selected_index+1);
render_content(content_win, content_head);
refresh();
wrefresh(index_win);
wrefresh(separator_win);
......
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment