:!cd now changes directory for user code

This commit is contained in:
Andrew Gibiansky 2014-05-18 10:18:53 -07:00
parent 943eb9fd27
commit 051cdde802
2 changed files with 44 additions and 37 deletions

View File

@ -36,19 +36,31 @@
"cell_type": "code",
"collapsed": false,
"input": [
"getStringTarget :: String -> String\n",
"getStringTarget = go \"\" . reverse\n",
" where\n",
" go acc rest = case rest of\n",
" '\"':'\\\\':rem -> go ('\"':acc) rem\n",
" '\"':rem -> acc\n",
" ' ':'\\\\':rem -> go (' ':acc) rem\n",
" ' ':rem -> acc\n",
" x:rem -> go (x:acc) rem\n",
" [] -> acc\n",
"\n",
"\" ~/archive/\n",
":load \" ~/archive/"
"import System.Directory\n",
"getDirectoryContents \".\""
],
"language": "python",
"metadata": {
"hidden": false
},
"outputs": [
{
"metadata": {},
"output_type": "display_data",
"text": [
"[\".\",\"..\",\".hdevtools.sock\",\"blog\",\"experiments\",\"hackathon\",\"haskell-course-preludes\",\"haskell-style-guide\",\"ihaskell\",\"ihaskell-app\",\"linal\",\"notes\",\"slinky.nb\",\"tasha\"]"
]
}
],
"prompt_number": 9
},
{
"cell_type": "code",
"collapsed": false,
"input": [
":!cd code\n",
":!pwd\n",
"setCurrentDirectory \"code\""
],
"language": "python",
"metadata": {
@ -57,37 +69,23 @@
"outputs": [
{
"html": [
"<span class='err-msg'>Parse error (line 12, column 13): lexical error in string/character literal at end of input</span>"
"<span class='err-msg'>No such directory: 'code'</span>"
],
"metadata": {},
"output_type": "display_data",
"text": [
"Parse error (line 12, column 13): lexical error in string/character literal at end of input"
"No such directory: 'code'"
]
}
],
"prompt_number": 1
},
{
"cell_type": "code",
"collapsed": false,
"input": [
"getStringTarget \"absdf\\\" he\\\\\\\"llo\""
],
"language": "python",
"metadata": {
"hidden": false
},
"outputs": [
},
{
"metadata": {},
"output_type": "display_data",
"text": [
"\"he\\\"llo\""
"/Users/silver/code"
]
}
],
"prompt_number": 13
"prompt_number": 8
},
{
"cell_type": "code",

View File

@ -106,6 +106,7 @@ globalImports :: [String]
globalImports =
[ "import IHaskell.Display()"
, "import qualified Prelude as IHaskellPrelude"
, "import qualified System.Directory as IHaskellDirectory"
, "import qualified IHaskell.Display"
, "import qualified IHaskell.IPython.Stdin"
, "import qualified System.Posix.IO as IHaskellIO"
@ -498,24 +499,32 @@ evalCommand _ (Directive LoadFile name) state = wrapExecution state $ do
modName <- intercalate "." <$> getModuleName contents
doLoadModule filename modName
evalCommand publish (Directive ShellCmd ('!':cmd)) state = wrapExecution state $ liftIO $
evalCommand publish (Directive ShellCmd ('!':cmd)) state = wrapExecution state $
case words cmd of
"cd":dirs -> do
-- Get home so we can replace '~` with it.
homeEither <- try $ getEnv "HOME" :: IO (Either SomeException String)
homeEither <- liftIO (try $ getEnv "HOME" :: IO (Either SomeException String))
let home = case homeEither of
Left _ -> "~"
Right val -> val
let directory = replace "~" home $ unwords dirs
exists <- doesDirectoryExist directory
exists <- liftIO $ doesDirectoryExist directory
if exists
then do
setCurrentDirectory directory
-- Set the directory in IHaskell native code, for future shell
-- commands. This doesn't set it for user code, though.
liftIO $ setCurrentDirectory directory
-- Set the directory for user code.
let cmd = printf "IHaskellDirectory.setCurrentDirectory \"%s\"" $
replace " " "\\ " $
replace "\"" "\\\"" directory
runStmt cmd RunToCompletion
return mempty
else
return $ displayError $ printf "No such directory: '%s'" directory
cmd -> do
cmd -> liftIO $ do
(readEnd, writeEnd) <- createPipe
handle <- fdToHandle writeEnd
pipe <- fdToHandle readEnd