38 lines
1.0 KiB
Haskell
38 lines
1.0 KiB
Haskell
{-# LANGUAGE LambdaCase #-}
|
|
module Operations.List where
|
|
|
|
import Control.Monad.Buuka
|
|
import Control.Monad.Reader
|
|
(asks, liftIO)
|
|
|
|
import Data.Foldable
|
|
(traverse_)
|
|
|
|
import Data.Semigroup
|
|
(Max(..))
|
|
|
|
import Data.Buuka
|
|
(BuukaEntry(..), URL(..))
|
|
import qualified Data.Buuka as B
|
|
|
|
import Web.Hashids
|
|
|
|
import Control.Lens
|
|
import Data.Text.Strict.Lens
|
|
(unpacked, utf8)
|
|
|
|
list :: BuukaM ()
|
|
list =
|
|
buukaQ (asks (format . B.elements)) >>= traverse_ (liftIO . putStrLn)
|
|
where
|
|
ctx = hashidsSimple "buuka"
|
|
format :: [BuukaEntry] -> [String]
|
|
format xs =
|
|
let formatted = zipWith formatEntry [1..] xs
|
|
indexWidth = getMax . foldMap (Max . length . fst) $ formatted
|
|
in fmap (\(idx,x) -> idx <> replicate (indexWidth - length idx) ' ' <> ". " <> x) formatted
|
|
formatEntry :: Int -> BuukaEntry -> (String, String)
|
|
formatEntry n = \case
|
|
BuukaEntry{title=Just t} -> (encode ctx n ^. utf8 . unpacked, t)
|
|
BuukaEntry{url=URL u} -> (encode ctx n ^. utf8 . unpacked, u)
|