on master help sign in
Add "elm repl --no-colors" flag
19d5a76Evan Czaplicki
builder/src/Elm/Project.hs+3−3
@@ -71,15 +71,15 @@ compile mode target maybeOutput docs summary@(Summary.Summary root project _ _ _
71-- COMPILE FOR REPL
71-- COMPILE FOR REPL
72
72
73
73
74−compileForRepl :: L.Localizer -> BS.ByteString -> Maybe N.Name -> Task.Task (Maybe FilePath)
74+compileForRepl :: Bool -> L.Localizer -> BS.ByteString -> Maybe N.Name -> Task.Task (Maybe FilePath)
75−compileForRepl localizer source maybeName =
75+compileForRepl noColors localizer source maybeName =
76 do summary@(Summary.Summary root project _ _ _) <- getRoot
76 do summary@(Summary.Summary root project _ _ _) <- getRoot
77 graph <- Crawl.crawlFromSource summary source
77 graph <- Crawl.crawlFromSource summary source
78 (dirty, ifaces) <- Plan.plan Nothing summary graph
78 (dirty, ifaces) <- Plan.plan Nothing summary graph
79 answers <- Compile.compile project Nothing ifaces dirty
79 answers <- Compile.compile project Nothing ifaces dirty
80 results <- Artifacts.write root answers
80 results <- Artifacts.write root answers
81 let (Compiler.Artifacts elmi _ _) = results ! N.replModule
81 let (Compiler.Artifacts elmi _ _) = results ! N.replModule
82− traverse (Output.generateReplFile localizer summary graph elmi) maybeName
82+ traverse (Output.generateReplFile noColors localizer summary graph elmi) maybeName
83
83
84
84
85
85
builder/src/Generate/Output.hs+4−3
@@ -145,18 +145,19 @@ generateMonolith mode maybeOutput (Summary.Summary _ project _ _ _) graph rootNa
145
145
146
146
147generateReplFile
147generateReplFile
148+ :: Bool
148− :: L.Localizer
149+ -> L.Localizer
149 -> Summary.Summary
150 -> Summary.Summary
150 -> Crawl.Result
151 -> Crawl.Result
151 -> I.Interface
152 -> I.Interface
152 -> N.Name
153 -> N.Name
153 -> Task.Task FilePath
154 -> Task.Task FilePath
154−generateReplFile localizer summary@(Summary.Summary _ project _ _ _) graph iface name =
155+generateReplFile noColors localizer summary@(Summary.Summary _ project _ _ _) graph iface name =
155 do
156 do
156 objectGraph <- organize summary graph
157 objectGraph <- organize summary graph
157
158
158 let home = Module.Canonical (Project.getName project) N.replModule
159 let home = Module.Canonical (Project.getName project) N.replModule
159− let builder = Obj.generateForRepl True localizer objectGraph iface home name
160+ let builder = Obj.generateForRepl (not noColors) localizer objectGraph iface home name
160
161
161 liftIO $ IO.writeBuilder (Paths.temp "js") $
162 liftIO $ IO.writeBuilder (Paths.temp "js") $
162 replRecovery <> "(function(){\n'use strict';" <> Functions.functions <> builder <> "}());"
163 replRecovery <> "(function(){\n'use strict';" <> Functions.functions <> builder <> "}());"
ui/terminal/src/Main.hs+1
@@ -93,6 +93,7 @@ repl =
93 replFlags =
93 replFlags =
94 flags Repl.Flags
94 flags Repl.Flags
95 |-- flag "interpreter" interpreter "Path to a alternate JS interpreter, like node or nodejs."
95 |-- flag "interpreter" interpreter "Path to a alternate JS interpreter, like node or nodejs."
96+ |-- onOff "no-colors" "Turn off the colors in the REPL. This can help if you are having trouble reading the values. Some terminals use a custom color scheme that diverges significantly from the standard ANSI colors, so another path may be to pick a more standard color scheme."
96 in
97 in
97 Interface "repl" (Common summary) details example noArgs replFlags Repl.run
98 Interface "repl" (Common summary) details example noArgs replFlags Repl.run
98
99
ui/terminal/src/Repl.hs+27−27
@@ -44,22 +44,23 @@ import qualified Reporting.Render.Type.Localizer as L
44data Flags =
44data Flags =
45 Flags
45 Flags
46 { _interpreter :: Maybe FilePath
46 { _interpreter :: Maybe FilePath
47+ , _noColors :: Bool
47 }
48 }
48
49
49
50
50run :: () -> Flags -> IO ()
51run :: () -> Flags -> IO ()
51−run () (Flags maybeAlternateInterpreter) =
52+run () (Flags maybeAlternateInterpreter noColors) =
52 do interpreter <- getInterpreter maybeAlternateInterpreter
53 do interpreter <- getInterpreter maybeAlternateInterpreter
53 _ <- Project.getRootWithReplFallback
54 _ <- Project.getRootWithReplFallback
54 printWelcomeMessage
55 printWelcomeMessage
55 settings <- initSettings
56 settings <- initSettings
56 let manager = Repl.runInputT settings (Repl.withInterrupt loop)
57 let manager = Repl.runInputT settings (Repl.withInterrupt loop)
57− (exitCode,_,_) <- RWS.runRWST manager interpreter initialState
58+ (exitCode,_,_) <- RWS.runRWST manager (interpreter, noColors) initialState
58 Exit.exitWith exitCode
59 Exit.exitWith exitCode
59
60
60
61
61type Runner =
62type Runner =
62− RWS.RWST FilePath () State IO
63+ RWS.RWST (FilePath, Bool) () State IO
63
64
64
65
65
66
@@ -155,7 +156,8 @@ data Output = None | Def N.Name | Expr Text.Text
155
156
156interpret :: Output -> State -> Runner (Maybe a)
157interpret :: Output -> State -> Runner (Maybe a)
157interpret def state =
158interpret def state =
159+ do noColors <- RWS.asks snd
158− do result <- liftIO $ Task.try Repl.reporter $ compile def state
160+ result <- liftIO $ Task.try Repl.reporter $ compile noColors def state
159
161
160 case result of
162 case result of
161 Nothing ->
163 Nothing ->
@@ -170,7 +172,7 @@ interpret def state =
170
172
171interpretHelp :: FilePath -> Runner ()
173interpretHelp :: FilePath -> Runner ()
172interpretHelp path =
174interpretHelp path =
173− do interpreter <- RWS.ask
175+ do interpreter <- RWS.asks fst
174 let proc = (Proc.proc interpreter [path]) { Proc.std_in = Proc.CreatePipe }
176 let proc = (Proc.proc interpreter [path]) { Proc.std_in = Proc.CreatePipe }
175 liftIO $ Proc.withCreateProcess proc $ \_ _ _ handle ->
177 liftIO $ Proc.withCreateProcess proc $ \_ _ _ handle ->
176 do _ <- Proc.waitForProcess handle
178 do _ <- Proc.waitForProcess handle
@@ -231,8 +233,8 @@ initialState =
231 State 0 Map.empty Map.empty Map.empty [] L.replEmpty
233 State 0 Map.empty Map.empty Map.empty [] L.replEmpty
232
234
233
235
234−compile :: Output -> State -> Task.Task (Maybe FilePath)
236+compile :: Bool -> Output -> State -> Task.Task (Maybe FilePath)
235−compile output (State count imports types defs tricks localizer) =
237+compile noColors output (State count imports types defs tricks localizer) =
236 let
238 let
237 header =
239 header =
238 "module " <> N.toBuilder N.replModule <> " exposing (..)\n\n"
240 "module " <> N.toBuilder N.replModule <> " exposing (..)\n\n"
@@ -241,24 +243,27 @@ compile output (State count imports types defs tricks localizer) =
241 (<>) header $
243 (<>) header $
242 addLines imports $ addLines types $ addLines defs $
244 addLines imports $ addLines types $ addLines defs $
243 List.foldr (<>) mempty (map Text.encodeUtf8Builder tricks)
245 List.foldr (<>) mempty (map Text.encodeUtf8Builder tricks)
246+
247+ compileForRepl builder maybeName =
248+ Project.compileForRepl noColors localizer (LBS.toStrict (B.toLazyByteString builder)) maybeName
244 in
249 in
245− case output of
250+ case output of
246− None ->
251+ None ->
247− if Map.null types && Map.null defs && null tricks then
252+ if Map.null types && Map.null defs && null tricks then
248− evaluate localizer (elmSourceCode <> "unit = ()") Nothing
253+ compileForRepl (elmSourceCode <> "unit = ()") Nothing
249− else
254+ else
250− evaluate localizer elmSourceCode Nothing
255+ compileForRepl elmSourceCode Nothing
251
256
252− Def name ->
257+ Def name ->
253− evaluate localizer elmSourceCode (Just name)
258+ compileForRepl elmSourceCode (Just name)
254
259
255− Expr expr ->
260+ Expr expr ->
256− let
261+ let
257− name = N.addIndex "repl_value_" count
262+ name = N.addIndex "repl_value_" count
258− text = Text.replace "\n" "\n " expr
263+ text = Text.replace "\n" "\n " expr
259− def = N.toBuilder name <> " =\n " <> Text.encodeUtf8Builder text <> "\n"
264+ def = N.toBuilder name <> " =\n " <> Text.encodeUtf8Builder text <> "\n"
260− in
265+ in
261− evaluate localizer (elmSourceCode <> def) (Just name)
266+ compileForRepl (elmSourceCode <> def) (Just name)
262
267
263
268
264addLines :: Map.Map N.Name Text.Text -> B.Builder -> B.Builder
269addLines :: Map.Map N.Name Text.Text -> B.Builder -> B.Builder
@@ -266,11 +271,6 @@ addLines dict builder =
266 Map.foldr (\text b -> Text.encodeUtf8Builder text <> "\n" <> b) builder dict
271 Map.foldr (\text b -> Text.encodeUtf8Builder text <> "\n" <> b) builder dict
267
272
268
273
269−evaluate :: L.Localizer -> B.Builder -> Maybe N.Name -> Task.Task (Maybe FilePath)
270−evaluate localizer builder maybeName =
271− Project.compileForRepl localizer (LBS.toStrict (B.toLazyByteString builder)) maybeName
272−
273−
274
274
275-- SETTINGS
275-- SETTINGS
276
276