{-|

The quick reference card: a compact overview of hledger's most useful
commands, flags, and concepts, shown by @hledger help quickref@.

-}

{-# LANGUAGE TemplateHaskell #-}

module Hledger.Cli.Commands.Quickref (
  showQuickref
) where

import Data.List (isPrefixOf, isSuffixOf)
import System.Console.ANSI (setSGRCode, SGR(..), Underlining(..))

import Hledger
import Hledger.Cli.CliOptions (prognameandversionnoarch)

-- | The quick reference card, from quickref.txt in the hledger package directory.
quickrefcontent :: String
quickrefcontent :: String
quickrefcontent = $(embedFileRelative "quickref.txt")

-- | Print the quick reference card, styling it a little when color is enabled.
showQuickref :: IO ()
showQuickref :: IO ()
showQuickref = do
  usecolor <- IO Bool
useColorOnStdout
  runPager $ styleQuickref usecolor quickrefcontent

-- | Render the card. The program name and version are added right-aligned on
-- the title line. When color is enabled: the title line gets hledger's usual
-- bold colour gradient (and the version a faint gradient), labels in the margin
-- (the first 18 columns) are faint, top-level headings are also underlined (up to
-- their first : or ,), a heading followed by just a parenthesised note is all faint,
-- and the commands, flags etc. beyond the margin are left normal, except parentheses,
-- which are faint.
styleQuickref :: Bool -> String -> String
styleQuickref :: Bool -> String -> String
styleQuickref Bool
usecolor String
s = [String] -> String
unlines ([String] -> String) -> [String] -> String
forall a b. (a -> b) -> a -> b
$ case String -> [String]
lines String
s of
  []         -> []
  String
title:[String]
rest -> String -> String
titleline String
title String -> [String] -> [String]
forall a. a -> [a] -> [a]
: (String -> String) -> [String] -> [String]
forall a b. (a -> b) -> [a] -> [b]
map String -> String
styleline [String]
rest
  where
    version :: String
version = String
prognameandversionnoarch
    -- align the version's right edge with the widest line of the card
    cardwidth :: Int
cardwidth = [Int] -> Int
forall a. Ord a => [a] -> a
forall (t :: * -> *) a. (Foldable t, Ord a) => t a -> a
maximum ([Int] -> Int) -> [Int] -> Int
forall a b. (a -> b) -> a -> b
$ (String -> Int) -> [String] -> [Int]
forall a b. (a -> b) -> [a] -> [b]
map String -> Int
forall a. [a] -> Int
forall (t :: * -> *) a. Foldable t => t a -> Int
length ([String] -> [Int]) -> [String] -> [Int]
forall a b. (a -> b) -> a -> b
$ String -> [String]
lines String
s
    titleline :: String -> String
titleline String
t = Int -> String -> String -> String
titleAndVersionLine Int
cardwidth String
t String
version
    labelwidth :: Int
labelwidth = Int
18
    styleheading :: String -> String
styleheading = String -> String
faint'
    styleline :: String -> String
styleline String
l
      | Bool -> Bool
not Bool
usecolor      = String
l
      | (Char -> Bool) -> String -> Bool
forall (t :: * -> *) a. Foldable t => (a -> Bool) -> t a -> Bool
all (Char -> Char -> Bool
forall a. Eq a => a -> a -> Bool
==Char
' ') String
label = String
label String -> String -> String
forall a. Semigroup a => a -> a -> a
<> String -> String
dimparens String
content
      | Bool
isnoteheading     = String -> String
stylelabel String
l
      | Bool
otherwise         = String -> String
stylelabel String
label String -> String -> String
forall a. Semigroup a => a -> a -> a
<> String -> String
dimparens String
content
      where
        -- eg "Common flags: (hledger -h ... to see all)"
        isnoteheading :: Bool
isnoteheading = case (Char -> Bool) -> String -> (String, String)
forall a. (a -> Bool) -> [a] -> ([a], [a])
break (Char -> Char -> Bool
forall a. Eq a => a -> a -> Bool
==Char
':') String
l of
          (Char
_:String
_, Char
_:String
rest) -> let note :: String
note = (Char -> Bool) -> String -> String
forall a. (a -> Bool) -> [a] -> [a]
dropWhile (Char -> Char -> Bool
forall a. Eq a => a -> a -> Bool
==Char
' ') String
rest in String
"(" String -> String -> Bool
forall a. Eq a => [a] -> [a] -> Bool
`isPrefixOf` String
note Bool -> Bool -> Bool
&& String
")" String -> String -> Bool
forall a. Eq a => [a] -> [a] -> Bool
`isSuffixOf` String
note
          (String, String)
_             -> Bool
False
        (String
label, String
content) = Int -> String -> (String, String)
forall a. Int -> [a] -> ([a], [a])
splitAt Int
labelwidth String
l
    -- an indented sub-label is just faint; a top-level heading's name is also underlined
    stylelabel :: String -> String
stylelabel t :: String
t@(Char
' ':String
_) = String -> String
styleheading String
t
    stylelabel String
t         = String -> String
underline' (String -> String
styleheading String
name) String -> String -> String
forall a. Semigroup a => a -> a -> a
<> String -> String
styleheading String
rest
      where (String
name, String
rest) = (Char -> Bool) -> String -> (String, String)
forall a. (a -> Bool) -> [a] -> ([a], [a])
break (Char -> String -> Bool
forall a. Eq a => a -> [a] -> Bool
forall (t :: * -> *) a. (Foldable t, Eq a) => a -> t a -> Bool
`elem` (String
",:" :: String)) String
t
    dimparens :: String -> String
dimparens = (Char -> String) -> String -> String
forall (t :: * -> *) a b. Foldable t => (a -> [b]) -> t a -> [b]
concatMap (\Char
c -> if Char
c Char -> String -> Bool
forall a. Eq a => a -> [a] -> Bool
forall (t :: * -> *) a. (Foldable t, Eq a) => a -> t a -> Bool
`elem` String
"()" then String -> String
styleheading [Char
c] else [Char
c])

-- | Wrap a string in ANSI codes to switch single underlining on and off.
underline' :: String -> String
underline' :: String -> String
underline' String
s = [SGR] -> String
setSGRCode [Underlining -> SGR
SetUnderlining Underlining
SingleUnderline] String -> String -> String
forall a. Semigroup a => a -> a -> a
<> String
s String -> String -> String
forall a. Semigroup a => a -> a -> a
<> [SGR] -> String
setSGRCode [Underlining -> SGR
SetUnderlining Underlining
NoUnderline]