Created
August 23, 2014 14:02
-
-
Save pauldub/86167d884cacb7ab88a7 to your computer and use it in GitHub Desktop.
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
#!/usr/local/bin/io | |
Shell := Object clone | |
Shell Runner := Object clone do( | |
shell ::= "/bin/sh -c" | |
cd := method(path, | |
Directory setCurrentWorkingDirectory( | |
if(path findSeq("..") != nil, | |
Directory with(Directory currentWorkingDirectory) directoryNamed(path) path, | |
path))) | |
escapeCommand := method(command, | |
if(shell isActivatable, shell(), shell) .. " \"" .. command .. "\"") | |
system := method( | |
command := call evalArgAt(0) | |
if(command != nil, System system(escapeCommand(command)))) | |
runCommand := method( | |
command := call evalArgAt(0) | |
if(command != nil, System runCommand(escapeCommand(command)))) | |
) | |
Shell do( | |
runner ::= Shell Runner clone | |
started := false | |
prompt ::= "> " | |
cwd := method(Directory with(Directory currentWorkingDirectory)) | |
home := Directory with(System getEnvironmentVariable("HOME")) | |
historyFile ::= home fileNamed(".iosh_history") | |
initFile ::= home fileNamed(".ioshrc") | |
run := method( | |
input := call evalArgAt(0) | |
if(input findSeq("cd ") != nil) then( | |
runner cd(input replaceSeq("~", System getEnvironmentVariable("HOME")) removeSeq("cd ")) | |
) else( | |
err := try( | |
result := Lobby doMessage(Message fromString(input))) | |
if(err, | |
runner system(Lobby evalArg(input interpolate)), | |
if(result, result println)))) | |
start := method( | |
if(historyFile exists != true, home fileNamed(historyFile name) create) | |
ReadLine loadHistory(historyFile path) | |
started = true | |
loop( | |
if(started != true, break) | |
ReadLine prompt := if(prompt isActivatable, prompt(), prompt) | |
input := ReadLine readLine | |
if(input == nil, break) | |
ReadLine addHistory(input) | |
# ReadLine asyncSend(saveHistory(historyFile path)) | |
if(input == "exit", break) | |
asyncSend(run(input)) | |
wait(0.05)) | |
ReadLine asyncSend(saveHistory(historyFile path)) | |
nil) | |
stop := method(started = false) | |
reload := method( | |
stop | |
loadInitFile) | |
loadInitFile := method( | |
("Loading " .. initFile path) println | |
if(initFile exists == true, doFile(initFile path))) | |
) | |
Ssh := Shell Runner clone do( | |
sshPath ::= "/usr/local/bin/ssh" | |
connection ::= "" | |
cwd := nil | |
shell := method(sshPath .. " -t " .. connection) | |
) | |
/* Hmm ... | |
shell := method(sshPath .. " -t " .. connection .. " '/usr/bin/sh -c \"" .. if(cwd != nil, " cd " .. cwd .. " && ", "")) | |
escapeCommand := method(command, | |
shell() .. command .. "\"'") | |
cd := method(path, | |
if(cwd == nil, cwd = runCommand("pwd") stdout replaceSeq("\n", "") replaceSeq("\r", "") replaceSeq("Standard Input: ", "")) | |
system("cd " .. path)) ) | |
*/ | |
#Shell loadInitFile | |
## Load init file or start a shell | |
if(Shell initFile exists == true, doFile(Shell initFile path), shell := Shell clone start) |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment