Skip to content

Commit

Permalink
fixed tab completion,cd to files with spaces in the name, prompt on e…
Browse files Browse the repository at this point in the history
…xit (configurable), move config to custom dir, make config if first time running
  • Loading branch information
TorchedSammy committed May 31, 2020
1 parent 58655a5 commit ebabd9f
Show file tree
Hide file tree
Showing 6 changed files with 36 additions and 13 deletions.
5 changes: 3 additions & 2 deletions bin/commands/cd.js
Original file line number Diff line number Diff line change
Expand Up @@ -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")
}
Expand Down
22 changes: 20 additions & 2 deletions bin/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -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))})

Expand All @@ -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});

Expand Down Expand Up @@ -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)
});
5 changes: 1 addition & 4 deletions config.json
Original file line number Diff line number Diff line change
@@ -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"
}
{"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}
2 changes: 1 addition & 1 deletion package-lock.json

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

2 changes: 1 addition & 1 deletion package.json
Original file line number Diff line number Diff line change
@@ -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": {
Expand Down
13 changes: 10 additions & 3 deletions src/utils.js
Original file line number Diff line number Diff line change
Expand Up @@ -14,7 +14,6 @@
*/

const os = require("os")
const config = require("../config.json")
const { colorMap } = require("./colorMap.json")
const Path = require("./Path.js")

Expand All @@ -27,15 +26,15 @@ 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])
}
return str
}

static displayMOTD() {
let motd = config.motd
let motd = this.config().motd
if(!motd || motd === "") return;
const vals = {
"%username%": os.userInfo().username,
Expand All @@ -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;

0 comments on commit ebabd9f

Please sign in to comment.