Simple calculator at xmonad prompt

I got a new idea for using xmonad XMonad.Prompt.Input. I thought it would be very cool if you could make a simple calculator that calculates that the user enters and returns the result in the next invitation text, ending when the user clicks the button ... The problem is that I do not do it completely know how to handle types ...

So far I have this:

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)) 

What does not work. I get:

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))'

I understand that this is because runAndGetOutput returns an IO String, and I need a normal string for inputPrompt, included from the XMonad.Prompt.Input import. But I have no idea how to handle this ...

Many thanks for your help!

EDIT: Now I have this:

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

, . , , , stdo , .

, echo : , . return, ( , , ). echo, echo bash script, echo.

EDIT: . :) .

+5
3

, XMonad.Util.Run, , xmonad ( , , ).

+2

X MonadIO,

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

, .

+1

Thanks guys, you are healthy :) Now it works. My last code is so short:

...
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

Just for others: to use this tiny calculator integrated into xmonad, I call it using key bindings such as:

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

Of course you need to install qalc. I think this is a pretty convenient snippet, because it is not just calc, you can basically call any executable file that produces a short result: a calculator, a dictionary, whatever you want ...

0
source

All Articles