git.iko.soy/rowtype-yoga/purescript-native
on golang help sign in
Add paste mode, remove --multi-line option, fix #934 (#2361)
commit0072f2569402b938d35d6d5cbe31cbdb1640f90d authorPhil Freeman <paf31@cantab.net> dateSun 23 Oct 2016, 14:50 (10 years ago) parentb8bb12b
6 files changed+48−53
psci/Main.hs+29−22 src/Language/PureScript/Interactive.hs+1−1 src/Language/PureScript/Interactive/Completion.hs+1 src/Language/PureScript/Interactive/Directive.hs+3 src/Language/PureScript/Interactive/Parser.hs+1 src/Language/PureScript/Interactive/Types.hs+13−30
psci/Main.hs+29−22
@@ -63,17 +63,10 @@ import System.Process (readProcessWithExitCode)
6363
6464-- | Command line options
6565data PSCiOptions = PSCiOptions
66− { psciMultiLineMode :: Bool
67− , psciInputFile :: [FilePath]
66+ { psciInputFile :: [FilePath]
6867 , psciBackend :: Backend
6968 }
7069
71−multiLineMode :: Opts.Parser Bool
72−multiLineMode = Opts.switch $
73− Opts.long "multi-line-mode"
74− <> Opts.short 'm'
75− <> Opts.help "Run in multi-line mode (use ^D to terminate commands)"
76−
7770inputFile :: Opts.Parser FilePath
7871inputFile = Opts.strArgument $
7972 Opts.metavar "FILE"
@@ -100,8 +93,7 @@ backend =
10093 <|> (nodeBackend <$> nodeFlagsFlag)
10194
10295psciOptions :: Opts.Parser PSCiOptions
103−psciOptions = PSCiOptions <$> multiLineMode
104− <*> many inputFile
96+psciOptions = PSCiOptions <$> many inputFile
10597 <*> backend
10698
10799version :: Opts.Parser (a -> a)
@@ -119,17 +111,20 @@ getOpt = Opts.execParser opts
119111 footerInfo = Opts.footer $ "psci " ++ showVersion Paths.version
120112
121113-- | Parses the input and returns either a command, or an error as a 'String'.
122−getCommand :: forall m. MonadException m => Bool -> InputT m (Either String (Maybe Command))
123−getCommand singleLineMode = handleInterrupt (return (Right Nothing)) $ do
124− firstLine <- withInterrupt $ getInputLine "> "
125− case firstLine of
114+getCommand :: forall m. MonadException m => InputT m (Either String (Maybe Command))
115+getCommand = handleInterrupt (return (Right Nothing)) $ do
116+ line <- withInterrupt $ getInputLine "> "
117+ case line of
126118 Nothing -> return (Right (Just QuitPSCi)) -- Ctrl-D when input is empty
127119 Just "" -> return (Right Nothing)
128− Just s | singleLineMode || head s == ':' -> return . fmap Just $ parseCommand s
129− Just s -> fmap Just . parseCommand <$> go [s]
120+ Just s -> return . fmap Just $ parseCommand s
121+
122+pasteMode :: forall m. MonadException m => InputT m (Either String Command)
123+pasteMode =
124+ parseCommand <$> go []
130125 where
131126 go :: [String] -> InputT m String
132− go ls = maybe (return . unlines $ reverse ls) (go . (:ls)) =<< getInputLine " "
127+ go ls = maybe (return . unlines $ reverse ls) (go . (:ls)) =<< getInputLine "… "
133128
134129-- | Make a JavaScript bundle for the browser.
135130bundle :: IO (Either Bundle.ErrorMessage String)
@@ -349,16 +344,28 @@ main = getOpt >>= loop
349344
350345 go :: state -> InputT (StateT PSCiState (ReaderT PSCiConfig IO)) ()
351346 go state = do
352− c <- getCommand (not psciMultiLineMode)
347+ c <- getCommand
353348 case c of
354349 Left err -> outputStrLn err >> go state
355350 Right Nothing -> go state
351+ Right (Just PasteLines) -> do
352+ c' <- pasteMode
353+ case c' of
354+ Left err -> outputStrLn err >> go state
355+ Right c'' -> handleCommandWithInterrupts state c''
356356 Right (Just QuitPSCi) -> do
357357 outputStrLn quitMessage
358358 liftIO $ shutdown state
359− Right (Just c') -> do
360− handleInterrupt (outputStrLn "Interrupted.")
361− (withInterrupt (lift (handleCommand (liftIO . eval state) (liftIO (reload state)) c')))
362− go state
359+ Right (Just c') -> handleCommandWithInterrupts state c'
360+
361+ handleCommandWithInterrupts
362+ :: state
363+ -> Command
364+ -> InputT (StateT PSCiState (ReaderT PSCiConfig IO)) ()
365+ handleCommandWithInterrupts state cmd = do
366+ handleInterrupt (outputStrLn "Interrupted.")
367+ (withInterrupt (lift (handleCommand (liftIO . eval state) (liftIO (reload state)) cmd)))
368+ go state
369+
363370 putStrLn prologueMessage
364371 setup >>= runner . go
src/Language/PureScript/Interactive.hs+1−1
@@ -104,7 +104,7 @@ handleCommand _ _ (KindOf typ) = handleKindOf typ
104104handleCommand _ _ (BrowseModule moduleName) = handleBrowse moduleName
105105handleCommand _ _ (ShowInfo QueryLoaded) = handleShowLoadedModules
106106handleCommand _ _ (ShowInfo QueryImport) = handleShowImportedModules
107−handleCommand _ _ QuitPSCi = P.internalError "`handleCommand QuitPSCi` was called. This is a bug."
107+handleCommand _ _ _ = P.internalError "handleCommand: unexpected command"
108108
109109-- | Reset the application state
110110handleResetState
src/Language/PureScript/Interactive/Completion.hs+1
@@ -122,6 +122,7 @@ directiveArg _ Browse = [CtxModule]
122122directiveArg _ Quit = []
123123directiveArg _ Reset = []
124124directiveArg _ Help = []
125+directiveArg _ Paste = []
125126directiveArg _ Show = map CtxFixed replQueryStrings
126127directiveArg _ Type = [CtxIdentifier]
127128directiveArg _ Kind = [CtxType]
src/Language/PureScript/Interactive/Directive.hs+3
@@ -30,6 +30,7 @@ directiveStrings =
3030 , (Type , ["type"])
3131 , (Kind , ["kind"])
3232 , (Show , ["show"])
33+ , (Paste , ["paste"])
3334 ]
3435
3536-- |
@@ -82,6 +83,7 @@ hasArgument :: Directive -> Bool
8283hasArgument Help = False
8384hasArgument Quit = False
8485hasArgument Reset = False
86+hasArgument Paste = False
8587hasArgument _ = True
8688
8789-- |
@@ -97,4 +99,5 @@ help =
9799 , (Kind, "<type>", "Show the kind of <type>")
98100 , (Show, "import", "Show all imported modules")
99101 , (Show, "loaded", "Show all loaded modules")
102+ , (Paste, "paste", "Enter multiple lines, terminated by ^D")
100103 ]
src/Language/PureScript/Interactive/Parser.hs+1
@@ -62,6 +62,7 @@ parseDirective cmd =
6262 Help -> return ShowHelp
6363 Quit -> return QuitPSCi
6464 Reset -> return ResetState
65+ Paste -> return PasteLines
6566 Browse -> BrowseModule <$> parseRest P.moduleName arg
6667 Show -> ShowInfo <$> parseReplQuery' (trim arg)
6768 Type -> TypeOf <$> parseRest P.parseValue arg
src/Language/PureScript/Interactive/Types.hs+13−30
@@ -72,46 +72,28 @@ updateLets f st = st { psciLetBindings = f (psciLetBindings st) }
7272-- Valid Meta-commands for PSCI
7373--
7474data Command
75− -- |
76− -- A purescript expression
77− --
75+ -- | A purescript expression
7876 = Expression P.Expr
79− -- |
80− -- Show the help (ie, list of directives)
81− --
77+ -- | Show the help (ie, list of directives)
8278 | ShowHelp
83− -- |
84− -- Import a module from a loaded file
85− --
79+ -- | Import a module from a loaded file
8680 | Import ImportedModule
87− -- |
88− -- Browse a module
89− --
81+ -- | Browse a module
9082 | BrowseModule P.ModuleName
91− -- |
92− -- Exit PSCI
93− --
83+ -- | Exit PSCI
9484 | QuitPSCi
95− -- |
96− -- Reset the state of the REPL
97− --
85+ -- | Reset the state of the REPL
9886 | ResetState
99− -- |
100− -- Add some declarations to the current evaluation context.
101− --
87+ -- | Add some declarations to the current evaluation context
10288 | Decls [P.Declaration]
103− -- |
104− -- Find the type of an expression
105− --
89+ -- | Find the type of an expression
10690 | TypeOf P.Expr
107− -- |
108− -- Find the kind of an expression
109− --
91+ -- | Find the kind of an expression
11092 | KindOf P.Type
111− -- |
112− -- Shows information about the current state of the REPL
113− --
93+ -- | Shows information about the current state of the REPL
11494 | ShowInfo ReplQuery
95+ -- | Paste multiple lines
96+ | PasteLines
11597
11698data ReplQuery
11799 = QueryLoaded
@@ -142,4 +124,5 @@ data Directive
142124 | Type
143125 | Kind
144126 | Show
127+ | Paste
145128 deriving (Eq, Show)