i have a new idea of using xmonad's XMonad.Prompt.Input. I thought it would be really cool, if one could make a simple calculator that would compute what the user enters and return the result in the next prompt's text, finishing when the user presses escape... The problem is, that I don't quite know how to deal with the 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))
Which doesn't 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 do understand it has something to do with the fact that runAndGetOutput returns IO String and I need a normal String for inputPrompt included from import XMonad.Prompt.Input. But I have no clue how to deal with that...
Thanks a lot 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
Which compiles, but doesn't work as expected. I can open the prompt, enter some text, then it launches the shell command, but then it just discards the stdo value instead of using it as a new prompt text.
I would expect the version with echo do following: When I open the prompt, some default string is shown. When I enter a value and press return, another prompt opens with the value previously entered (thanks to echo which just returns what it has got). If it worked with echo, i would replace echo with some bash script to perform the calculations and return the result instead of echo.
Recent EDIT: Finally resolved. The final code of my small calc snippet is in my self-answer:) Thank you all.
You should be using the functions available in XMonad.Util.Run, which take care of a few xmonad-specific details (some signal-handling, I think).