fix: cli: don't highlight help when TERM=dumb

This commit is contained in:
Simon Michael 2023-01-27 06:06:17 -10:00
parent bb5d473955
commit eed8a06906
3 changed files with 22 additions and 17 deletions

View File

@ -260,10 +260,7 @@ terminalFgColor = terminalColor Foreground
{-# NOINLINE terminalColor #-} {-# NOINLINE terminalColor #-}
terminalColor :: ConsoleLayer -> Maybe (RGB Float) terminalColor :: ConsoleLayer -> Maybe (RGB Float)
terminalColor layer = unsafePerformIO $ do terminalColor layer = unsafePerformIO $ do
inemacs <- not.null <$> lookupEnv "INSIDE_EMACS" fmap fractionalRGB <$> getLayerColor layer -- safe to run on non-interactive/dumb terminal
if inemacs -- skip this in emacs shell buffers, the terminal escape sequence is visible for some reason
then return Nothing
else fmap fractionalRGB <$> getLayerColor layer
where where
fractionalRGB :: (Fractional a) => RGB Word16 -> RGB a fractionalRGB :: (Fractional a) => RGB Word16 -> RGB a
fractionalRGB (RGB r g b) = RGB (fromIntegral r / 65535) (fromIntegral g / 65535) (fromIntegral b / 65535) -- chatgpt fractionalRGB (RGB r g b) = RGB (fromIntegral r / 65535) (fromIntegral g / 65535) (fromIntegral b / 65535) -- chatgpt

View File

@ -403,7 +403,9 @@ showModeUsage =
(helpText [] HelpFormatDefault :: Mode a -> [Text]) (helpText [] HelpFormatDefault :: Mode a -> [Text])
-- | Add some ANSI decoration to cmdargs' help output. -- | Add some ANSI decoration to cmdargs' help output.
highlightHelp = unlines . zipWith (curry f) [1..] . lines highlightHelp
| not useColorOnStdout = id
| otherwise = unlines . zipWith (curry f) [1..] . lines
where where
f (n,s) f (n,s)
| n==1 = bold s | n==1 = bold s

View File

@ -153,11 +153,15 @@ _banner_speed = drop 1 [""
-- picking one that will contrast with the current terminal background colour. -- picking one that will contrast with the current terminal background colour.
accent :: String -> String accent :: String -> String
accent accent
| not useColorOnStdout = id
| terminalIsLight == Just False = yellow -- . blackBg | terminalIsLight == Just False = yellow -- . blackBg
| otherwise = green -- . blackBg | otherwise = green -- . blackBg
highlightAddon = id highlightAddon = id
-- More careful version of bold
bold' = if useColorOnStdout then bold else id
-- | The commands list, showing command names, standard aliases, -- | The commands list, showing command names, standard aliases,
-- and short descriptions. This is modified at runtime, as follows: -- and short descriptions. This is modified at runtime, as follows:
-- --
@ -177,25 +181,27 @@ highlightAddon = id
-- TODO: generate more of this automatically. -- TODO: generate more of this automatically.
-- --
commandsList :: String -> [String] -> Bool -> [String] commandsList :: String -> [String] -> Bool -> [String]
commandsList progversion othercmds highlight = commandsList progversion othercmds highlight0 =
let highlight = highlight0 && useColorOnStdout in
(if highlight then (map (\s -> if "+" `isPrefixOf` s then highlightAddon (' ' : drop 1 s) else s)) else id) $ (if highlight then (map (\s -> if "+" `isPrefixOf` s then highlightAddon (' ' : drop 1 s) else s)) else id) $
map (bold.accent) _banner_smslant ++ [ map (bold'.accent) _banner_smslant ++
[
-- keep synced with hledger.m4.md > PART 4: COMMANDS, Hledger/Cli/Commands > commands.m4 --> -- keep synced with hledger.m4.md > PART 4: COMMANDS, Hledger/Cli/Commands > commands.m4 -->
-----------------------------------------80------------------------------------- -----------------------------------------80-------------------------------------
"" ""
,accent progversion ,accent progversion
,"" ,""
,"Usage: " ++ bold "hledger CMD [OPTS] [-- ADDONCMDOPTS]" ,"Usage: " ++ bold' "hledger CMD [OPTS] [-- ADDONCMDOPTS]"
,"" ,""
,"Commands:" ,"Commands:"
,bold "DATA ENTRY: add or edit entries in the journal file" ,bold' "DATA ENTRY: add or edit entries in the journal file"
," add add transactions using terminal prompts" ," add add transactions using terminal prompts"
,"+edit edit a subset of transactions" ,"+edit edit a subset of transactions"
,"+iadd add transactions using a TUI" ,"+iadd add transactions using a TUI"
," import add new transactions from from other files, eg csv" ," import add new transactions from from other files, eg csv"
,"" ,""
-----------------------------------------80------------------------------------- -----------------------------------------80-------------------------------------
,bold "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" ,"+autosync download/deduplicate/convert OFX data"
," close generate balance-zeroing/restoring transactions" ," close generate balance-zeroing/restoring transactions"
,"+interest generate interest transactions" ,"+interest generate interest transactions"
@ -203,7 +209,7 @@ commandsList progversion othercmds highlight =
,"+stockquotes download market prices from AlphaVantage" ,"+stockquotes download market prices from AlphaVantage"
,"" ,""
-----------------------------------------80------------------------------------- -----------------------------------------80-------------------------------------
,bold "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 check for various kinds of issue in the data"
,"+check-fancyassertions check more powerful balance assertions" ,"+check-fancyassertions check more powerful balance assertions"
,"+check-tagfiles check file paths in tag values exist" ,"+check-tagfiles check file paths in tag values exist"
@ -212,7 +218,7 @@ commandsList progversion othercmds highlight =
,"+pijul record/status/log journal changes easily with pijul" ,"+pijul record/status/log journal changes easily with pijul"
,"" ,""
-----------------------------------------80------------------------------------- -----------------------------------------80-------------------------------------
,bold "REPORTS, FINANCIAL: standard financial reports" ,bold' "REPORTS, FINANCIAL: standard financial reports"
," aregister (areg) show transactions in a particular account" ," aregister (areg) show transactions in a particular account"
," balancesheet (bs) show assets, liabilities and net worth" ," balancesheet (bs) show assets, liabilities and net worth"
," balancesheetequity (bse) show assets, liabilities and equity" ," balancesheetequity (bse) show assets, liabilities and equity"
@ -220,7 +226,7 @@ commandsList progversion othercmds highlight =
," incomestatement (is) show revenues and expenses" ," incomestatement (is) show revenues and expenses"
,"" ,""
-----------------------------------------80------------------------------------- -----------------------------------------80-------------------------------------
,bold "REPORTS, VERSATILE: more complex/versatile reporting commands" ,bold' "REPORTS, VERSATILE: more complex/versatile reporting commands"
," balance (bal) show balance changes, end balances, budgets, gains.." ," balance (bal) show balance changes, end balances, budgets, gains.."
,"+plot create charts from balance reports, in terminal or GUI" ,"+plot create charts from balance reports, in terminal or GUI"
," print show transactions or export journal data" ," print show transactions or export journal data"
@ -228,7 +234,7 @@ commandsList progversion othercmds highlight =
," roi show return on investments" ," roi show return on investments"
,"" ,""
-----------------------------------------80------------------------------------- -----------------------------------------80-------------------------------------
,bold "REPORTS, BASIC: simple reports" ,bold' "REPORTS, BASIC: simple reports"
," accounts show account names" ," accounts show account names"
," activity show bar charts of posting counts per period" ," activity show bar charts of posting counts per period"
," codes show transaction codes" ," codes show transaction codes"
@ -243,17 +249,17 @@ commandsList progversion othercmds highlight =
," test run self tests" ," test run self tests"
,"" ,""
-----------------------------------------80------------------------------------- -----------------------------------------80-------------------------------------
,bold "UIS: other user interfaces" ,bold' "UIS: other user interfaces"
,"+ui run terminal UI" ,"+ui run terminal UI"
,"+web run web UI" ,"+web run web UI"
,"" ,""
,bold "OTHER: other hledger-* addon commands found in PATH" ,bold' "OTHER: other hledger-* addon commands found in PATH"
] ++ ] ++
multicol 80 (map (highlightAddon . (' ':) . drop 1) othercmds) multicol 80 (map (highlightAddon . (' ':) . drop 1) othercmds)
++ ++
["" [""
-----------------------------------------80------------------------------------- -----------------------------------------80-------------------------------------
,bold "HELP: command-line help and more docs" ,bold' "HELP: command-line help and more docs"
," hledger show this commands list" ," hledger show this commands list"
," hledger -h show hledger's general help" ," hledger -h show hledger's general help"
," hledger COMMAND -h show COMMAND's help" ," hledger COMMAND -h show COMMAND's help"