Commit 051cdde8 authored by Andrew Gibiansky's avatar Andrew Gibiansky

:!cd now changes directory for user code

parent 943eb9fd
...@@ -36,19 +36,8 @@ ...@@ -36,19 +36,8 @@
"cell_type": "code", "cell_type": "code",
"collapsed": false, "collapsed": false,
"input": [ "input": [
"getStringTarget :: String -> String\n", "import System.Directory\n",
"getStringTarget = go \"\" . reverse\n", "getDirectoryContents \".\""
" 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/"
], ],
"language": "python", "language": "python",
"metadata": { "metadata": {
...@@ -56,38 +45,47 @@ ...@@ -56,38 +45,47 @@
}, },
"outputs": [ "outputs": [
{ {
"html": [
"<span class='err-msg'>Parse error (line 12, column 13): lexical error in string/character literal at end of input</span>"
],
"metadata": {}, "metadata": {},
"output_type": "display_data", "output_type": "display_data",
"text": [ "text": [
"Parse error (line 12, column 13): lexical error in string/character literal at end of input" "[\".\",\"..\",\".hdevtools.sock\",\"blog\",\"experiments\",\"hackathon\",\"haskell-course-preludes\",\"haskell-style-guide\",\"ihaskell\",\"ihaskell-app\",\"linal\",\"notes\",\"slinky.nb\",\"tasha\"]"
] ]
} }
], ],
"prompt_number": 1 "prompt_number": 9
}, },
{ {
"cell_type": "code", "cell_type": "code",
"collapsed": false, "collapsed": false,
"input": [ "input": [
"getStringTarget \"absdf\\\" he\\\\\\\"llo\"" ":!cd code\n",
":!pwd\n",
"setCurrentDirectory \"code\""
], ],
"language": "python", "language": "python",
"metadata": { "metadata": {
"hidden": false "hidden": false
}, },
"outputs": [ "outputs": [
{
"html": [
"<span class='err-msg'>No such directory: 'code'</span>"
],
"metadata": {},
"output_type": "display_data",
"text": [
"No such directory: 'code'"
]
},
{ {
"metadata": {}, "metadata": {},
"output_type": "display_data", "output_type": "display_data",
"text": [ "text": [
"\"he\\\"llo\"" "/Users/silver/code"
] ]
} }
], ],
"prompt_number": 13 "prompt_number": 8
}, },
{ {
"cell_type": "code", "cell_type": "code",
......
...@@ -106,6 +106,7 @@ globalImports :: [String] ...@@ -106,6 +106,7 @@ globalImports :: [String]
globalImports = globalImports =
[ "import IHaskell.Display()" [ "import IHaskell.Display()"
, "import qualified Prelude as IHaskellPrelude" , "import qualified Prelude as IHaskellPrelude"
, "import qualified System.Directory as IHaskellDirectory"
, "import qualified IHaskell.Display" , "import qualified IHaskell.Display"
, "import qualified IHaskell.IPython.Stdin" , "import qualified IHaskell.IPython.Stdin"
, "import qualified System.Posix.IO as IHaskellIO" , "import qualified System.Posix.IO as IHaskellIO"
...@@ -498,24 +499,32 @@ evalCommand _ (Directive LoadFile name) state = wrapExecution state $ do ...@@ -498,24 +499,32 @@ evalCommand _ (Directive LoadFile name) state = wrapExecution state $ do
modName <- intercalate "." <$> getModuleName contents modName <- intercalate "." <$> getModuleName contents
doLoadModule filename modName doLoadModule filename modName
evalCommand publish (Directive ShellCmd ('!':cmd)) state = wrapExecution state $ liftIO $ evalCommand publish (Directive ShellCmd ('!':cmd)) state = wrapExecution state $
case words cmd of case words cmd of
"cd":dirs -> do "cd":dirs -> do
-- Get home so we can replace '~` with it. -- 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 let home = case homeEither of
Left _ -> "~" Left _ -> "~"
Right val -> val Right val -> val
let directory = replace "~" home $ unwords dirs let directory = replace "~" home $ unwords dirs
exists <- doesDirectoryExist directory exists <- liftIO $ doesDirectoryExist directory
if exists if exists
then do 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 return mempty
else else
return $ displayError $ printf "No such directory: '%s'" directory return $ displayError $ printf "No such directory: '%s'" directory
cmd -> do cmd -> liftIO $ do
(readEnd, writeEnd) <- createPipe (readEnd, writeEnd) <- createPipe
handle <- fdToHandle writeEnd handle <- fdToHandle writeEnd
pipe <- fdToHandle readEnd pipe <- fdToHandle readEnd
......
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment