2020-07-02 15:25:54 +00:00
|
|
|
"use strict";
|
|
|
|
|
|
|
|
import fs from 'fs';
|
|
|
|
|
|
|
|
import Terrain50 from 'terrain50';
|
|
|
|
|
|
|
|
import a from '../../Helpers/Ansi.mjs';
|
|
|
|
import l from '../../Helpers/Log.mjs';
|
|
|
|
|
|
|
|
import { percentage } from '../../Helpers/MathsHelpers.mjs';
|
|
|
|
|
|
|
|
export default async function(settings) {
|
|
|
|
// 1: Parse settings
|
|
|
|
let stream_in = process.stdin;
|
|
|
|
if(settings.cli.input !== "-") {
|
|
|
|
l.log(`Reading from ${a.hicol}${settings.cli.input}${a.reset}`);
|
|
|
|
stream_in = fs.createReadStream(settings.cli.input);
|
|
|
|
}
|
|
|
|
else
|
|
|
|
l.log(`Reading from stdin`);
|
|
|
|
|
|
|
|
let stream_out = process.stdout;
|
|
|
|
if(settings.cli.output !== "-") {
|
|
|
|
l.log(`Writing to ${a.hicol}${settings.cli.output}${a.reset}`);
|
|
|
|
stream_out = fs.createWriteStream(settings.cli.output);
|
|
|
|
}
|
|
|
|
else
|
|
|
|
l.log(`Writing to stdout`);
|
|
|
|
|
|
|
|
let count = settings.cli.count,
|
|
|
|
offset = settings.cli.offset;
|
|
|
|
|
|
|
|
// ------------------------------------------------------------------------
|
|
|
|
|
|
|
|
let i = -1;
|
2021-01-15 17:06:42 +00:00
|
|
|
for await(let next of Terrain50.ParseStream(stream_in, settings.cli.tolerant ? /\s+/ : " ")) {
|
2020-07-02 15:25:54 +00:00
|
|
|
i++;
|
|
|
|
|
|
|
|
if(i < offset) continue;
|
|
|
|
|
2020-07-02 16:25:53 +00:00
|
|
|
// Write it to the output
|
|
|
|
let is_last = i - offset >= count - 1;
|
2020-07-02 15:25:54 +00:00
|
|
|
await next.serialise(stream_out, is_last);
|
|
|
|
|
2020-07-02 16:26:45 +00:00
|
|
|
// Update the user
|
2020-07-02 16:27:39 +00:00
|
|
|
l.log(`Written ${(i - offset) + 1} / ${count} objects (~${percentage((i - offset) + 1, count).toFixed(2)}%)`);
|
2020-07-02 16:26:45 +00:00
|
|
|
|
2020-07-02 16:25:53 +00:00
|
|
|
// Don't go further than we need to
|
2020-07-02 16:19:27 +00:00
|
|
|
if(is_last) break;
|
2020-07-02 15:25:54 +00:00
|
|
|
}
|
|
|
|
l.log(`Slicing complete`);
|
2020-07-02 16:19:27 +00:00
|
|
|
|
2020-07-02 16:23:29 +00:00
|
|
|
stream_in.destroy();
|
2020-07-02 15:25:54 +00:00
|
|
|
}
|