Subj : Custom Scrollabe Text File Viewer
To : Digital Man
From : Codefenix
Date : Wed Nov 06 2024 06:05 pm
Re: Custom Scrollabe Text File Viewer
By: Codefenix to Digital Man on Wed Nov 06 2024 09:38 am
DM>> script load the file into memory in chunks (how printfile() works by
DM>> default).
Co> That's interesting. Like just use the frame object as a kind of viewport
Co> for the lines I want to display, and use the up/down/etc to control which
Co> range of lines come into view. Seems very doable. Thanks for the thought.
Ok, this version does exactly that. It still reads the entire file into memory,
but it only writes a screen's worth of text to the frame at a time. Now it's much faster and has handled everything I've thrown at it. The largest file I tried was 1,599 KB, and it handled it with ease. I don't personally plan on displaying files higher than the megabyte teritory, at least not currently. I also ditched the scrollbar in favor of a simple percentage indicator in the bottom right. Other than that, it works basically like the one I demo'd earlier.
If I keep working on this, I might try to add a CTRL+F option for finding text, but probably not much else. For now it suits my needs just fine.
function init_display() {
const w = console.screen_columns;
const h = console.screen_rows;
const f = { top: new Frame(1, 1, w, h, BG_BLACK|LIGHTGRAY) };
f.topbar = new Frame(1, 1, w, 1, BG_LIGHTGRAY|BLACK, f.top);
f.output = new Frame(1, 2, w, h - 2, BG_BLACK|LIGHTGRAY, f.top);
f.bottombar = new Frame(1, h, w, 1, BG_LIGHTGRAY|BLACK, f.top);
f.output.word_wrap = true;
f.bottombar.putmsg(OPTIONS);
f.top.open();
return f;
}
function get_lines_for_screen (lines, start, end) {
var viewport = [];
for (var i = start; i < end; i++) {
viewport.push(lines[i]);
}
return "\n" + viewport.join("\n") + "\n";
}
function viewfile(file_name, file_title, start_at_top) {
var title = file_title ? file_title : file_getname(file_name);
var exit_viewer = false;
var key_pressed;
var line_index;
var file_lines = [];
var max_lines;
var move_viewport = false;
const frames = init_display();