1 {-# LANGUAGE InstanceSigs #-}
2 {-# LANGUAGE OverloadedStrings #-}
3 {-# LANGUAGE UndecidableInstances #-} -- for From (Word *) d
4 module Symantic.CLI.Help where
6 import Control.Applicative (Applicative(..))
8 import Data.Foldable (null)
9 import Data.Function (($), (.))
10 import Data.Functor (Functor(..), (<$>))
11 import Data.Maybe (Maybe(..), maybe, isJust)
12 import Data.Monoid (Monoid(..))
13 import Data.Semigroup (Semigroup(..))
14 import Text.Show (Show(..))
15 import Data.Tree as Tree
16 import qualified Symantic.Document as Doc
18 import Symantic.CLI.API
19 import Symantic.CLI.Schema as Schema
24 { help_result :: HelpInh d -> HelpResult d
25 -- ^ The 'HelpResult' of the current symantic.
26 , help_schema :: Schema d f k
27 -- ^ The 'Schema' of the current symantic.
30 runHelp :: Docable d => HelpInh d -> Help d f k -> d
31 runHelp def (Help h _p) = runHelpNodes def (h def) <> Doc.newline
33 docHelp :: Docable d => Doc.Indentable d => Docable d => Help d f k -> d
34 docHelp = runHelp defHelpInh
36 coerceHelp :: Help d f k -> Help d f' k'
37 coerceHelp Help{help_schema, ..} = Help
38 { help_schema = Schema.coerceSchema help_schema
43 -- | Configuration inherited top-down.
46 { helpInh_message :: !(Maybe d)
47 -- ^ The message inherited from 'help's.
48 , helpInh_command_indent :: !Doc.Indent
49 -- ^ 'Doc.Indent'ation for 'command's.
50 , helpInh_tag_indent :: !Doc.Indent
51 -- ^ 'Doc.Indent'ation for 'Tag's.
52 , helpInh_schema :: !(SchemaInh d)
53 -- ^ The inherited 'SchemaInh' for 'runSchema'.
54 , helpInh_helpless_options :: !Bool
55 -- ^ Whether to include options without help in the listing.
56 , helpInh_command_rule :: !Bool
57 -- ^ Whether to print the name of the rule.
58 , helpInh_full :: !Bool
59 -- ^ Whether to print full help.
62 defHelpInh :: Docable d => HelpInh d
64 { helpInh_message = Nothing
65 , helpInh_command_indent = 2
66 , helpInh_tag_indent = 16
67 , helpInh_schema = defSchemaInh
68 , helpInh_helpless_options = False
69 , helpInh_command_rule = False
73 -- ** Type 'HelpResult'
74 type HelpResult d = Tree.Forest (HelpNode, d)
76 defHelpResult :: Monoid d => HelpResult d
77 defHelpResult = mempty
79 -- *** Type 'HelpNode'
91 Tree (HelpNode, d) -> d
92 runHelpNode (Tree.Node (n,d) _ts) = d -- "[" <> Doc.stringH (show n) <> "]" <> d
94 -- | Introduce 'Doc.newline' according to the 'HelpNode's
95 -- put next to each others.
100 Tree.Forest (HelpNode, d) -> d
101 runHelpNodes inh [] = mempty
102 runHelpNodes inh ( t0@(Tree.Node _ t0s)
103 : t1@(Tree.Node (HelpNode_Command, _) _) : ts ) =
106 (if null t0s || not (helpInh_full inh) then mempty else Doc.newline) <>
107 runHelpNodes inh (t1:ts)
108 runHelpNodes inh ( t0@(Tree.Node (HelpNode_Tag, _) _)
109 : t1@(Tree.Node (HelpNode_Tag, _) _) : ts ) =
112 runHelpNodes inh (t1:ts)
113 runHelpNodes inh ( t0@(Tree.Node (HelpNode_Rule, _) t0s)
114 : t1@(Tree.Node (HelpNode_Env, _) _) : ts ) =
117 (if null t0s || not (helpInh_full inh) then mempty else Doc.newline) <>
118 runHelpNodes inh (t1:ts)
119 runHelpNodes inh ( t0@(Tree.Node (HelpNode_Env, _) _)
120 : t1@(Tree.Node (HelpNode_Env, _) _) : ts ) =
123 runHelpNodes inh (t1:ts)
124 runHelpNodes inh ( t0 : t1@(Tree.Node (HelpNode_Tag, _) _) : ts ) =
128 runHelpNodes inh (t1:ts)
129 runHelpNodes inh ( t0 : t1@(Tree.Node (HelpNode_Env, _) _) : ts ) =
133 runHelpNodes inh (t1:ts)
134 runHelpNodes inh ( t0@(Tree.Node (HelpNode_Message, _) _)
139 runHelpNodes inh (t1:ts)
140 runHelpNodes inh (t:ts) = runHelpNode t <> runHelpNodes inh ts
142 instance Semigroup d => Semigroup (Help d f k) where
143 Help hx px <> Help hy py = Help (hx<>hy) (px<>py)
144 instance Monoid d => Monoid (Help d f k) where
145 mempty = Help mempty mempty
148 instance (Semigroup d, IsString d) => IsString (Help d e s a) where
149 fromString "" = Help $ \_ro -> Nothing
150 fromString s = Help $ \_ro -> Just $ fromString s
151 instance Show (Help Doc.Term e s a) where
152 show = TL.unpack . Doc.textTerm . runHelp
153 instance Docable d => Functor (Help d f) where
154 f <$$> Help h s = Help h (f<$$>s)
156 instance Docable d => App (Help d) where
157 Help hf pf <.> Help hx px = Help (hf<>hx) (pf<.>px)
158 instance Docable d => Alt (Help d) where
159 Help hl pl <!> Help hr pr = Help (hl<>hr) (pl<!>pr)
160 opt (Help h s) = Help h (opt s)
162 try (Help h s) = Help h (try s)
163 choice hs = Help (mconcat $ help_result <$> hs) (choice (help_schema <$> hs))
164 option a (Help h s) = Help h (option a s)
166 instance Docable d => Permutable (Help d) where
167 type Permutation (Help d) = HelpPerm d
168 runPermutation (HelpPerm h s) = Help h $ runPermutation s
169 toPermutation (Help h s) = HelpPerm h $ toPermutation s
170 toPermDefault a (Help h s) = HelpPerm h $ toPermDefault a s
171 instance Pro (Help d) where
172 dimap a2b b2a (Help h s) = Help h $ dimap a2b b2a s
173 instance Docable d => AltApp (Help d) where
174 many0 (Help h s) = Help h (many0 s)
175 many1 (Help h s) = Help h (many1 s)
176 instance Docable d => CLI_Var (Help d) where
177 type VarConstraint (Help d) a = ()
178 var' n = Help mempty (var' n)
179 just a = Help mempty (just a)
180 nothing = Help mempty nothing
181 instance Docable d => CLI_Env (Help d) where
182 type EnvConstraint (Help d) a = ()
187 then maybe [] (pure . pure . (HelpNode_Message,)) (helpInh_message inh)
190 Doc.breakfill (helpInh_tag_indent inh)
191 (Doc.bold (Doc.green (Doc.from (Doc.Word n)))
193 <> (if null ts then mempty else Doc.space)
194 <> Doc.align (runHelpNodes inh ts)
196 [ Tree.Node (HelpNode_Env, d) ts ]
198 where schema = env' n
199 instance Docable d => CLI_Command (Help d) where
200 -- type CommandConstraint (Help d) a = ()
201 command n (Help h s) =
205 then maybe [] (pure . pure . (HelpNode_Message,)) (helpInh_message inh)
208 { helpInh_message = Nothing
209 , helpInh_command_rule = True
212 (if not (null n) && helpInh_command_rule inh
213 then ref<>Doc.space<>"::= " else mempty)
214 <> Schema.runSchema schema (helpInh_schema inh)
215 <> (if null ts || not (helpInh_full inh) then mempty else Doc.newline)
216 <> Doc.incrIndent (helpInh_command_indent inh)
217 ((if null ts then mempty else Doc.newline) <> runHelpNodes inh ts)
219 [ Tree.Node (HelpNode_Command, d) ts ]
227 Doc.from (Doc.Word n)
228 instance Docable d => CLI_Tag (Help d) where
229 type TagConstraint (Help d) a = ()
230 tagged n (Help h s) =
232 if (isJust (helpInh_message inh)
233 || helpInh_helpless_options inh)
237 maybe [] (pure . pure . (HelpNode_Message,)) (helpInh_message inh) <>
238 h inh{helpInh_message=Nothing} in
240 Doc.breakfill (helpInh_tag_indent inh)
242 Schema.runSchema schema (helpInh_schema inh)
243 <> Doc.space) -- FIXME: space is not always needed
244 <> (if null ts then mempty else Doc.space)
245 <> Doc.align (runHelpNodes inh ts)
247 [ Tree.Node (HelpNode_Tag, d) ts ]
250 where schema = tagged n s
251 endOpts = Help mempty endOpts
252 instance Docable d => CLI_Help (Help d) where
253 type HelpConstraint (Help d) d' = d ~ d'
254 help msg (Help h s) = Help
255 (\inh -> h inh{helpInh_message=Just msg})
257 program n (Help h s) =
261 then maybe [] (pure . pure . (HelpNode_Message,)) (helpInh_message inh)
264 { helpInh_message = Nothing
265 , helpInh_command_rule = True
268 Schema.runSchema schema (helpInh_schema inh)
269 <> (if null ts {-|| not (helpInh_full inh)-} then mempty else Doc.newline)
271 (helpInh_command_indent inh)
272 ((if null ts then mempty else Doc.newline) <> runHelpNodes inh ts)
274 [ Tree.Node (HelpNode_Rule, d) ts ]
282 then maybe [] (pure . pure . (HelpNode_Message,)) (helpInh_message inh)
285 { helpInh_message = Nothing
286 , helpInh_command_rule = True
289 ref<>Doc.space<>"::= "
290 <> Schema.runSchema schema (helpInh_schema inh)
291 <> (if null ts || not (helpInh_full inh) then mempty else Doc.newline)
293 (helpInh_command_indent inh)
294 ((if null ts then mempty else Doc.newline) <> runHelpNodes inh ts)
296 [ Tree.Node (HelpNode_Rule, d) ts ]
304 Doc.from (Doc.Word n)
305 type HelpResponseArgs = SchemaResponseArgs
306 instance Docable d => CLI_Response (Help d) where
307 type ResponseConstraint (Help d) a = () -- ResponseConstraint (Schema d)
308 type ResponseArgs (Help d) a = SchemaResponseArgs a -- ResponseArgs (Schema d) a
309 type Response (Help d) = () -- Response (Schema d)
313 ResponseConstraint repr a =>
314 repr (ResponseArgs repr a)
316 response' = Help mempty $ response' @(Schema d) @a
319 instance Docable d => Sym_AltApp (Help d) where
320 many (Help h s) = Help h (many s)
321 some (Help h s) = Help h (many s)
326 = HelpPerm (HelpInh d -> HelpResult d)
328 instance Functor (HelpPerm d k) where
329 f`fmap`HelpPerm h ps = HelpPerm h (f<$>ps)
330 instance Applicative (HelpPerm d k) where
331 pure a = HelpPerm mempty (pure a)
332 HelpPerm fh f <*> HelpPerm xh x =
333 HelpPerm (fh<>xh) (f<*>x)
334 instance Docable d => CLI_Help (HelpPerm d) where
335 type HelpConstraint (HelpPerm d) d' = d ~ d'
336 help msg (HelpPerm h s) = HelpPerm
337 (\inh -> h inh{helpInh_message=Just msg})
339 program n (HelpPerm h s) = HelpPerm
340 (help_result $ program n (Help h (runPermutation s)))
342 rule n (HelpPerm h s) = HelpPerm
343 (help_result $ rule n (Help h (runPermutation s)))