forked from cryptic-game/frontend
-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
refactored first commands added autocomplete after first word (cryptic-game#39) added some argparsing improved help command added aliases
- Loading branch information
Showing
16 changed files
with
782 additions
and
21 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,10 @@ | ||
import {Status} from './status'; | ||
import {Hostname} from './hostname'; | ||
import {Miner} from './miner'; | ||
import {Cd} from './cd'; | ||
import {Ls} from './ls'; | ||
import {Clear} from './clear'; | ||
import {Exit} from './exit'; | ||
|
||
export const BUILTINS = [Status, Hostname, Miner, Cd, Ls, Clear, Exit]; | ||
|
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,43 @@ | ||
import {Command, IOHandler, ArgType} from '../command'; | ||
import {ShellApi} from '../shellapi'; | ||
import {Path} from 'src/app/api/files/path'; | ||
|
||
export class Cd extends Command { | ||
constructor(shellApi: ShellApi) { | ||
super('cd', shellApi); | ||
this.addDescription('changes the working directory'); | ||
this.addPositionalArgument({name: 'directory', optional: true, argType: ArgType.DIRECTORY}); | ||
} | ||
|
||
async run(iohandler: IOHandler): Promise<number> { | ||
const args = iohandler.positionalArgs; | ||
const input = args.length === 1 ? args[0] : '/'; | ||
let path: Path; | ||
try { | ||
path = Path.fromString(input, this.shellApi.working_dir); | ||
} catch { | ||
iohandler.stderr('The specified path is not valid'); | ||
return 1; | ||
} | ||
let file: any; | ||
try { | ||
file = await this.shellApi.fileService.getFromPath(this.shellApi.activeDevice['uuid'], path).toPromise(); | ||
} catch (error) { | ||
if (error.message === 'file_not_found') { | ||
iohandler.stderr('That directory does not exist'); | ||
return 2; | ||
} else { | ||
this.reportError(error); | ||
return 1; | ||
} | ||
} | ||
if (file.is_directory) { | ||
this.shellApi.working_dir = file.uuid; | ||
this.shellApi.refreshPrompt(); | ||
return 0; | ||
} else { | ||
iohandler.stderr('That is not a directory'); | ||
return 1; | ||
} | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,14 @@ | ||
import {Command, IOHandler} from '../command'; | ||
import {ShellApi} from '../shellapi'; | ||
|
||
export class Clear extends Command { | ||
constructor(shellApi: ShellApi) { | ||
super('clear', shellApi); | ||
this.addDescription('clears the terminal'); | ||
} | ||
|
||
async run(_: IOHandler): Promise<number> { | ||
this.shellApi.terminal.clear(); | ||
return 0; | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,14 @@ | ||
import {Command, IOHandler} from '../command'; | ||
import {ShellApi} from '../shellapi'; | ||
|
||
export class Exit extends Command { | ||
constructor(shellApi: ShellApi) { | ||
super('exit', shellApi); | ||
this.addDescription('closes the terminal or leaves another device'); | ||
} | ||
|
||
async run(_: IOHandler): Promise<number> { | ||
this.shellApi.terminal.popState(); | ||
return 0; | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,51 @@ | ||
import {Command, IOHandler} from '../command'; | ||
import {Device} from '../../api/devices/device'; | ||
import {ShellApi} from '../shellapi'; | ||
|
||
export class Hostname extends Command { | ||
constructor(shellApi: ShellApi) { | ||
super('hostname', shellApi); | ||
this.addDescription('changes the name of the device'); | ||
this.addPositionalArgument({name: 'name', optional: true}); | ||
} | ||
|
||
async run(iohandler: IOHandler): Promise<number> { | ||
const args = iohandler.positionalArgs; | ||
if (args.length === 1) { | ||
const hostname = args[0]; | ||
let newDevice: Device; | ||
try { | ||
newDevice = await this.shellApi.websocket.ms('device', ['device', 'change_name'], { | ||
device_uuid: this.shellApi.activeDevice['uuid'], | ||
name: hostname | ||
}).toPromise(); | ||
} catch { | ||
iohandler.stderr('The hostname couldn\'t be changed'); | ||
return 1; | ||
} | ||
this.shellApi.activeDevice = newDevice; | ||
this.shellApi.refreshPrompt(); | ||
|
||
if (this.shellApi.activeDevice.uuid === this.shellApi.windowDelegate.device.uuid) { | ||
Object.assign(this.shellApi.windowDelegate.device, newDevice); | ||
} | ||
} else { | ||
let device: Device; | ||
try { | ||
device = await this.shellApi.websocket.ms( | ||
'device', | ||
['device', 'info'], | ||
{device_uuid: this.shellApi.activeDevice['uuid']} | ||
).toPromise(); | ||
} catch { | ||
iohandler.stdout(this.shellApi.activeDevice['name']); | ||
} | ||
if (device['name'] !== this.shellApi.activeDevice['name']) { | ||
this.shellApi.activeDevice = device; | ||
this.shellApi.refreshPrompt(); | ||
} | ||
iohandler.stdout(device['name']); | ||
} | ||
return 0; | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,54 @@ | ||
import {Command, IOHandler, ArgType} from '../command'; | ||
import {ShellApi} from '../shellapi'; | ||
import {Path} from 'src/app/api/files/path'; | ||
import {File} from 'src/app/api/files/file'; | ||
|
||
export class Ls extends Command { | ||
constructor(shellApi: ShellApi) { | ||
super('ls', shellApi); | ||
this.addDescription('shows files of the current working directory'); | ||
this.addPositionalArgument({name: 'path', optional: true, argType: ArgType.PATH}); | ||
} | ||
|
||
async run(iohandler: IOHandler): Promise<number> { | ||
const args = iohandler.positionalArgs; | ||
let files: File[]; | ||
if (args.length === 0) { | ||
files = await this.shellApi.listFilesOfWorkingDir(); | ||
} else { | ||
let path: Path; | ||
try { | ||
path = Path.fromString(args[0], this.shellApi.working_dir); | ||
} catch { | ||
iohandler.stderr('The specified path is not valid'); | ||
return 1; | ||
} | ||
try { | ||
const target = await this.shellApi.fileService.getFromPath(this.shellApi.activeDevice['uuid'], path).toPromise(); | ||
if (target.is_directory) { | ||
files = await this.shellApi.fileService.getFiles(this.shellApi.activeDevice['uuid'], target.uuid).toPromise(); | ||
} else { | ||
files = [target]; | ||
} | ||
} catch (error) { | ||
if (error.message === 'file_not_found') { | ||
iohandler.stderr('That directory does not exist'); | ||
return 2; | ||
} else { | ||
this.reportError(error); | ||
return 1; | ||
} | ||
} | ||
} | ||
|
||
files.filter((file) => file.is_directory).sort().forEach(folder => { | ||
// TODO use escape codes | ||
iohandler.stdout(`<span style="color: ${this.shellApi.settings.getLSFC()};">${(this.shellApi.settings.getLSPrefix()) ? '[Folder] ' : ''}${folder.filename}</span>`); | ||
}); | ||
|
||
files.filter((file) => !file.is_directory).sort().forEach(file => { | ||
iohandler.stdout(`${(this.shellApi.settings.getLSPrefix() ? '[File] ' : '')}${file.filename}`); | ||
}); | ||
return 0; | ||
} | ||
} |
Oops, something went wrong.