Displaying results in terminal table with select, scroll and experimental search. Expanded broken character handling for PervCity scraper.
This commit is contained in:
22
src/tui/formatters.js
Normal file
22
src/tui/formatters.js
Normal file
@@ -0,0 +1,22 @@
|
||||
'use strict';
|
||||
|
||||
const moment = require('moment');
|
||||
|
||||
const formatters = {
|
||||
site: site => site.name,
|
||||
date: (date, column) => moment(date).format(column.format || 'MMM DD, YYYY'),
|
||||
actors: actors => actors.join(', '),
|
||||
rating: (rating) => {
|
||||
if (rating.stars === null) {
|
||||
return 'Unrated';
|
||||
}
|
||||
|
||||
if (rating.likes === null || rating.dislikes === null) {
|
||||
return `★ ${rating.stars.toFixed(2)}`;
|
||||
}
|
||||
|
||||
return `★ ${rating.stars.toFixed(2)} ▲ ${String(rating.likes).padEnd(3)} ▼ ${String(rating.dislikes).padEnd(3)}`;
|
||||
},
|
||||
};
|
||||
|
||||
module.exports = formatters;
|
||||
94
src/tui/render.js
Normal file
94
src/tui/render.js
Normal file
@@ -0,0 +1,94 @@
|
||||
'use strict';
|
||||
|
||||
const config = require('config');
|
||||
const blessed = require('neo-blessed');
|
||||
const moment = require('moment');
|
||||
const opn = require('opn');
|
||||
|
||||
const formatters = require('./formatters');
|
||||
|
||||
function render(scenes, screen) {
|
||||
const tableTop = blessed.Text({
|
||||
content: config.columns.reduce((acc, column, index) => `${acc}${'─'.repeat(column.width)}${index < config.columns.length - 1 ? '┬' : '┐\x1b[0m'}`, '\x1b[30m┌'),
|
||||
});
|
||||
|
||||
const items = scenes.map((scene, sceneIndex) => {
|
||||
const isFuture = moment(scene.date).isAfter();
|
||||
|
||||
const row = config.columns.reduce((acc, column) => {
|
||||
const value = (formatters[column.value]
|
||||
? formatters[column.value](scene[column.value], column)
|
||||
: scene[column.value])
|
||||
.toString();
|
||||
|
||||
const truncatedValue = value.length > column.width - 2 ? `${value.slice(0, column.width - 2 - 3)}...` : value;
|
||||
const paddedValue = truncatedValue.padEnd(column.width - 1).padStart(column.width);
|
||||
const coloredValue = isFuture ? `\x1b[92m${paddedValue}\x1b[0m` : `\x1b[97m${paddedValue}\x1b[0m`;
|
||||
|
||||
return `${acc}${coloredValue}\x1b[90m│\x1b[0m`;
|
||||
}, '\x1b[90m│\x1b[0m');
|
||||
|
||||
if (sceneIndex < scenes.length - 1) {
|
||||
const line = config.columns.reduce((acc, column, index) => `${acc}${'─'.repeat(column.width)}${index < config.columns.length - 1 ? '┼' : '┤\x1b[0m'}`, '\n\x1b[30m├');
|
||||
|
||||
return `${row}${line}`;
|
||||
}
|
||||
|
||||
return `${row}${sceneIndex}`;
|
||||
});
|
||||
|
||||
function search(cb) {
|
||||
const searchbox = blessed.Textbox({
|
||||
inputOnFocus: true,
|
||||
});
|
||||
|
||||
screen.append(searchbox);
|
||||
searchbox.focus();
|
||||
|
||||
screen.render();
|
||||
|
||||
searchbox.on('submit', () => {
|
||||
menu.focus();
|
||||
cb(null, searchbox.value);
|
||||
|
||||
screen.append(menu);
|
||||
screen.render();
|
||||
});
|
||||
}
|
||||
|
||||
const menu = blessed.List({
|
||||
style: {
|
||||
selected: {
|
||||
bold: true,
|
||||
},
|
||||
},
|
||||
top: 1,
|
||||
height: screen.rows - 3,
|
||||
keys: true,
|
||||
vi: true,
|
||||
mouse: true,
|
||||
search,
|
||||
items,
|
||||
});
|
||||
|
||||
const tableBottom = blessed.Text({
|
||||
content: config.columns.reduce((acc, column, index) => `${acc}${'─'.repeat(column.width)}${index < config.columns.length - 1 ? '┴' : '┘\x1b[0m\n'}`, '\x1b[30m└'),
|
||||
top: screen.rows - 2,
|
||||
});
|
||||
|
||||
screen.append(tableTop);
|
||||
screen.append(menu);
|
||||
screen.append(tableBottom);
|
||||
|
||||
menu.focus();
|
||||
|
||||
menu.on('select', (child) => {
|
||||
const scene = scenes[menu.getItemIndex(child)];
|
||||
|
||||
opn(scene.url);
|
||||
});
|
||||
|
||||
screen.render();
|
||||
}
|
||||
|
||||
module.exports = render;
|
||||
Reference in New Issue
Block a user