Skip to content

Commit 48ea210

Browse files
committed
imp:setup: tweak some tests' styles (more green and yellow, less red)
and related code cleanup.
1 parent 34a17d6 commit 48ea210

File tree

1 file changed

+54
-38
lines changed

1 file changed

+54
-38
lines changed

hledger/Hledger/Cli/Commands/Setup.hs

Lines changed: 54 additions & 38 deletions
Original file line numberDiff line numberDiff line change
@@ -386,7 +386,7 @@ setupJournal meconf = do
386386
undeclaredcommodities = journalCommoditiesUsed j \\ journalCommoditiesDeclared j
387387
if null undeclaredcommodities
388388
then p Y (show numcommodities <> " commodities")
389-
else p N (show (length undeclaredcommodities) <> " undeclared commodities")
389+
else w N (show (length undeclaredcommodities) <> " undeclared commodities")
390390

391391
let
392392
accttypes = [Asset, Liability, Equity, Revenue, Expense, Cash, Conversion]
@@ -434,28 +434,32 @@ setupJournal meconf = do
434434
-- if null typesinferredfromnames then i N "" else i Y (concatMap show typesinferredfromnames)
435435

436436
pdesc "all accounts are declared ?"
437-
if null undeclaredaccts then p Y (show numaccts <> " accounts") else i N (show (length undeclaredaccts) <> " undeclared accounts")
437+
if null undeclaredaccts
438+
then p Y (show numaccts <> " accounts")
439+
else w N (show (length undeclaredaccts) <> " undeclared accounts")
438440

439441
pdesc "all accounts have types ?"
440-
if null untypedaccts then p Y "" else i N (show (length untypedaccts) <> " accounts without types")
442+
if null untypedaccts
443+
then p Y ""
444+
else i N (show (length untypedaccts) <> " accounts have no type")
441445

442446
pdesc "accounts of all types exist ?"
443447
if null typesnotfound
444448
then p Y (concatMap show accttypes <> " accounts detected")
445-
else p N (concatMap show typesnotfound <> " accounts not found; some reports may not work")
449+
else w N (concatMap show typesnotfound <> " accounts not found; some reports may not work")
446450

447451
pdesc "commodities/accounts are being checked ?"
448452
let strict = isJust $ conflookup (\a -> any (==a) ["-s", "--strict"])
449453
if strict
450-
then i Y "commodities and accounts must be declared"
454+
then p Y "commodities and accounts must be declared"
451455
else i N "you can use -s to check them"
452456

453457
pdesc "balance assertions are being checked ?"
454458
let ignoreassertions = isJust $ conflookup (\a -> any (==a) ["-I", "--ignore-assertions"])
455459
if
456460
| ignoreassertions && not strict -> i N "you can use -s to check them"
457-
| not strict -> i Y "you can use -I to ignore them"
458-
| otherwise -> i Y "can't ignore assertions (-s in config file)"
461+
| not strict -> p Y "you can use -I to ignore them"
462+
| otherwise -> p Y "can't ignore assertions (-s in config file)"
459463

460464
------------------------------------------------------------------------------
461465

@@ -474,12 +478,17 @@ supportsColor = (>=! "1.41") -- robust color detection/control (2024)
474478
supportsPager = (>=! "1.41") -- use a pager for all output (2024)
475479
supportsBashCompletions = (>=! "1.41") -- up to date bash shell completions (2024)
476480

477-
-- Status of a setup question/statement: yes, no, unknown
481+
-- Status of a setup question/statement: yes, no, unknown.
478482
data YNU = Y | N | U deriving (Eq)
479483

480-
-- | Show status, with colours and emojis for added clarity when permitted,
481-
-- decorating Y and N with "good" and "bad" styles respectively. Used by p.
482-
-- See also 'showInfo' below.
484+
-- Show a status as unstyled english text.
485+
instance Show YNU where
486+
show Y = "yes"
487+
show N = " no"
488+
show U = " ?"
489+
490+
-- | Print a status, ANSI-styled and emoji-decorated when permitted, using the good/bad styles for Y/N;
491+
-- and the (possibly empty) provided message. See also 'w' and 'i'.
483492
--
484493
-- Status is communicated to the user
485494
-- 1. as text: "yes"/"no"/"?"
@@ -490,40 +499,47 @@ data YNU = Y | N | U deriving (Eq)
490499
--
491500
-- The emojis chosen are hopefully somewhat likely to render reasonably well even on non-apple machines;
492501
-- and if they don't, 1 and 2 will still carry the message.
493-
instance Show YNU where
494-
show Y = ansiGood $ "yes" `andIfColour` ""
495-
show N = ansiBad $ " no" `andIfColour` ""
496-
show U = ansiWarning $ " ?" `andIfColour` "🔸" -- 🔶
497-
498-
-- | Show status, with colours and emojis for added clarity when permitted,
499-
-- decorating Y and N with "neutral" style. Used by i.
500-
-- See also 'YNU''s Show instance above.
501-
showInfo Y = ansiNeutral $ "yes" `andIfColour` "ℹ️" -- may render as monochrome in some terminals ?
502-
showInfo N = ansiNeutral $ " no" `andIfColour` "ℹ️"
503-
showInfo U = ansiWarning $ " ?" `andIfColour` "🔸"
504-
505-
-- Dev note: confusingly, these things may not correspond:
506-
-- - "good"/"neutral"/"warning"/"bad" display text & user's perspective
507-
-- - ansiGood/ansiNeutral/ansiWarning/ansiBad styles defined below
508-
-- - warn[IO] and error functions defined elsewhere, which ANSI-decorate and display possibly ANSI-decorated text
509-
510-
-- Apply status-related ANSI styles to text.
502+
--
503+
-- Note these things are distinct and not necessarily corresponding, which could be confusing:
504+
-- - "good"/"neutral"/"warning"/"bad" test status, in display text & user's perspective
505+
-- - ansiGood/ansiNeutral/ansiWarning/ansiBad styles, defined below
506+
-- - warn[IO] and error functions defined elsewhere, which apply their own warning and error ANSI styles,
507+
-- to (possibly ANSI-styled) text.
508+
--
509+
p :: YNU -> String -> IO ()
510+
p status msg = putStrLn $ unwords ["", showGoodBad status, "", msg]
511+
where
512+
showGoodBad Y = ansiGood $ "yes" `andIfColour` ""
513+
showGoodBad N = ansiBad $ " no" `andIfColour` ""
514+
showGoodBad U = ansiWarning $ " ?" `andIfColour` "🔸" -- 🔶
515+
516+
-- | Print a status, ANSI-styled and emoji-decorated when permitted, using the good/warning styles for Y/N;
517+
-- and the (possibly empty) provided message.
518+
w :: YNU -> String -> IO ()
519+
w status msg = putStrLn $ unwords ["", showGoodWarn status, "", msg]
520+
where
521+
showGoodWarn Y = ansiGood $ "yes" `andIfColour` "ℹ️" -- may render as monochrome in some terminals ?
522+
showGoodWarn N = ansiWarning $ " no" `andIfColour` "🔸"
523+
showGoodWarn U = ansiWarning $ " ?" `andIfColour` "🔸"
524+
525+
-- | Print a status, ANSI-styled and emoji-decorated when permitted, using the neutral style for Y/N;
526+
-- and the (possibly empty) provided message.
527+
i :: YNU -> String -> IO ()
528+
i status msg = putStrLn $ unwords ["", showNeutral status, "", msg]
529+
where
530+
showNeutral Y = ansiNeutral $ "yes" `andIfColour` "ℹ️"
531+
showNeutral N = ansiNeutral $ " no" `andIfColour` "ℹ️"
532+
showNeutral U = ansiWarning $ " ?" `andIfColour` "🔸"
533+
534+
-- Apply setup-status-related ANSI styles to text.
511535
ansiGood = bold' . brightGreen'
512536
ansiNeutral = bold' . brightBlue'
513537
ansiWarning = bold' . brightYellow'
514538
ansiBad = bold' . brightRed'
515539

540+
-- Append a space and the second text, if colour is permitted on stdout (using 'useColorOnStdoutUnsafe').
516541
andIfColour a b = a <> if useColorOnStdoutUnsafe then " " <> b else ""
517542

518-
-- | Print a test's pass or fail status, as "yes" or "no" or "",
519-
-- in green/red if supported, and the (possibly empty) provided message.
520-
p :: YNU -> String -> IO ()
521-
p ok msg = putStrLn $ unwords ["", show ok, "", msg]
522-
523-
-- | Like p, but display the status as info, in neutral blue.
524-
i :: YNU -> String -> IO ()
525-
i ok msg = putStrLn $ unwords ["", showInfo ok, "", msg]
526-
527543
-- | Print a setup test groups heading.
528544
pgroup :: String -> IO ()
529545
pgroup s = putStrLn $ "\n" <> bold' s

0 commit comments

Comments
 (0)