* Copyright 2010, Ingo Weinhold, ingo_weinhold@gmx.de.
* Distributed under the terms of the MIT License.
*/
#include "pager.h"
#include <ctype.h>
#include <string.h>
#include <algorithm>
#include <boot/platform/generic/text_console.h>
PagerTextSource::~PagerTextSource()
{
}
static size_t
next_line(const PagerTextSource& textSource, size_t width, size_t offset,
char* buffer, size_t bufferSize)
{
size_t bytesRead = textSource.Read(offset, buffer, bufferSize - 1);
if (bytesRead == 0)
return 0;
buffer[bytesRead] = '\0';
for (size_t i = 0; i < bytesRead; i++) {
if (buffer[i] == '\0')
buffer[i] = ' ';
}
if (const char* lineEnd = strchr(buffer, '\n'))
bytesRead = lineEnd - buffer;
if (bytesRead > (size_t)width)
bytesRead = width;
for (size_t i = 0; i < bytesRead; i++) {
if (!isprint(buffer[i]))
buffer[i] = '.';
}
bool lineBreak = buffer[bytesRead] == '\n';
buffer[bytesRead] = '\0';
return bytesRead + (lineBreak ? 1 : 0);
}
static int32
count_lines(const PagerTextSource& textSource, size_t width, char* buffer,
size_t bufferSize)
{
int32 lineCount = 0;
size_t offset = 0;
while (true) {
size_t bytesRead = next_line(textSource, width, offset, buffer,
bufferSize);
if (bytesRead == 0)
break;
offset += bytesRead;
lineCount++;
}
return lineCount;
}
static size_t
offset_of_line(const PagerTextSource& textSource, size_t width, char* buffer,
size_t bufferSize, int32 line)
{
int32 lineCount = 0;
size_t offset = 0;
while (true) {
if (line == lineCount)
return offset;
size_t bytesRead = next_line(textSource, width, offset, buffer,
bufferSize);
if (bytesRead == 0)
break;
offset += bytesRead;
lineCount++;
}
return offset;
}
void
pager(const PagerTextSource& textSource)
{
console_set_cursor(0, 0);
int32 width = console_width();
int32 height = console_height();
char lineBuffer[256];
int32 lineCount = count_lines(textSource, width, lineBuffer,
sizeof(lineBuffer));
int32 topLine = 0;
bool quit = false;
while (!quit) {
size_t offset = offset_of_line(textSource, width, lineBuffer,
sizeof(lineBuffer), topLine);
console_clear_screen();
int32 screenLine = 0;
while (screenLine + 1 < height) {
size_t bytesRead = next_line(textSource, width, offset, lineBuffer,
sizeof(lineBuffer));
if (bytesRead == 0)
break;
console_set_cursor(0, screenLine);
puts(lineBuffer);
offset += bytesRead;
screenLine++;
}
console_set_cursor(0, height - 1);
console_set_color(BLACK, GRAY);
int32 bottomLine = std::min(topLine + height - 2, lineCount - 1);
printf("%" B_PRId32 " - %" B_PRId32 " %" B_PRId32 "%%",
topLine, bottomLine, (int32)((bottomLine + 1) * 100 / lineCount));
console_set_color(WHITE, BLACK);
int32 previousTopLine = topLine;
while (!quit && topLine == previousTopLine) {
switch (console_wait_for_key()) {
case TEXT_CONSOLE_KEY_ESCAPE:
case 'q':
case 'Q':
quit = true;
break;
case TEXT_CONSOLE_KEY_DOWN:
case TEXT_CONSOLE_KEY_RETURN:
case 'j':
case 'J':
topLine++;
break;
case TEXT_CONSOLE_KEY_UP:
case 'k':
case 'K':
topLine--;
break;
case TEXT_CONSOLE_KEY_PAGE_UP:
topLine -= height - 1;
break;
case TEXT_CONSOLE_KEY_PAGE_DOWN:
topLine += height - 1;
break;
case TEXT_CONSOLE_KEY_HOME:
topLine = 0;
break;
case TEXT_CONSOLE_KEY_END:
topLine = lineCount;
break;
}
if (topLine > lineCount - (height - 1))
topLine = lineCount - (height - 1);
if (topLine < 0)
topLine = 0;
}
}
}