Skip to content

Commit

Permalink
X.H.DynamicLog: Add shorten' and shortenLeft' (customizable end)
Browse files Browse the repository at this point in the history
  • Loading branch information
liskin committed Dec 13, 2020
1 parent 27f03ad commit 89646d7
Show file tree
Hide file tree
Showing 2 changed files with 18 additions and 11 deletions.
5 changes: 4 additions & 1 deletion CHANGES.md
Original file line number Diff line number Diff line change
Expand Up @@ -176,6 +176,9 @@
- Added `shortenLeft` function, like existing `shorten` but shortens by
truncating from left instead of right. Useful for showing directories.

- Added `shorten'` and `shortenLeft'` functions with customizable overflow
markers.

- Added `filterOutWsPP` for filtering out certain workspaces from being
displayed.

Expand Down Expand Up @@ -309,7 +312,7 @@

* `XMonad.Util.DebugWindow`
Fixed a bottom in `debugWindow` when used on windows with UTF8 encoded titles.

* `XMonad.Config.Xfce`
Set `terminal` to `xfce4-terminal`.

Expand Down
24 changes: 14 additions & 10 deletions XMonad/Hooks/DynamicLog.hs
Original file line number Diff line number Diff line change
Expand Up @@ -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,
Expand Down Expand Up @@ -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.
Expand Down

0 comments on commit 89646d7

Please sign in to comment.