Snakn

Haskell multiclipboard

Mar 21st, 2019
299
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
  1. import System.IO
  2. import System.Environment
  3. import System.Hclip
  4.  
  5. data Hash = Hash {
  6.     key :: String,
  7.     val :: String
  8. }deriving Show
  9.  
  10. main :: IO()
  11. main = do
  12.     args <- getArgs
  13.     let cmd = args !! 0
  14.     let param = args !! 1
  15.     evalInput cmd param
  16.    
  17. evalInput cmd param
  18.     | cmd == "cp" = writeToClip param
  19.     | cmd == "sv" = writeToF param
  20.     | cmd == "ls" = listFromfile
  21.    
  22. writeToF str = do
  23.     k <- getClipboard
  24.     let input = (str ++ " " ++ k)
  25.     file <- openFile "clip.txt" AppendMode
  26.     hClose file
  27.    
  28. readFromF = do
  29.     file <- openFile "clip.txt" ReadMode
  30.     contents <- hGetContents file
  31.     let singlelines = (lines contents)
  32.     let li = (hashlist singlelines)
  33.     return li
  34.  
  35. writeToClip str = do
  36.     li <- readFromF
  37.     let result = findByKey li str
  38.     setClipboard result
  39.  
  40. listFromfile = do
  41.     li <- readFromF
  42.     putStrLn $ prettyprint li
  43.  
  44. prettyprint :: [Hash] -> String
  45. prettyprint [] = ""
  46. prettyprint (x:xs) = (key x) ++ ":" ++ (val x) ++ "\n" ++ prettyprint xs
  47.  
  48. hashlist :: [String] -> [Hash]
  49. hashlist [] = []
  50. hashlist (x:xs) = h x : hashlist xs
  51.             where h xs = Hash (head $ words xs) (drop (1 + (length $ head $ words xs)) xs)
  52.    
  53. findByKey :: [Hash] -> String -> String
  54. findByKey [] str = []
  55. findByKey (x:xs) str
  56.           | (key x) == str = (val x)
  57.           | otherwise = findByKey xs str
Advertisement