X.H.DynamicLog: Add shorten' and shortenLeft' (customizable end)

This commit is contained in:
Tomas Janousek
2020-12-13 21:29:12 +00:00
parent 27f03ad9c5
commit 89646d75fd
2 changed files with 18 additions and 11 deletions

View File

@@ -43,7 +43,7 @@ module XMonad.Hooks.DynamicLog (
dzenPP, xmobarPP, sjanssenPP, byorgeyPP,
-- * Formatting utilities
wrap, pad, trim, shorten, shortenLeft,
wrap, pad, trim, shorten, shorten', shortenLeft, shortenLeft',
xmobarColor, xmobarAction, xmobarBorder,
xmobarRaw, xmobarStrip, xmobarStripTags,
dzenColor, dzenEscape, dzenStrip, filterOutWsPP,
@@ -386,18 +386,22 @@ trim = f . f
-- | Limit a string to a certain length, adding "..." if truncated.
shorten :: Int -> String -> String
shorten n xs | length xs < n = xs
| otherwise = take (n - length end) xs ++ end
where
end = "..."
shorten = shorten' "..."
-- | Limit a string to a certain length, adding @end@ if truncated.
shorten' :: String -> Int -> String -> String
shorten' end n xs | length xs < n = xs
| otherwise = take (n - length end) xs ++ end
-- | Like 'shorten', but truncate from the left instead of right.
shortenLeft :: Int -> String -> String
shortenLeft n xs | l < n = xs
| otherwise = end ++ (drop (l - n + length end) xs)
where
end = "..."
l = length xs
shortenLeft = shortenLeft' "..."
-- | Like 'shorten'', but truncate from the left instead of right.
shortenLeft' :: String -> Int -> String -> String
shortenLeft' end n xs | l < n = xs
| otherwise = end ++ (drop (l - n + length end) xs)
where l = length xs
-- | Output a list of strings, ignoring empty ones and separating the
-- rest with the given separator.