File: Help.hs

package info (click to toggle)
darcs 2.12.4-2
  • links: PTS, VCS
  • area: main
  • in suites: stretch
  • size: 5,748 kB
  • sloc: haskell: 42,936; sh: 11,086; ansic: 837; perl: 129; makefile: 8
file content (419 lines) | stat: -rw-r--r-- 17,058 bytes parent folder | download
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
--  Copyright (C) 2002-2004 David Roundy
--
--  This program is free software; you can redistribute it and/or modify
--  it under the terms of the GNU General Public License as published by
--  the Free Software Foundation; either version 2, or (at your option)
--  any later version.
--
--  This program is distributed in the hope that it will be useful,
--  but WITHOUT ANY WARRANTY; without even the implied warranty of
--  MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
--  GNU General Public License for more details.
--
--  You should have received a copy of the GNU General Public License
--  along with this program; see the file COPYING.  If not, write to
--  the Free Software Foundation, Inc., 51 Franklin Street, Fifth Floor,
--  Boston, MA 02110-1301, USA.

module Darcs.UI.Commands.Help (
 helpCmd,
 commandControlList, environmentHelp,          -- these are for preproc.hs
 printVersion,
 listAvailableCommands ) where

import Prelude ()
import Darcs.Prelude

import Darcs.UI.Flags
    ( DarcsFlag
    , environmentHelpEmail
    , environmentHelpSendmail
    )
import Darcs.UI.Options.Markdown ( optionsMarkdown )
import Darcs.UI.Commands
    ( CommandArgs(..)
    , CommandControl(..)
    , normalCommand
    , DarcsCommand(..), withStdOpts
    , WrappedCommand(..)
    , wrappedCommandName
    , disambiguateCommands
    , extractCommands
    , getCommandHelp
    , nodefaults
    , usage
    )
import Darcs.UI.External ( viewDoc )
import Darcs.Util.Lock ( environmentHelpTmpdir, environmentHelpKeepTmpdir
                       , environmentHelpLocks )
import Darcs.Patch.Match ( helpOnMatchers )
import Darcs.Repository.Prefs ( boringFileHelp, binariesFileHelp, environmentHelpHome )
import Darcs.Util.Ssh ( environmentHelpSsh, environmentHelpScp, environmentHelpSshPort )
import Darcs.Util.File ( withCurrentDirectory )
import Darcs.Util.Path ( AbsolutePath )
import Control.Arrow ( (***) )
import Data.Char ( isAlphaNum, toLower, toUpper )
import Data.Either ( partitionEithers )
import Data.List ( groupBy, isPrefixOf, intercalate, nub )
import Darcs.Util.English ( andClauses )
import Darcs.Util.Printer (text, vcat, vsep, ($$), empty)
import Darcs.Util.Printer.Color ( environmentHelpColor, environmentHelpEscape, environmentHelpEscapeWhite )
import System.Exit ( exitSuccess )
import Version ( version )
import Darcs.Util.Download ( environmentHelpProxy, environmentHelpProxyPassword )
import Darcs.Util.Workaround ( getCurrentDirectory )
import Darcs.UI.Options ( DarcsOption, defaultFlags, ocheck, onormalise, oid )
import qualified Darcs.UI.Options.All as O ( StdCmdAction, Verbosity, UseCache )
import qualified Darcs.UI.TheCommands as TheCommands

helpDescription :: String
helpDescription = "Display help about darcs and darcs commands."

helpHelp :: String
helpHelp =
 "Without arguments, `darcs help` prints a categorized list of darcs\n" ++
 "commands and a short description of each one.  With an extra argument,\n" ++
 "`darcs help foo` prints detailed help about the darcs command foo.\n"

argPossibilities :: [String]
argPossibilities = map wrappedCommandName $ extractCommands commandControlList

helpOpts :: DarcsOption a
            (Maybe O.StdCmdAction
             -> Bool
             -> Bool
             -> O.Verbosity
             -> Bool
             -> O.UseCache
             -> Maybe String
             -> Bool
             -> Maybe String
             -> Bool
             -> a)
helpOpts = withStdOpts oid oid

help :: DarcsCommand [DarcsFlag]
help = DarcsCommand
    { commandProgramName = "darcs"
    , commandName = "help"
    , commandHelp = helpHelp
    , commandDescription = helpDescription
    , commandExtraArgs = -1
    , commandExtraArgHelp = ["[<DARCS_COMMAND> [DARCS_SUBCOMMAND]]  "]
    , commandCommand = \ x y z -> helpCmd x y z >> exitSuccess
    , commandPrereq = \_ -> return $ Right ()
    , commandGetArgPossibilities = return argPossibilities
    , commandArgdefaults = nodefaults
    , commandAdvancedOptions = []
    , commandBasicOptions = []
    , commandDefaults = defaultFlags helpOpts
    , commandCheckOptions = ocheck helpOpts
    , commandParseOptions = onormalise helpOpts
    }

helpCmd :: (AbsolutePath, AbsolutePath) -> [DarcsFlag] -> [String] -> IO ()
helpCmd _ _ ["manpage"] = putStr $ unlines manpageLines
helpCmd _ _ ["markdown"] = putStr $ unlines markdownLines
helpCmd _ _ ["patterns"] = viewDoc $ text $ unlines helpOnMatchers
helpCmd _ _ ("environment":vs_) =
    viewDoc $ header $$
              vsep (map render known) $$
              footer
  where
    header | null known = empty
           | otherwise = text "Environment Variables" $$
                         text "====================="

    footer | null unknown = empty
           | otherwise = text "" $$
                         text ("Unknown environment variables: "
                               ++ intercalate ", " unknown)

    render (ks, ds) = text (andClauses ks ++ ":") $$
                      vcat [ text ("  " ++ d) | d <- ds ]

    (unknown, known) = case map (map toUpper) vs_ of
                           [] -> ([], environmentHelp)
                           vs -> (nub *** (nub . concat)) . partitionEithers $
                                     map doLookup vs

    -- v is not known if it doesn't appear in the list of aliases of any
    -- of the environment var help descriptions.
    doLookup v = case filter ((v `elem`) . fst) environmentHelp of
                     [] -> Left v
                     es -> Right es

helpCmd _ _ [] = viewDoc $ text $ usage commandControlList

helpCmd _ _ (cmd:args) =
    let disambiguated = disambiguateCommands commandControlList cmd args
    in case disambiguated of
         Left err -> fail err
         Right (cmds,_) ->
             let msg = case cmds of
                         CommandOnly c       -> getCommandHelp Nothing  c
                         SuperCommandOnly c  -> getCommandHelp Nothing  c
                         SuperCommandSub c s -> getCommandHelp (Just c) s
             in viewDoc $ text msg

listAvailableCommands :: IO ()
listAvailableCommands =
    do here <- getCurrentDirectory
       is_valid <- mapM
                   (\(WrappedCommand c)-> withCurrentDirectory here $ commandPrereq c [])
                   (extractCommands commandControlList)
       putStr $ unlines $ map (wrappedCommandName . fst) $
                filter (isRight.snd) $
                zip (extractCommands commandControlList) is_valid
       putStrLn "--help"
       putStrLn "--version"
       putStrLn "--exact-version"
       putStrLn "--overview"
    where isRight (Right _) = True
          isRight _ = False

printVersion :: IO ()
printVersion = putStrLn $ "darcs version " ++ version

-- avoiding a module import cycle between Help and TheCommands
commandControlList :: [CommandControl]
commandControlList =
  normalCommand help : TheCommands.commandControlList

-- FIXME: the "grouping" comments below should made subsections in the
-- manpage, as we already do for DarcsCommand groups. --twb, 2009

-- | Help on each environment variable in which Darcs is interested.
environmentHelp :: [([String], [String])]
environmentHelp = [
 -- General-purpose
 environmentHelpHome,
 environmentHelpEditor,
 environmentHelpPager,
 environmentHelpColor,
 environmentHelpEscapeWhite,
 environmentHelpEscape,
 environmentHelpTmpdir,
 environmentHelpKeepTmpdir,
 environmentHelpEmail,
 environmentHelpSendmail,
 environmentHelpLocks,
 -- Remote Repositories
 environmentHelpSsh,
 environmentHelpScp,
 environmentHelpSshPort,
 environmentHelpProxy,
 environmentHelpProxyPassword,
 environmentHelpTimeout]

-- | This module is responsible for emitting a darcs "man-page", a
-- reference document used widely on Unix-like systems.  Manpages are
-- primarily used as a quick reference, or "memory jogger", so the
-- output should be terser than the user manual.
--
-- Before modifying the output, please be sure to read the man(7) and
-- man-pages(7) manpages, as these respectively describe the relevant
-- syntax and conventions.

-- | The lines of the manpage to be printed.
manpageLines :: [String]
manpageLines = [
 ".TH DARCS 1 \"" ++ version ++ "\"",
 ".SH NAME",
 "darcs \\- an advanced revision control system",
 ".SH SYNOPSIS",
 ".B darcs", ".I command", ".RI < arguments |[ options ]>...",
 "",
 "Where the", ".I commands", "and their respective", ".I arguments", "are",
 "",
 unlines synopsis,
 ".SH DESCRIPTION",
 -- FIXME: this is copy-and-pasted from darcs.cabal, so
 -- it'll get out of date as people forget to maintain
 -- both in sync.
 "Darcs is a free, open source revision control",
 "system. It is:",
 ".TP 3", "\\(bu",
 "Distributed: Every user has access to the full",
 "command set, removing boundaries between server and",
 "client or committer and non\\(hycommitters.",
 ".TP", "\\(bu",
 "Interactive: Darcs is easy to learn and efficient to",
 "use because it asks you questions in response to",
 "simple commands, giving you choices in your work",
 "flow. You can choose to record one change in a file,",
 "while ignoring another. As you update from upstream,",
 "you can review each patch name, even the full `diff'",
 "for interesting patches.",
 ".TP", "\\(bu",
 "Smart: Originally developed by physicist David",
 "Roundy, darcs is based on a unique algebra of",
 "patches.",
 "This smartness lets you respond to changing demands",
 "in ways that would otherwise not be possible. Learn",
 "more about spontaneous branches with darcs.",
 ".SH OPTIONS",
 "Different options are accepted by different Darcs commands.",
 "Each command's most important options are listed in the",
 ".B COMMANDS",
 "section.  For a full list of all options accepted by",
 "a particular command, run `darcs", ".I command", "\\-\\-help'.",
 ".SS " ++ escape (unlines helpOnMatchers), -- FIXME: this is a kludge.
 ".SH COMMANDS",
 unlines commands,
 unlines environment,
 ".SH FILES",
 ".SS \"_darcs/prefs/binaries\"",
 escape $ unlines binariesFileHelp,
 ".SS \"_darcs/prefs/boring\"",
 escape $ unlines boringFileHelp,
 ".SH BUGS",
 "At http://bugs.darcs.net/ you can find a list of known",
 "bugs in Darcs.  Unknown bugs can be reported at that",
 "site (after creating an account) or by emailing the",
 "report to bugs@darcs.net.",
 -- ".SH EXAMPLE",
 -- FIXME:
 -- new project: init, rec -la;
 -- track upstream project: clone, pull -a;
 -- contribute to project: add, rec, push/send.
 ".SH SEE ALSO",
 "The Darcs website provides a lot of additional information.",
 "It can be found at http://darcs.net/",
 ".SH LICENSE",
 "Darcs is free software; you can redistribute it and/or modify",
 "it under the terms of the GNU General Public License as published by",
 "the Free Software Foundation; either version 2, or (at your option)",
 "any later version." ]
    where
      -- | A synopsis line for each command.  Uses 'foldl' because it is
      -- necessary to avoid blank lines from Hidden_commands, as groff
      -- translates them into annoying vertical padding (unlike TeX).
      synopsis :: [String]
      synopsis = foldl iter [] commandControlList
          where iter :: [String] -> CommandControl -> [String]
                iter acc (GroupName _) = acc
                iter acc (HiddenCommand _) = acc
                iter acc (CommandData (WrappedCommand c@SuperCommand {})) =
                    acc ++ concatMap
                            (render (commandName c ++ " "))
                            (extractCommands (commandSubCommands c))
                iter acc (CommandData c) = acc ++ render "" c
                render :: String -> WrappedCommand -> [String]
                render prefix (WrappedCommand c) =
                    [".B darcs " ++ prefix ++ commandName c] ++
                    map mangle_args (commandExtraArgHelp c) ++
                    -- In the output, we want each command to be on its own
                    -- line, but we don't want blank lines between them.
                    -- AFAICT this can only be achieved with the .br
                    -- directive, which is probably a GNUism.
                    [".br"]

      -- | As 'synopsis', but make each group a subsection (.SS), and
      -- include the help text for each command.
      commands :: [String]
      commands = foldl iter [] commandControlList
          where iter :: [String] -> CommandControl -> [String]
                iter acc (GroupName x) = acc ++ [".SS \"" ++ x ++ "\""]
                iter acc (HiddenCommand _) = acc
                iter acc (CommandData (WrappedCommand c@SuperCommand {})) =
                    acc ++ concatMap
                            (render (commandName c ++ " "))
                            (extractCommands (commandSubCommands c))
                iter acc (CommandData c) = acc ++ render "" c
                render :: String -> WrappedCommand -> [String]
                render prefix (WrappedCommand c) =
                    [".B darcs " ++ prefix ++ commandName c] ++
                    map mangle_args (commandExtraArgHelp c) ++
                    [".RS 4", escape $ commandHelp c, ".RE"]

      -- | Now I'm showing off: mangle the extra arguments of Darcs commands
      -- so as to use the ideal format for manpages, italic words and roman
      -- punctuation.
      mangle_args :: String -> String
      mangle_args s =
          ".RI " ++ unwords (map show (groupBy cmp $ map toLower $ gank s))
              where cmp x y = not $ xor (isAlphaNum x) (isAlphaNum y)
                    xor x y = (x && not y) || (y && not x)
                    gank (' ':'o':'r':' ':xs) = '|' : gank xs
                    gank (x:xs) = x : gank xs
                    gank [] = []

      environment :: [String]
      environment = ".SH ENVIRONMENT" : concat
                    [(".SS \"" ++ andClauses ks ++ "\"") : map escape ds
                     | (ks, ds) <- environmentHelp]

      escape :: String -> String
      escape = minus . bs       -- Order is important
        where
          minus      = replace "-"     "\\-"
          bs         = replace "\\"    "\\\\"

          replace :: Eq a => [a] -> [a] -> [a] -> [a]
          replace _ _ [] = []
          replace find repl s =
              if find `isPrefixOf` s
                  then repl ++ replace find repl (drop (length find) s)
                  else head s : replace find repl (tail s)

markdownLines :: [String]
markdownLines =
 [ "# Commands", ""
 , unlines commands
 , "# Environment variables"
 , "", unlines environment
 , "# Patterns"
 , "", unlines helpOnMatchers  ]
   where
      environment :: [String]
      environment = intercalate [""]
                     [ renderEnv ks ds | (ks, ds) <- environmentHelp ]
        where
          renderEnv k d = ("## " ++ (intercalate ", " k)) : "" : d
      commands :: [String]
      commands = foldl iter [] commandControlList
      iter :: [String] -> CommandControl -> [String]
      iter acc (GroupName x) = acc ++ ["## " ++ x, ""]
      iter acc (HiddenCommand _) = acc
      iter acc (CommandData (WrappedCommand c@SuperCommand {})) =
          acc ++ concatMap
                  (render (commandName c ++ " "))
                  (extractCommands (commandSubCommands c))
      iter acc (CommandData c) = acc ++ render "" c
      render :: String -> WrappedCommand -> [String]
      render prefix (WrappedCommand c) =
          [ "### " ++ prefix ++ commandName c
          , "", "darcs " ++ prefix ++ commandName c ++ " [OPTION]... " ++
          unwords (commandExtraArgHelp c)
          , "", commandDescription c
          , "", commandHelp c
          , "Options:", optionsMarkdown $ commandBasicOptions c
          , if null opts2 then ""
             else unlines ["Advanced Options:", optionsMarkdown opts2]
          ]
       where opts2 = commandAdvancedOptions c

environmentHelpEditor :: ([String], [String])
environmentHelpEditor = (["DARCS_EDITOR", "DARCSEDITOR", "VISUAL", "EDITOR"],[
 "To edit a patch description of email comment, Darcs will invoke an",
 "external editor.  Your preferred editor can be set as any of the",
 "environment variables $DARCS_EDITOR, $DARCSEDITOR, $VISUAL or $EDITOR.",
 "If none of these are set, editor(1) is used."])

environmentHelpPager :: ([String], [String])
environmentHelpPager = (["DARCS_PAGER", "PAGER"],[
 "Darcs will invoke a pager if the output of some command is longer",
 "than 20 lines. Darcs will use the pager specified by $DARCS_PAGER",
 "or $PAGER.  If neither are set, pager(1) will be used."])

environmentHelpTimeout :: ([String], [String])
environmentHelpTimeout = (["DARCS_CONNECTION_TIMEOUT"],[
 "Set the maximum time in seconds that darcs allows and connection to",
 "take. If the variable is not specified the default are 30 seconds. This",
 "option only works with curl."])

-- | There are two environment variables that we do not document:
-- - DARCS_USE_ISPRINT: deprecated, use DARCS_DONT_ESCAPE_ISPRINT.
-- - DARCS_TESTING_PREFS_DIR: used by the test suite to tell darcs
--                            where to find its configuration files.