Skip to content

Commit a109f54

Browse files
authored
Updated "help" and "install" commands
help: the command list is now separated into pages use "help --page=x" to see the page №x install: you can now use "install --list" instead of "ls /system/js/apps" to see the list of available applications "install --list=x" to see the page №x sorry for bad eng
1 parent a23a791 commit a109f54

File tree

1 file changed

+30
-27
lines changed

1 file changed

+30
-27
lines changed

js/service/shell/commands.js

Lines changed: 30 additions & 27 deletions
Original file line numberDiff line numberDiff line change
@@ -20,6 +20,7 @@ const $$ = require('jsos');
2020
const persistence = require('persistence');
2121
const processor = require('./index.js');
2222
const { log, warn } = $$.logger;
23+
const { HEIGHT } = require('../../core/tty/vga.js');
2324

2425
debug('Loading commands...');
2526

@@ -79,26 +80,26 @@ const cmds = {
7980
let args = _args.trim();
8081

8182
if (!args || args.split(/\s+/)[0].startsWith("-p") || args.split(/\s+/)[0].startsWith("--page")) {
82-
var tmp_page = args.split(/\s+/)[0].slice(3 + 4 * Number(args.split(/\s+/)[0].startsWith("--page"))) - 1 || 0;
83+
let tmp_page = args.split(/\s+/)[0].slice(3 + 4 * Number(args.split(/\s+/)[0].startsWith("--page"))) - 1 || 0;
8384
if(tmp_page == -1) tmp_page = 0;
84-
var list = Array.from(processor.getCommands()).sort();
85-
if(tmp_page < 0 || tmp_page + 1 > Math.ceil(list.length / (vga.HEIGHT - 4))){
85+
const height = HEIGHT - 4;
86+
const commandList = Array.from(processor.getCommands()).sort();
87+
if(tmp_page < 0 || tmp_page + 1 > Math.ceil(commandList.length / height)){
8688
f.stdio.setColor('red');
8789
f.stdio.write("Invalid page!");
8890
return res(1);
8991
}
90-
91-
f.stdio.writeLine(`Commands list (page ${tmp_page + 1}/${Math.ceil(list.length / (vga.HEIGHT - 4))}):`);
92+
f.stdio.writeLine(`Commands list (page ${tmp_page + 1}/${Math.ceil(commandList.length / height)}):`);
9293
// let out = 'Commands list:\n';
9394

94-
for (var i = tmp_page * (vga.HEIGHT - 4); i < (tmp_page + 1) * (vga.HEIGHT - 4); i++) {
95+
for (var i = tmp_page * height; i < (tmp_page + 1) * height; i++) {
9596
// out += `${i}: ${processor.getDescription(i)}\n`;
96-
if(i == list.length) break;
97-
var _i = list[i];
97+
if(i == commandList.length) break;
98+
const command = commandList[i];
9899
f.stdio.setColor('yellow');
99-
f.stdio.write(_i);
100+
f.stdio.write(command);
100101
f.stdio.setColor('white');
101-
f.stdio.writeLine(`: ${processor.getDescription(_i)}`);
102+
f.stdio.writeLine(`: ${processor.getDescription(command)}`);
102103
}
103104
// f.stdio.write(out);
104105
} else {
@@ -166,34 +167,35 @@ const cmds = {
166167
'install': {
167168
'description': 'Install applications',
168169
'usage': 'install <app> |OR| install --list[=n] |OR| install -l[=n]',
169-
run (app, f, res) {
170-
if(app.trim().split(/\s+/)[0].slice(0, 2) == "-l" || app.trim().split(/\s+/)[0].slice(0, 6) == "--list"){
170+
run (_args, f, res) {
171+
let args = _args.trim().split(/\s+/);
172+
if(args[0].slice(0, 2) === "-l" || args[0].slice(0, 6) === "--list"){
171173
const fs = require('fs');
172174
fs.readdir("/system/js/apps/", "utf-8", (err, list_) => {
173175
if(err){
174176
f.stdio.writeError("Unknown error!");
175177
debug(error);
176178
return res(1);
177179
}
178-
var list = [], ml = 1;
179-
for(var i = 0; i < list_.length; i++){
180-
ml = Math.max(ml, list_[i].length + 1);
180+
let list = [], maxLength = 1;
181+
for(const app of list_){
182+
maxLength = Math.max(maxLength, app.length + 1);
181183
}
182-
for(var i = 0; i < list_.length; i++){
183-
list.push(list_[i] + " ".repeat(ml - list_[i].length) + "| " + fs.readFileSync("/system/js/apps/" + list_[i] + "/description.txt", "utf-8"));
184+
for(const app of list_){
185+
list.push(app + " ".repeat(maxLength - app.length) + "| " + fs.readFileSync("/system/js/apps/" + app + "/description.txt", "utf-8").replace("\n", "")); //костыль, не знаю, почему, но иногда появляется перенос строки в конце описания
184186
}
185-
if(app.trim().split(/\s+/)[0] == "-l" || app.trim().split(/\s+/)[0] == "--list") app = "-l=1";
186-
var tmp_page = app.trim().slice(Number(app.trim().split(/\s+/)[0].slice(0, 6) == "--list") * 4 + 3) - 1;
187-
if(tmp_page + 1 > Math.ceil(list.length / (vga.HEIGHT - 12))){
187+
if(args[0] === "-l" || args[0] === "--list") args[0] = "-l=1";
188+
const tmp_page = args[0].slice(Number(args[0].slice(0, 6) == "--list") * 4 + 3) - 1; //Получаем номер страницы мз первого аргумента
189+
const height = HEIGHT - 12;
190+
if(tmp_page + 1 > Math.ceil(list.length / height)){
188191
f.stdio.setColor('red');
189192
f.stdio.writeLine("Invalid page!");
190193
return res(1);
191194
}
192195
f.stdio.setColor('magenta');
193-
f.stdio.writeLine(`Applications list (page: ${tmp_page + 1}/${Math.ceil(list.length / (vga.HEIGHT - 12))})
194-
`);
196+
f.stdio.writeLine(`Applications list (page: ${tmp_page + 1}/${Math.ceil(list.length / height)})`);
195197
f.stdio.setColor('yellow');
196-
for(var i = tmp_page * (vga.HEIGHT - 12); i < (tmp_page + 1) * (vga.HEIGHT - 12); i++){
198+
for(var i = tmp_page * height; i < (tmp_page + 1) * height; i++){
197199
if(i == list.length) break;
198200
f.stdio.writeLine(list[i]);
199201
}
@@ -208,15 +210,15 @@ start <app>`);
208210
});
209211
return res(0);
210212
}
211-
if ($$.appman.install(app.trim().split(/\s+/)[0])) {
213+
if ($$.appman.install(args[0])) {
212214

213215
f.stdio.setColor('green');
214-
f.stdio.writeLine(`App ${app} installed successful!`);
216+
f.stdio.writeLine(`App ${args[0]} installed successful!`);
215217

216218
return res(0);
217219

218-
}else{
219-
f.stdio.writeError(`Error happened during ${app} installation`);
220+
} else {
221+
f.stdio.writeError(`Error happened during installation of ${app}`);
220222

221223
return res(1);
222224
}
@@ -453,3 +455,4 @@ for (const i in cmds) {
453455
debug('Commands loaded successful!');
454456

455457
module.exports = cmds;
458+

0 commit comments

Comments
 (0)