繁体   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