[Haskell] Read the contents from the clipboard in Haskell
有時候只是想要做一點簡單的事情的時候,其實會直接貼上 string 做處理。但是因為不知道怎麼在 ghci 上貼入 multiple line string,所以想到可以直接讀取複製的內容直接做處理。
在 Mac OSX 裡有一個相當好用的指令是 pbpaste
,可以直接蛉出在剪貼簿中的指令。開一個 subprocess 把指令的 output pipe 進來就可以了。嘗試如下。
This file contains 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
-- Read the contents of clipboard | |
-- Ref: http://stackoverflow.com/questions/1712347/closest-equivalent-to-subprocess-communicate-in-haskell | |
import System.Process | |
processData :: String -> String | |
processData = (id :: String -> String) | |
processPaste :: (String -> String) -> IO () | |
processPaste pd = do | |
let cmd = "pbpaste" | |
args = [] | |
instr = [] | |
(rc, out, err) <- readProcessWithExitCode cmd args instr | |
putStrLn $ pd out |
嘗試執行結果如下
Prelude> :l read_pbpaste.hs
[1 of 1] Compiling Main ( read_pbpaste.hs, interpreted )
Ok, modules loaded: Main.
*Main> import Data.Char
*Main Data.Char> processPaste Data.Char.to
Data.Char.toLower Data.Char.toTitle Data.Char.toUpper
*Main Data.Char> processPaste $ map Data.Char.toUpper
Loading package array-0.4.0.1 ... linking ... done.
Loading package deepseq-1.3.0.1 ... linking ... done.
Loading package filepath-1.3.0.1 ... linking ... done.
Loading package old-locale-1.0.0.5 ... linking ... done.
Loading package time-1.4.0.1 ... linking ... done.
Loading package bytestring-0.10.0.2 ... linking ... done.
Loading package unix-2.6.0.1 ... linking ... done.
Loading package directory-1.2.0.1 ... linking ... done.
Loading package process-1.1.0.2 ... linking ... done.
ING PACKAGE ARRAY-0.4.0.1 ... LINKING ... DONE.
LOADING PACKAGE DEEPSEQ-1.3.0.1 ... LINKING ... DONE.
LOADING PACKAGE FILEPATH-1.3.0.1 ... LINKING ... DONE.
LOADING PACKAGE OLD-LOCALE-1.0.0.5 ... LINKING ... DONE.
LOADING PACKAGE TIME-1.4.0.1 ... LINKING ... DONE.
LOADING PACKAGE BYTESTRING-0.10.0.2 ... LINKING ... DONE.
LOADING PACKAGE UNIX-2.6.0.1 ... LINKING ... DONE.
LOADING PACKAGE DIRECTORY-1.2.0.1 ... LINKING ... DONE.
LOADING PACKAGE PROCESS-1.1.0.2 ... LINKING ... DONE.
HTTPS://EN.WIKIPEDIA.ORG/WIKI/CONTINUATION-PASSING_STYLE
*Main Data.Char>
2013/09/08 09:17 AM 早上試一下,還可以這樣子幹也蠻方便的
Prelude System.Process> putStrLn =<< readProcess "pbpaste" [] []
http://hackage.haskell.org/packages/archive/process/1.0.1.1/doc/html/System-Process.html
Prelude System.Process> import Data.Char
Prelude System.Process Data.Char> putStrLn . (map toUpper) =<< readProcess "pbpaste" [] []
HTTP://HACKAGE.HASKELL.ORG/PACKAGES/ARCHIVE/PROCESS/1.0.1.1/DOC/HTML/SYSTEM-PROCESS.HTML
所以想要寫完全沒有彈性的碼可以這樣子寫。
This file contains 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
processPaste' :: (String -> String) -> IO () | |
processPaste' pd = putStrLn . pd =<< readProcess "pbpaste" [] [] |
沒有留言:
張貼留言