Advertisement
banovski

Swap or prepend

Sep 11th, 2024 (edited)
285
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Haskell 1.20 KB | Source Code | 0 0
  1. -- The app reads two tab-separated columns of text from the clipboard
  2. -- and either swaps the columns or prepends the two columns with the
  3. -- second one. The swap mode is enabled with the "-s" option, the
  4. -- other mode with any non-empty string. No option results in an error
  5. -- message.
  6.  
  7. import System.Environment (getArgs)
  8. import System.Hclip (getClipboard)
  9. import System.Exit (die)
  10.  
  11. main :: IO ()
  12. main = do
  13.   args <- getArgs
  14.   if null args
  15.     then die "No option has been given!"
  16.     else do
  17.       input <- getClipboard
  18.       if null input
  19.         then die "The clipboard is emtpy!"
  20.         else mapM_ (putStrLn . process (head args == "-s")) $ lines input
  21.  
  22.  
  23. findTab :: String -> Int
  24. findTab = aux 0
  25.   where
  26.     aux index (x:xs)
  27.       | x == '\t' = index
  28.       | otherwise = aux (index + 1) xs
  29.  
  30. splitAtTab :: String -> (String, String)
  31. splitAtTab [] = ([], [])
  32. splitAtTab list = (take tabIndex list, drop (tabIndex + 1) list)
  33.   where
  34.     tabIndex = findTab list
  35.  
  36. process :: Bool -> String -> String
  37. process _ [] = []
  38. process bool string
  39.   | bool = snd tuple ++ "\t" ++ fst tuple
  40.   | otherwise = snd tuple ++ "\t" ++ fst tuple ++ "\t" ++ snd tuple
  41.   where
  42.     tuple = splitAtTab string
  43.  
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement