@@ -7,11 +7,7 @@ import Data.List.NonEmpty qualified as NE
77import Data.Tagged (Tagged (.. ))
88import Data.Text (splitOn )
99import Data.Text qualified as Text
10- import Language.PureScript.Backend
11- ( AppEntryPoint (.. )
12- , AppOrModule (.. )
13- , ModuleEntryPoint (.. )
14- )
10+ import Language.PureScript.Backend.Types (AppOrModule (.. ))
1511import Language.PureScript.Names qualified as PS
1612import Options.Applicative
1713 ( Parser
@@ -29,21 +25,24 @@ import Options.Applicative
2925 , short
3026 , value
3127 )
32- import Options.Applicative.Help.Pretty
3328import Path (reldir , relfile )
3429import Path.Posix (Dir , File , SomeBase (.. ), parseSomeDir , parseSomeFile )
30+ import Prettyprinter (Doc , annotate , flatAlt , indent , line , vsep , (<+>) )
31+ import Prettyprinter qualified as PP
32+ import Prettyprinter.Render.Terminal (AnsiStyle , Color (.. ))
33+ import Prettyprinter.Render.Terminal qualified as PT
3534
3635data Args = Args
37- { foreignPath :: Tagged " foreign" (SomeBase Dir )
38- , psOutputPath :: Tagged " output" (SomeBase Dir )
39- , luaOutputFile :: Tagged " output-lua" (SomeBase File )
40- , appOrModule :: AppOrModule
36+ { foreignPath ∷ Tagged " foreign" (SomeBase Dir )
37+ , psOutputPath ∷ Tagged " output" (SomeBase Dir )
38+ , luaOutputFile ∷ Tagged " output-lua" (SomeBase File )
39+ , appOrModule ∷ AppOrModule
4140 }
4241 deriving stock (Show )
4342
44- options :: Parser Args
43+ options ∷ Parser Args
4544options = do
46- foreignPath <-
45+ foreignPath ←
4746 option
4847 (eitherReader (bimap displayException Tagged . parseSomeDir))
4948 ( fold
@@ -52,10 +51,11 @@ options = do
5251 , value $ Tagged $ Rel [reldir |foreign|]
5352 , helpDoc . Just $
5453 " Path to a directory containing foreign files."
55- <$$> bold " Default: foreign"
54+ <> linebreak
55+ <> bold " Default: foreign"
5656 ]
5757 )
58- psOutputPath <-
58+ psOutputPath ←
5959 option
6060 (eitherReader (bimap displayException Tagged . parseSomeDir))
6161 ( fold
@@ -64,10 +64,11 @@ options = do
6464 , value $ Tagged $ Rel [reldir |output|]
6565 , helpDoc . Just $
6666 " Path to purs output directory."
67- <$$> bold " Default: output"
67+ <> linebreak
68+ <> bold " Default: output"
6869 ]
6970 )
70- luaOutputFile <-
71+ luaOutputFile ←
7172 option
7273 (eitherReader (bimap displayException Tagged . parseSomeFile))
7374 ( fold
@@ -76,20 +77,21 @@ options = do
7677 , value $ Tagged $ Rel [relfile |main.lua|]
7778 , helpDoc . Just $
7879 " Path to write compiled Lua file to."
79- <$$> bold " Default: main.lua"
80+ <> linebreak
81+ <> bold " Default: main.lua"
8082 ]
8183 )
82- appOrModule <-
84+ appOrModule ←
8385 option (eitherReader parseAppOrModule) . fold $
8486 [ metavar " ENTRY"
8587 , short ' e'
8688 , long " entry"
87- , value . AsApplication $
88- AppEntryPoint (PS. ModuleName " Main" ) (PS. Ident " main" )
89+ , value $ AsApplication (PS. ModuleName " Main" ) (PS. Ident " main" )
8990 , helpDoc . Just $
9091 vsep
9192 [ " Where to start compilation."
92- <//> " Could be one of the following formats:"
93+ <> softbreak
94+ <> " Could be one of the following formats:"
9395 , " - Application format:" <+> magenta " <Module>.<binding>"
9496 , green $ indent 2 " Example: Acme.App.main"
9597 , " - Module format:" <+> magenta " <Module>"
@@ -99,27 +101,22 @@ options = do
99101 ]
100102 pure Args {.. }
101103
102- parseAppOrModule :: String -> Either String AppOrModule
104+ parseAppOrModule ∷ String → Either String AppOrModule
103105parseAppOrModule s = case splitOn " ." (toText s) of
104- [] -> Left " Invalid entry point format"
105- [name]
106- | isModule name ->
107- pure . AsModule . ModuleEntryPoint $ PS. ModuleName name
108- segments -> do
106+ [] → Left " Invalid entry point format"
107+ [name] | isModule name → pure . AsModule $ PS. ModuleName name
108+ segments → do
109109 let name = last (NE. fromList segments)
110110 pure
111111 if isModule name
112- then
113- AsModule . ModuleEntryPoint . PS. ModuleName $
114- Text. intercalate " ." segments
112+ then AsModule . PS. ModuleName $ Text. intercalate " ." segments
115113 else
116- AsApplication $
117- let modname = Text. intercalate " ." (init (NE. fromList segments))
118- in AppEntryPoint (PS. ModuleName modname) (PS. Ident name)
114+ let modname = Text. intercalate " ." (init (NE. fromList segments))
115+ in AsApplication (PS. ModuleName modname) (PS. Ident name)
119116 where
120117 isModule = Char. isAsciiUpper . Text. head
121118
122- parseArguments :: IO Args
119+ parseArguments ∷ IO Args
123120parseArguments =
124121 execParser $
125122 info
@@ -128,3 +125,21 @@ parseArguments =
128125 <> progDesc " Compile PureScript's CoreFn to Lua"
129126 <> header " pslua - a PureScript backend for Lua"
130127 )
128+
129+ --------------------------------------------------------------------------------
130+ -- Helpers for pretty-printing -------------------------------------------------
131+
132+ linebreak ∷ Doc AnsiStyle
133+ linebreak = flatAlt line mempty
134+
135+ softbreak ∷ Doc AnsiStyle
136+ softbreak = PP. group linebreak
137+
138+ green ∷ Doc AnsiStyle → Doc AnsiStyle
139+ green = annotate (PT. color Green )
140+
141+ magenta ∷ Doc AnsiStyle → Doc AnsiStyle
142+ magenta = annotate (PT. color Magenta )
143+
144+ bold ∷ Doc AnsiStyle → Doc AnsiStyle
145+ bold = annotate PT. bold
0 commit comments