我有一个使用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))
Run Code Online (Sandbox Code Playgroud)
哪个不起作用.我明白了:
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))'
Run Code Online (Sandbox Code Playgroud)
我确实理解它与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
Run Code Online (Sandbox Code Playgroud)
哪个编译,但不能按预期工作.我可以打开提示,输入一些文本,然后启动shell命令,但之后它只丢弃stdo值而不是将其用作新的提示文本.
我希望带有echo的版本可以执行以下操作:当我打开提示时,会显示一些默认字符串.当我输入一个值并按回车键时,会打开另一个提示,其中包含先前输入的值(感谢echo,它只返回它所获得的值).如果它与echo一起工作,我会用一些bash脚本替换echo来执行计算并返回结果而不是echo.
最近的编辑: 终于解决了.我的小钙片的最终代码在我的自我答案:)谢谢大家.