簡體   English   中英

xmonad提示中的簡單計算器

[英]Simple calculator in xmonad prompt

我有一個使用xmonad的XMonad.Prompt.Input的新想法。 我認為這真的很酷,如果一個人可以制作一個簡單的計算器來計算用戶輸入的內容並在下一個提示的文本中返回結果,當用戶按下escape時結束......問題是,我沒有非常知道如何處理類型......

到目前為止我有這個:

runAndGetOutput cmd = do
    (_, pout, _, phandle) <- runInteractiveCommand cmd
    waitForProcess phandle
    a <- hGetContents pout
    return a 

calcPrompt :: XPConfig -> String -> X () 
calcPrompt c ans =
    inputPrompt c ans ?+ \ next -> 
        calcPrompt c (runAndGetOutput ("calc" ++  next)) 

哪個不起作用。 我明白了:

Couldn't match expected type `[Char]' with actual type `IO String'
Expected type: String
Actual type: IO String
In the return type of a call of `runAndGetOutput'
In the second argument of `calcPrompt', namely
`(runAndGetOutput ("calc" ++ next))'

我確實理解它與runAndGetOutput返回IO String的事實有關,我需要從import XMonad.Prompt.Input包含的inputPrompt的普通String。 但我不知道如何處理......

非常感謝你的幫助!

編輯:現在我有這個:

runAndGetOutput :: String -> IO String
runAndGetOutput cmd = do
    (_, pout, _, phandle) <- runInteractiveCommand cmd
    a <- hGetContents pout
        waitForProcess phandle
        return a 

calcPrompt :: XPConfig -> String -> X () 
calcPrompt c ans =
    inputPrompt c ans ?+ \next ->
        liftIO (runAndGetOutput ("echo -n " ++ next)) >>= calcPrompt c

哪個編譯,但不能按預期工作。 我可以打開提示,輸入一些文本,然后啟動shell命令,但之后它只丟棄stdo值而不是將其用作新的提示文本。

我希望帶有echo的版本可以執行以下操作:當我打開提示時,會顯示一些默認字符串。 當我輸入一個值並按回車鍵時,會打開另一個提示,其中包含先前輸入的值(感謝echo,它只返回它所獲得的值)。 如果它與echo一起工作,我會用一些bash腳本替換echo來執行計算並返回結果而不是echo。

最近的編輯:終於解決了。 我的小鈣片的最終代碼在我的自我答案:)謝謝大家。

您應該使用XMonad.Util.Run中可用的函數,它們會處理一些特定於xmonad的細節(我認為有些信號處理)。

X有一個MonadIO實例,所以

calcPrompt c ans =
    inputPrompt c ans ?+ \next ->
        liftIO (runAndGetOutput ("calc" ++ next)) >>= calcPrompt c

我認為應該有用。

謝謝你們......你們很棒:)現在它可以了。 我的最終代碼太短了:

...
import XMonad.Prompt
import XMonad.Prompt.Input
import Data.Char (isSpace)

...    

calcPrompt :: XPConfig -> String -> X () 
calcPrompt c ans =
    inputPrompt c (trim ans) ?+ \input -> 
        liftIO(runProcessWithInput "qalc" [input] "") >>= calcPrompt c 
    where
        trim  = f . f
            where f = reverse . dropWhile isSpace

只為其他人:使用xmonad中集成的這個微小的calc,我通過像這樣的鍵綁定來調用它:

, ((modm, xK_KP_Multiply), calcPrompt defaultXPConfig "qalc" )

當然你需要安裝qalc。 我認為這是一個非常方便的片段,因為它不只是一個計算,你基本上可以調用任何產生一些短輸出的可執行文件:計算器,字典,你喜歡什么...

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM