52 lines
1.5 KiB
Haskell
52 lines
1.5 KiB
Haskell
{-# LANGUAGE LambdaCase #-}
|
|
{-|
|
|
Module : Operations.Format
|
|
Description : Format the list of bookmarks
|
|
Copyright : (c) Mats Rauhala, 2020
|
|
License : BSD-3-Clause
|
|
Maintainer : mats.rauhala@iki.fi
|
|
Stability : experimental
|
|
Portability : POSIX
|
|
|
|
Format the list of bookmarks. It uses the "hashids" module to create a unique
|
|
hash for each entry. Some extra (user) security is given by using the hash of
|
|
the full entries as the initial context for hashids. If the state has been
|
|
modified between operations, the ids change.
|
|
-}
|
|
module Operations.Format where
|
|
|
|
import Data.Buuka
|
|
(Buuka, BuukaEntry(..), URL(..))
|
|
import qualified Data.Buuka as B
|
|
|
|
import Data.Semigroup
|
|
(Max(..))
|
|
|
|
import Web.Hashids
|
|
|
|
import Control.Lens
|
|
import Data.Text.Strict.Lens
|
|
(utf8)
|
|
|
|
import Data.Text
|
|
(Text)
|
|
import qualified Data.Text as T
|
|
|
|
-- | Format the entries
|
|
formatEntries
|
|
:: Buuka -- ^ The full set of entries, for the context
|
|
-> [BuukaEntry] -- ^ The list of entries to be formatted
|
|
-> [Text]
|
|
formatEntries buuka xs =
|
|
let formatted = zipWith formatEntry [1..] xs
|
|
indexWidth = getMax . foldMap (Max . T.length . fst) $ formatted
|
|
in fmap (\(idx,x) -> idx <> T.replicate (indexWidth - T.length idx) " " <> ". " <> x) formatted
|
|
where
|
|
ctx = mkContext buuka
|
|
mkContext :: Buuka -> HashidsContext
|
|
mkContext = hashidsSimple . B.fingerprint
|
|
formatEntry :: Int -> BuukaEntry -> (Text, Text)
|
|
formatEntry n = \case
|
|
BuukaEntry{title=Just t} -> (encode ctx n ^. utf8, t)
|
|
BuukaEntry{url=URL u} -> (encode ctx n ^. utf8, u)
|