From ebabd9f42b48bf08defc4d3fc16d93bb1aebb912 Mon Sep 17 00:00:00 2001 From: TorchedSammy <38820196+TorchedSammy@users.noreply.github.com> Date: Sat, 30 May 2020 20:37:23 -0400 Subject: [PATCH] fixed tab completion,cd to files with spaces in the name, prompt on exit (configurable), move config to custom dir, make config if first time running --- bin/commands/cd.js | 5 +++-- bin/index.js | 22 ++++++++++++++++++++-- config.json | 5 +---- package-lock.json | 2 +- package.json | 2 +- src/utils.js | 13 ++++++++++--- 6 files changed, 36 insertions(+), 13 deletions(-) diff --git a/bin/commands/cd.js b/bin/commands/cd.js index cddcb96..ae27ed4 100644 --- a/bin/commands/cd.js +++ b/bin/commands/cd.js @@ -3,8 +3,9 @@ const Path = require("../../src/Path.js") exports.run = (args) => { if(!args[1]) return; - if(fs.existsSync(Path.reverseHandle(args[1]))) { - process.chdir(Path.reverseHandle(args[1])) + const dir = args.slice(1).join(" ") + if(fs.existsSync(Path.reverseHandle(dir))) { + process.chdir(Path.reverseHandle(dir)) } else { process.stdout.write("That directory does not exist.\n") } diff --git a/bin/index.js b/bin/index.js index 5731e7f..4d99d38 100644 --- a/bin/index.js +++ b/bin/index.js @@ -23,6 +23,13 @@ const utils = require("../src/utils.js") const Path = require("../src/Path.js") let commands = [] +const baseConfig = { + prompt: "{bold}→ {green}%username%@%hostname%{reset} {bold}{blue}%cwd% λ{reset}{bold} ", + motd: "{bold}Welcome {cyan}%username%{reset}{bold} to {cyan}KannaShell v%ver%!\n", + askOnExit: true +} +if(!utils.config()) fs.appendFileSync(`${require("os").userInfo().homedir}\\.kannashell\\config.json`, JSON.stringify(baseConfig)) + // Put available commands in an array. fs.readdirSync(__dirname + "/commands/").filter(c => c.endsWith('.js')).forEach(c => {commands.push(c.slice(0, -3))}) @@ -31,7 +38,7 @@ completer = (line) => { line = line.slice(3) const currAddedDir = (line.indexOf('\\') != - 1) ? line.substring(0, line.lastIndexOf('\\') + 1) : ''; const currAddingDir = line.substr(line.lastIndexOf('\\') + 1); - const path = process.cwd() + '\\' + currAddedDir; + const path = `${line.match(/^(~|\/)/) ? Path.reverseHandle(line.split("\\")[0]) : process.cwd()}\\${line.match(/^(~|\/)/) ? currAddedDir.slice(1) : currAddedDir}`; const completions = fs.readdirSync(path, { withFileTypes: true }).filter(p => p.isDirectory()).map(d => d.name) const hits = completions.filter(function(c) { return c.indexOf(currAddingDir) === 0}); @@ -75,5 +82,16 @@ utils.displayMOTD() prompt() ci.on('SIGINT', () => { - return; + if(!config.askOnExit) { + ci.close() + } else { + ci.question("Are you sure that you want to exit?", (ans) => { + if(ans.match(/^y(es)?$/i)) ci.close() + }) + } +}); + +ci.on('close', () => { + console.log("\nGoodbye!") + process.exit(0) }); \ No newline at end of file diff --git a/config.json b/config.json index 030d33b..d7a9daf 100644 --- a/config.json +++ b/config.json @@ -1,4 +1 @@ -{ - "prompt": "{bold}→ {green}%username%@%hostname%{reset} {bold}{blue}%cwd% λ{reset}{bold} ", - "motd": "{bold}Welcome {cyan}%username%{reset}{bold} to {cyan}KannaShell v%ver%!\n" -} \ No newline at end of file +{"prompt":"{bold}→ {green}%username%@%hostname%{reset} {bold}{blue}%cwd% λ{reset}{bold} ","motd":"{bold}Welcome {cyan}%username%{reset}{bold} to {cyan}KannaShell v%ver%!\n","askOnExit":true} \ No newline at end of file diff --git a/package-lock.json b/package-lock.json index 3634ab5..3547c34 100644 --- a/package-lock.json +++ b/package-lock.json @@ -1,5 +1,5 @@ { "name": "kannashell", - "version": "0.0.18", + "version": "0.0.19", "lockfileVersion": 1 } diff --git a/package.json b/package.json index 9b5432e..e0bd0c1 100644 --- a/package.json +++ b/package.json @@ -1,6 +1,6 @@ { "name": "kannashell", - "version": "0.0.18", + "version": "0.0.19", "description": "A small, light terminal shell for Windows.", "main": "bin/index.js", "scripts": { diff --git a/src/utils.js b/src/utils.js index f72ade1..30674c5 100644 --- a/src/utils.js +++ b/src/utils.js @@ -14,7 +14,6 @@ */ const os = require("os") -const config = require("../config.json") const { colorMap } = require("./colorMap.json") const Path = require("./Path.js") @@ -27,7 +26,7 @@ class KannaUtils { "%cwf%": Path.handle(process.cwd()).split("\\")[Path.handle(process.cwd()).split("\\").length - 1], ...colorMap } - let str = config.prompt + let str = this.config().prompt for(let key in vals) { str = str.replace(new RegExp(key, "g"), vals[key]) } @@ -35,7 +34,7 @@ class KannaUtils { } static displayMOTD() { - let motd = config.motd + let motd = this.config().motd if(!motd || motd === "") return; const vals = { "%username%": os.userInfo().username, @@ -50,6 +49,14 @@ class KannaUtils { } return console.log(motd + colorMap["{reset}"]) } + + static config() { + if(require("fs").existsSync(`${os.userInfo().homedir}\\.kannashell\\config.json`)) { + return JSON.parse(require("fs").readFileSync(`${os.userInfo().homedir}\\.kannashell\\config.json`)) + } else { + return false; + } + } } module.exports = KannaUtils; \ No newline at end of file