简体   繁体   English

如何使用XMonad显示MPD音量

[英]How to display MPD volume with XMonad

I am trying to read MPD volume in XMonad using haskell-libmpd. 我正在尝试使用haskell-libmpd在XMonad中读取MPD卷。 While the standalone code: 而独立代码:

-- current darcs as of 2010-12-31
{-# LANGUAGE
     DeriveDataTypeable,
     FlexibleContexts,
     FlexibleInstances,
     MultiParamTypeClasses,
     NoMonomorphismRestriction,
     PatternGuards,
     ScopedTypeVariables,
     TypeSynonymInstances,
     UndecidableInstances,
     OverloadedStrings
     #-}
{-# OPTIONS_GHC -W -fwarn-unused-imports -fno-warn-missing-signatures #-}

import Control.Applicative
import Control.Monad
import Control.Monad.Instances ()
import Control.Monad.Writer
import Control.Monad.Trans (liftIO)
import Data.List
import Data.Int
import Data.Maybe
import Data.Either
import Data.Either.Utils
import Data.Traversable(traverse)
import qualified Data.Map as M
import System.IO
import System.Environment (getArgs)
import System.Process
import Prelude
import Text.Regex.Posix
import qualified Network.MPD as MPD
import qualified Network.MPD.Commands.Extensions as MPD
import Data.Array
import System.Cmd

int2str :: (Show a, Num a, Ord a) => a -> String
int2str x = if x < 10 then '0':sx else sx where sx = show x

parseMPD :: MPD.Response MPD.Status -> [[String]]
parseMPD (Left e) = return $ show e:repeat ""
parseMPD (Right st) = do
     return [vol, "%"]
     where
          vol = int2str $ MPD.stVolume st
--          song = MPD.withMPD MPD.currentSong 


main = do
     x <- MPD.withMPD $ MPD.status
     let a = unwords (foldr1 (++) (parseMPD x))
     rawSystem "notify-send" ["MPD Volume", a]

compiles correctly, using the same code in XMonad config 使用XMonad配置中的相同代码正确编译

int2str :: (Show a, Num a, Ord a) => a -> String
int2str x = if x < 10 then '0':sx else sx where sx = show x

parseMPD :: MPD.Response MPD.Status -> [[String]]
parseMPD (Left e) = return $ show e:repeat ""
parseMPD (Right st) = do
     return [vol, "%"]
     where
          vol = int2str $ MPD.stVolume st

volume :: MonadIO m => m()
volume = do
    x <- MPD.withMPD $ MPD.status
    let a = unwords (foldr1 (++) (parseMPD x))
    safeSpawn "notify-send" ["MPD Volume", a]

causes an error: 导致错误:

 xmonad.hs:182:14:
     Could not deduce (m ~ IO)
     from the context (MonadIO m)
       bound by the type signature for volume :: MonadIO m => m ()
       at xmonad.hs:180:11-26
       `m' is a rigid type variable bound by
           the type signature for volume :: MonadIO m => m ()
           at xmonad.hs:180:11
     Expected type: m (MPD.Response MPD.Status)
       Actual type: IO (MPD.Response MPD.Status)
     In a stmt of a 'do' block: x <- MPD.withMPD $ MPD.status
     In the expression:
       do { x <- MPD.withMPD $ MPD.status;
            let a = unwords (foldr1 (++) (parseMPD x));
            safeSpawn "notify-send" ["MPD Volume", a] }
     In an equation for `volume':
         volume
           = do { x <- MPD.withMPD $ MPD.status;
                  let a = ...;
                  safeSpawn "notify-send" ["MPD Volume", ....] }

How can I get MPD volume without running external apps? 如何在不运行外部应用程序的情况下获得MPD音量?

Answer: you're not using the same code in your xmonad config, you liar! 答:骗子,您在xmonad配置中没有使用相同的代码! You added the binding for volume . 您添加了volume的绑定。 Don't worry, though, it should be easy to fix: you can turn any IO a action into a MonadIO m => ma action with liftIO : 不要担心,虽然,它应该很容易解决:您可以将任何IO a动作到MonadIO m => ma用行动liftIO

volume :: MonadIO m => m()
volume = do
    x <- liftIO . MPD.withMPD $ MPD.status
    let a = unwords (foldr1 (++) (parseMPD x))
    safeSpawn "notify-send" ["MPD Volume", a]

By the way, you may later become interested in XMonad.Prompt.MPD . 顺便说一句,您以后可能会对XMonad.Prompt.MPD感兴趣。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM