git.iko.soy/rowtype-yoga/purescript-native
on golang help sign in
Add paste mode, remove --multi-line option, fix #934 (#2361)
0072f25Phil Freeman
psci/Main.hs+29−22
@@ -63,17 +63,10 @@ import System.Process (readProcessWithExitCode)
63
63
64-- | Command line options
64-- | Command line options
65data PSCiOptions = PSCiOptions
65data PSCiOptions = PSCiOptions
66− { psciMultiLineMode :: Bool
67− , psciInputFile :: [FilePath]
66+ { psciInputFile :: [FilePath]
68 , psciBackend :: Backend
67 , psciBackend :: Backend
69 }
68 }
70
69
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−
77inputFile :: Opts.Parser FilePath
70inputFile :: Opts.Parser FilePath
78inputFile = Opts.strArgument $
71inputFile = Opts.strArgument $
79 Opts.metavar "FILE"
72 Opts.metavar "FILE"
@@ -100,8 +93,7 @@ backend =
100 <|> (nodeBackend <$> nodeFlagsFlag)
93 <|> (nodeBackend <$> nodeFlagsFlag)
101
94
102psciOptions :: Opts.Parser PSCiOptions
95psciOptions :: Opts.Parser PSCiOptions
103−psciOptions = PSCiOptions <$> multiLineMode
96+psciOptions = PSCiOptions <$> many inputFile
104− <*> many inputFile
105 <*> backend
97 <*> backend
106
98
107version :: Opts.Parser (a -> a)
99version :: Opts.Parser (a -> a)
@@ -119,17 +111,20 @@ getOpt = Opts.execParser opts
119 footerInfo = Opts.footer $ "psci " ++ showVersion Paths.version
111 footerInfo = Opts.footer $ "psci " ++ showVersion Paths.version
120
112
121-- | Parses the input and returns either a command, or an error as a 'String'.
113-- | 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))
114+getCommand :: forall m. MonadException m => InputT m (Either String (Maybe Command))
123−getCommand singleLineMode = handleInterrupt (return (Right Nothing)) $ do
115+getCommand = handleInterrupt (return (Right Nothing)) $ do
124− firstLine <- withInterrupt $ getInputLine "> "
116+ line <- withInterrupt $ getInputLine "> "
125− case firstLine of
117+ case line of
126 Nothing -> return (Right (Just QuitPSCi)) -- Ctrl-D when input is empty
118 Nothing -> return (Right (Just QuitPSCi)) -- Ctrl-D when input is empty
127 Just "" -> return (Right Nothing)
119 Just "" -> return (Right Nothing)
128− Just s | singleLineMode || head s == ':' -> return . fmap Just $ parseCommand s
120+ Just s -> return . fmap Just $ parseCommand s
121+
122+pasteMode :: forall m. MonadException m => InputT m (Either String Command)
123+pasteMode =
129− Just s -> fmap Just . parseCommand <$> go [s]
124+ parseCommand <$> go []
130 where
125 where
131 go :: [String] -> InputT m String
126 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 "… "
133
128
134-- | Make a JavaScript bundle for the browser.
129-- | Make a JavaScript bundle for the browser.
135bundle :: IO (Either Bundle.ErrorMessage String)
130bundle :: IO (Either Bundle.ErrorMessage String)
@@ -349,16 +344,28 @@ main = getOpt >>= loop
349
344
350 go :: state -> InputT (StateT PSCiState (ReaderT PSCiConfig IO)) ()
345 go :: state -> InputT (StateT PSCiState (ReaderT PSCiConfig IO)) ()
351 go state = do
346 go state = do
352− c <- getCommand (not psciMultiLineMode)
347+ c <- getCommand
353 case c of
348 case c of
354 Left err -> outputStrLn err >> go state
349 Left err -> outputStrLn err >> go state
355 Right Nothing -> go state
350 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''
356 Right (Just QuitPSCi) -> do
356 Right (Just QuitPSCi) -> do
357 outputStrLn quitMessage
357 outputStrLn quitMessage
358 liftIO $ shutdown state
358 liftIO $ shutdown state
359− Right (Just c') -> do
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
360− handleInterrupt (outputStrLn "Interrupted.")
366+ handleInterrupt (outputStrLn "Interrupted.")
361− (withInterrupt (lift (handleCommand (liftIO . eval state) (liftIO (reload state)) c')))
367+ (withInterrupt (lift (handleCommand (liftIO . eval state) (liftIO (reload state)) cmd)))
362− go state
368+ go state
369+
363 putStrLn prologueMessage
370 putStrLn prologueMessage
364 setup >>= runner . go
371 setup >>= runner . go
src/Language/PureScript/Interactive.hs+1−1
@@ -104,7 +104,7 @@ handleCommand _ _ (KindOf typ) = handleKindOf typ
104handleCommand _ _ (BrowseModule moduleName) = handleBrowse moduleName
104handleCommand _ _ (BrowseModule moduleName) = handleBrowse moduleName
105handleCommand _ _ (ShowInfo QueryLoaded) = handleShowLoadedModules
105handleCommand _ _ (ShowInfo QueryLoaded) = handleShowLoadedModules
106handleCommand _ _ (ShowInfo QueryImport) = handleShowImportedModules
106handleCommand _ _ (ShowInfo QueryImport) = handleShowImportedModules
107−handleCommand _ _ QuitPSCi = P.internalError "`handleCommand QuitPSCi` was called. This is a bug."
107+handleCommand _ _ _ = P.internalError "handleCommand: unexpected command"
108
108
109-- | Reset the application state
109-- | Reset the application state
110handleResetState
110handleResetState
src/Language/PureScript/Interactive/Completion.hs+1
@@ -122,6 +122,7 @@ directiveArg _ Browse = [CtxModule]
122directiveArg _ Quit = []
122directiveArg _ Quit = []
123directiveArg _ Reset = []
123directiveArg _ Reset = []
124directiveArg _ Help = []
124directiveArg _ Help = []
125+directiveArg _ Paste = []
125directiveArg _ Show = map CtxFixed replQueryStrings
126directiveArg _ Show = map CtxFixed replQueryStrings
126directiveArg _ Type = [CtxIdentifier]
127directiveArg _ Type = [CtxIdentifier]
127directiveArg _ Kind = [CtxType]
128directiveArg _ Kind = [CtxType]
src/Language/PureScript/Interactive/Directive.hs+3
@@ -30,6 +30,7 @@ directiveStrings =
30 , (Type , ["type"])
30 , (Type , ["type"])
31 , (Kind , ["kind"])
31 , (Kind , ["kind"])
32 , (Show , ["show"])
32 , (Show , ["show"])
33+ , (Paste , ["paste"])
33 ]
34 ]
34
35
35-- |
36-- |
@@ -82,6 +83,7 @@ hasArgument :: Directive -> Bool
82hasArgument Help = False
83hasArgument Help = False
83hasArgument Quit = False
84hasArgument Quit = False
84hasArgument Reset = False
85hasArgument Reset = False
86+hasArgument Paste = False
85hasArgument _ = True
87hasArgument _ = True
86
88
87-- |
89-- |
@@ -97,4 +99,5 @@ help =
97 , (Kind, "<type>", "Show the kind of <type>")
99 , (Kind, "<type>", "Show the kind of <type>")
98 , (Show, "import", "Show all imported modules")
100 , (Show, "import", "Show all imported modules")
99 , (Show, "loaded", "Show all loaded modules")
101 , (Show, "loaded", "Show all loaded modules")
102+ , (Paste, "paste", "Enter multiple lines, terminated by ^D")
100 ]
103 ]
src/Language/PureScript/Interactive/Parser.hs+1
@@ -62,6 +62,7 @@ parseDirective cmd =
62 Help -> return ShowHelp
62 Help -> return ShowHelp
63 Quit -> return QuitPSCi
63 Quit -> return QuitPSCi
64 Reset -> return ResetState
64 Reset -> return ResetState
65+ Paste -> return PasteLines
65 Browse -> BrowseModule <$> parseRest P.moduleName arg
66 Browse -> BrowseModule <$> parseRest P.moduleName arg
66 Show -> ShowInfo <$> parseReplQuery' (trim arg)
67 Show -> ShowInfo <$> parseReplQuery' (trim arg)
67 Type -> TypeOf <$> parseRest P.parseValue arg
68 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) }
72-- Valid Meta-commands for PSCI
72-- Valid Meta-commands for PSCI
73--
73--
74data Command
74data Command
75− -- |
76− -- A purescript expression
75+ -- | A purescript expression
77− --
78 = Expression P.Expr
76 = Expression P.Expr
79− -- |
80− -- Show the help (ie, list of directives)
77+ -- | Show the help (ie, list of directives)
81− --
82 | ShowHelp
78 | ShowHelp
83− -- |
84− -- Import a module from a loaded file
79+ -- | Import a module from a loaded file
85− --
86 | Import ImportedModule
80 | Import ImportedModule
87− -- |
88− -- Browse a module
81+ -- | Browse a module
89− --
90 | BrowseModule P.ModuleName
82 | BrowseModule P.ModuleName
91− -- |
92− -- Exit PSCI
83+ -- | Exit PSCI
93− --
94 | QuitPSCi
84 | QuitPSCi
95− -- |
96− -- Reset the state of the REPL
85+ -- | Reset the state of the REPL
97− --
98 | ResetState
86 | ResetState
99− -- |
100− -- Add some declarations to the current evaluation context.
87+ -- | Add some declarations to the current evaluation context
101− --
102 | Decls [P.Declaration]
88 | Decls [P.Declaration]
103− -- |
104− -- Find the type of an expression
89+ -- | Find the type of an expression
105− --
106 | TypeOf P.Expr
90 | TypeOf P.Expr
107− -- |
108− -- Find the kind of an expression
91+ -- | Find the kind of an expression
109− --
110 | KindOf P.Type
92 | KindOf P.Type
111− -- |
112− -- Shows information about the current state of the REPL
93+ -- | Shows information about the current state of the REPL
113− --
114 | ShowInfo ReplQuery
94 | ShowInfo ReplQuery
95+ -- | Paste multiple lines
96+ | PasteLines
115
97
116data ReplQuery
98data ReplQuery
117 = QueryLoaded
99 = QueryLoaded
@@ -142,4 +124,5 @@ data Directive
142 | Type
124 | Type
143 | Kind
125 | Kind
144 | Show
126 | Show
127+ | Paste
145 deriving (Eq, Show)
128 deriving (Eq, Show)