Skip to content

Commit

Permalink
imp: cli: add a little highlighting to help output
Browse files Browse the repository at this point in the history
  • Loading branch information
simonmichael committed Jan 26, 2023
1 parent 0de3aca commit 72cd441
Show file tree
Hide file tree
Showing 2 changed files with 44 additions and 20 deletions.
22 changes: 20 additions & 2 deletions hledger/Hledger/Cli/CliOptions.hs
Expand Up @@ -87,6 +87,7 @@ import Data.Maybe
import qualified Data.Text as T
import Data.Void (Void)
import Safe
import String.ANSI
import System.Console.CmdArgs hiding (Default,def)
import System.Console.CmdArgs.Explicit
import System.Console.CmdArgs.Text
Expand Down Expand Up @@ -396,8 +397,25 @@ parseCommandDoc t =

-- | Get a mode's usage message as a nicely wrapped string.
showModeUsage :: Mode a -> String
showModeUsage = (showText defaultWrap :: [Text] -> String) .
(helpText [] HelpFormatDefault :: Mode a -> [Text])
showModeUsage =
highlightHelp .
(showText defaultWrap :: [Text] -> String) .
(helpText [] HelpFormatDefault :: Mode a -> [Text])

-- | Add some ANSI decoration to cmdargs' help output.
highlightHelp = unlines . zipWith (curry f) [1..] . lines
where
f (n,s)
| n==1 = bold s
| s `elem` [
"General input flags:"
,"General reporting flags:"
,"General help flags:"
,"Flags:"
,"General flags:"
,"Examples:"
] = bold s
| otherwise = s

-- | Get the most appropriate documentation topic for a mode.
-- Currently, that is either the hledger, hledger-ui or hledger-web
Expand Down
42 changes: 24 additions & 18 deletions hledger/Hledger/Cli/Commands.hs
Expand Up @@ -48,6 +48,7 @@ import Data.List
import Data.Text (Text)
import qualified Data.Text as T
import Data.Time.Calendar
import String.ANSI
import System.Environment (withArgs)
import System.Console.CmdArgs.Explicit as C
import Test.Tasty (defaultMain)
Expand Down Expand Up @@ -120,6 +121,8 @@ builtinCommands = [
,(testmode , testcmd)
]

accent = blue

-- figlet -f FONTNAME hledger, then escape backslashes
_banner_slant = drop 1 [""
-----------------------------------------80-------------------------------------
Expand All @@ -131,7 +134,7 @@ _banner_slant = drop 1 [""
," /____/ "
]

_banner_smslant = drop 1 [""
_banner_smslant = map accent $ drop 1 [""
," __ __ __ "
," / / / /__ ___/ /__ ____ ____"
," / _ \\/ / -_) _ / _ `/ -_) __/"
Expand All @@ -148,6 +151,8 @@ _banner_speed = drop 1 [""
," /____/ "
]

highlightAddon = id

-- | The commands list, showing command names, standard aliases,
-- and short descriptions. This is modified at runtime, as follows:
--
Expand All @@ -166,33 +171,34 @@ _banner_speed = drop 1 [""
--
-- TODO: generate more of this automatically.
--
commandsList :: String -> [String] -> [String]
commandsList progversion othercmds =
commandsList :: String -> [String] -> Bool -> [String]
commandsList progversion othercmds highlight =
(if highlight then (map (\s -> if "+" `isPrefixOf` s then highlightAddon (' ' : drop 1 s) else s)) else id) $
_banner_smslant ++ [
-- keep synced with hledger.m4.md > PART 4: COMMANDS, Hledger/Cli/Commands > commands.m4 -->
-----------------------------------------80-------------------------------------
""
,progversion
,accent progversion
,""
,"Usage: hledger COMMAND [OPTIONS] [-- ADDONCMDOPTIONS]"
,"Commands:"
,"Usage: " ++ bold "hledger CMD [OPTS] [-- ADDONCMDOPTS]"
,""
,"DATA ENTRY: add or edit entries in the journal file"
,"Commands:"
,bold "DATA ENTRY: add or edit entries in the journal file"
," add add transactions using terminal prompts"
,"+edit edit a subset of transactions"
,"+iadd add transactions using a TUI"
," import add new transactions from from other files, eg csv"
,""
-----------------------------------------80-------------------------------------
,"DATA CREATION: create or convert entries to be added to the journal file"
,bold "DATA CREATION: create or convert entries to be added to the journal file"
,"+autosync download/deduplicate/convert OFX data"
," close generate balance-zeroing/restoring transactions"
,"+interest generate interest transactions"
," rewrite generate auto postings, like print --auto"
,"+stockquotes download market prices from AlphaVantage"
,""
-----------------------------------------80-------------------------------------
,"DATA MANAGEMENT: help validate or manage journal files"
,bold "DATA MANAGEMENT: help validate or manage journal files"
," check check for various kinds of issue in the data"
,"+check-fancyassertions check more powerful balance assertions"
,"+check-tagfiles check file paths in tag values exist"
Expand All @@ -201,23 +207,23 @@ commandsList progversion othercmds =
,"+pijul record/status/log journal changes easily with pijul"
,""
-----------------------------------------80-------------------------------------
,"REPORTS, FINANCIAL: standard financial reports"
,bold "REPORTS, FINANCIAL: standard financial reports"
," aregister (areg) show transactions in a particular account"
," balancesheet (bs) show assets, liabilities and net worth"
," balancesheetequity (bse) show assets, liabilities and equity"
," cashflow (cf) show changes in liquid assets"
," incomestatement (is) show revenues and expenses"
,""
-----------------------------------------80-------------------------------------
,"REPORTS, VERSATILE: more complex/versatile reporting commands"
,bold "REPORTS, VERSATILE: more complex/versatile reporting commands"
," balance (bal) show balance changes, end balances, budgets, gains.."
,"+plot create charts from balance reports, in terminal or GUI"
," print show transactions or export journal data"
," register (reg) show postings in one or more accounts & running total"
," roi show return on investments"
,""
-----------------------------------------80-------------------------------------
,"REPORTS, BASIC: simple reports"
,bold "REPORTS, BASIC: simple reports"
," accounts show account names"
," activity show bar charts of posting counts per period"
," codes show transaction codes"
Expand All @@ -232,17 +238,17 @@ commandsList progversion othercmds =
," test run self tests"
,""
-----------------------------------------80-------------------------------------
,"UIS: other user interfaces"
,bold "UIS: other user interfaces"
,"+ui run terminal UI"
,"+web run web UI"
,""
,"OTHER: other hledger-* addon commands found in PATH"
,bold "OTHER: other hledger-* addon commands found in PATH"
] ++
multicol 80 (map ((' ':) . drop 1) othercmds)
multicol 80 (map (highlightAddon . (' ':) . drop 1) othercmds)
++
[""
-----------------------------------------80-------------------------------------
,"HELP: command-line help and more docs"
,bold "HELP: command-line help and more docs"
," hledger show this commands list"
," hledger -h show hledger's general help"
," hledger COMMAND -h show COMMAND's help"
Expand Down Expand Up @@ -282,15 +288,15 @@ commandsFromCommandsList s =
[w | c:l <- s, c `elem` [' ','+'], let w:_ = words l]

knownCommands :: [String]
knownCommands = sort . commandsFromCommandsList . drop 1 $ commandsList progname [] -- progname will not be seen
knownCommands = sort . commandsFromCommandsList . drop 1 $ commandsList progname [] False -- progname will not be seen

-- | Print the commands list, modifying the template above based on
-- the currently available addons. Missing addons will be removed, and
-- extra addons will be added under Misc.
printCommandsList :: String -> [String] -> IO ()
printCommandsList progversion addonsFound =
pager . unlines . concatMap adjustline $
commandsList progversion (map ('+':) unknownCommandsFound)
commandsList progversion (map ('+':) unknownCommandsFound) True
where
commandsFound = map (' ':) builtinCommandNames ++ map ('+':) addonsFound
unknownCommandsFound = addonsFound \\ knownCommands
Expand Down

0 comments on commit 72cd441

Please sign in to comment.