Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- -- The app reads two tab-separated columns of text from the clipboard
- -- and either swaps the columns or prepends the two columns with the
- -- second one. The swap mode is enabled with the "-s" option, the
- -- other mode with any non-empty string. No option results in an error
- -- message.
- import System.Environment (getArgs)
- import System.Hclip (getClipboard)
- import System.Exit (die)
- main :: IO ()
- main = do
- args <- getArgs
- if null args
- then die "No option has been given!"
- else do
- input <- getClipboard
- if null input
- then die "The clipboard is emtpy!"
- else mapM_ (putStrLn . process (head args == "-s")) $ lines input
- findTab :: String -> Int
- findTab = aux 0
- where
- aux index (x:xs)
- | x == '\t' = index
- | otherwise = aux (index + 1) xs
- splitAtTab :: String -> (String, String)
- splitAtTab [] = ([], [])
- splitAtTab list = (take tabIndex list, drop (tabIndex + 1) list)
- where
- tabIndex = findTab list
- process :: Bool -> String -> String
- process _ [] = []
- process bool string
- | bool = snd tuple ++ "\t" ++ fst tuple
- | otherwise = snd tuple ++ "\t" ++ fst tuple ++ "\t" ++ snd tuple
- where
- tuple = splitAtTab string
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement