Created
July 29, 2012 00:47
-
-
Save homeworkprod/3195482 to your computer and use it in GitHub Desktop.
Generate XMonad workspace names from a range of numbers with optional descriptive suffixes.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
----------------------------------------- | |
-- | Generate workspace names for XMonad. | |
----------------------------------------- | |
import qualified Data.List as List | |
import qualified Data.Map as Map | |
-- | A workspace's number. | |
-- | |
-- If a workspace's name starts with a number, one can switch to that | |
-- workspace using the configured modifier key with that number. | |
newtype WorkspaceNumber = WNumber Integer deriving (Eq, Ord) | |
-- | A workspace suffix. | |
-- | |
-- It will be appended to the workspace's number. | |
newtype WorkspaceSuffix = WSuffix String deriving (Eq, Ord) | |
-- | A mapping from workspace suffixes to workspace numbers. | |
type SuffixesByNumber = Map.Map WorkspaceNumber WorkspaceSuffix | |
-- | Map workspace numbers to suffixes. | |
mapNumbersToSuffixes :: [(Integer, String)] -> SuffixesByNumber | |
mapNumbersToSuffixes numberAndSuffixPairs = Map.fromList (map parse numberAndSuffixPairs) | |
where | |
parse (numberRaw, suffixRaw) = (WNumber numberRaw, WSuffix suffixRaw) | |
-- | Generate a workspace's name from its number and, if available, a suffix. | |
determineName :: SuffixesByNumber -> WorkspaceNumber -> String | |
determineName suffixesByNumber k = case Map.lookup k suffixesByNumber of | |
Nothing -> extractNumberValue k | |
Just v -> (extractNumberValue k) ++ ":" ++ (extractSuffixValue v) | |
where | |
extractNumberValue (WNumber x) = show x | |
extractSuffixValue (WSuffix x) = x | |
-- expected result: | |
-- ["1:web", "2", "3", "4", "5:mail", "6", "7", "8", "9"] | |
numbers = map WNumber [1..9] | |
suffixesByNumber = mapNumbersToSuffixes [ | |
(1, "web"), | |
(5, "mail") | |
] | |
names = map (determineName suffixesByNumber) numbers | |
join :: [String] -> String | |
join xs = List.intercalate ", " xs | |
main = do | |
putStrLn ("workspace names: " ++ join names) |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment