module XMonad.Actions.Launcher(
defaultLauncherModes
, ExtensionActions
, LauncherConfig(..)
, launcherPrompt
) where
import qualified Data.Map as M
import XMonad hiding (config)
import XMonad.Prelude
import XMonad.Prompt
import XMonad.Util.Run
data HoogleMode = HMode FilePath String
data CalculatorMode = CalcMode
data LauncherConfig = LauncherConfig {
LauncherConfig -> String
browser :: String
, LauncherConfig -> String
pathToHoogle :: String
}
type ExtensionActions = M.Map String (String -> X())
instance XPrompt CalculatorMode where
showXPrompt :: CalculatorMode -> String
showXPrompt CalculatorMode
CalcMode = String
"calc %s> "
commandToComplete :: CalculatorMode -> String -> String
commandToComplete CalculatorMode
CalcMode = String -> String
forall a. a -> a
id
completionFunction :: CalculatorMode -> ComplFunction
completionFunction CalculatorMode
CalcMode = \String
s -> if String -> Bool
forall (t :: * -> *) a. Foldable t => t a -> Bool
null String
s then [String] -> IO [String]
forall (m :: * -> *) a. Monad m => a -> m a
return [] else
String -> [String]
lines (String -> [String]) -> IO String -> IO [String]
forall (f :: * -> *) a b. Functor f => (a -> b) -> f a -> f b
<$> String -> [String] -> String -> IO String
forall (m :: * -> *).
MonadIO m =>
String -> [String] -> String -> m String
runProcessWithInput String
"calc" [String
s] String
""
modeAction :: CalculatorMode -> String -> String -> X ()
modeAction CalculatorMode
CalcMode String
_ String
_ = () -> X ()
forall (m :: * -> *) a. Monad m => a -> m a
return ()
instance XPrompt HoogleMode where
showXPrompt :: HoogleMode -> String
showXPrompt HoogleMode
_ = String
"hoogle %s> "
commandToComplete :: HoogleMode -> String -> String
commandToComplete HoogleMode
_ = String -> String
forall a. a -> a
id
completionFunction :: HoogleMode -> ComplFunction
completionFunction (HMode String
pathToHoogleBin' String
_) = \String
s -> String -> [String] -> IO [String]
completionFunctionWith String
pathToHoogleBin' [String
"--count",String
"8",String
s]
modeAction :: HoogleMode -> String -> String -> X ()
modeAction (HMode String
pathToHoogleBin'' String
browser') String
query String
result = do
[String]
completionsWithLink <- IO [String] -> X [String]
forall (m :: * -> *) a. MonadIO m => IO a -> m a
liftIO (IO [String] -> X [String]) -> IO [String] -> X [String]
forall a b. (a -> b) -> a -> b
$ String -> [String] -> IO [String]
completionFunctionWith String
pathToHoogleBin'' [String
"--count",String
"5",String
"--link",String
query]
let link :: Maybe String
link = do
String
s <- (String -> Bool) -> [String] -> Maybe String
forall (t :: * -> *) a. Foldable t => (a -> Bool) -> t a -> Maybe a
find (Maybe Int -> Bool
forall a. Maybe a -> Bool
isJust (Maybe Int -> Bool) -> (String -> Maybe Int) -> String -> Bool
forall b c a. (b -> c) -> (a -> b) -> a -> c
. \String
complStr -> String -> String -> Maybe Int
forall a. Eq a => [a] -> [a] -> Maybe Int
findSeqIndex String
complStr String
result) [String]
completionsWithLink
Int
i <- String -> String -> Maybe Int
forall a. Eq a => [a] -> [a] -> Maybe Int
findSeqIndex String
s String
"http://"
String -> Maybe String
forall (m :: * -> *) a. Monad m => a -> m a
return (String -> Maybe String) -> String -> Maybe String
forall a b. (a -> b) -> a -> b
$ Int -> String -> String
forall a. Int -> [a] -> [a]
drop Int
i String
s
case Maybe String
link of
Just String
l -> String -> X ()
forall (m :: * -> *). MonadIO m => String -> m ()
spawn (String -> X ()) -> String -> X ()
forall a b. (a -> b) -> a -> b
$ String
browser' String -> String -> String
forall a. [a] -> [a] -> [a]
++ String
" " String -> String -> String
forall a. [a] -> [a] -> [a]
++ String
l
Maybe String
_ -> () -> X ()
forall (m :: * -> *) a. Monad m => a -> m a
return ()
where
findSeqIndex :: (Eq a) => [a] -> [a] -> Maybe Int
findSeqIndex :: [a] -> [a] -> Maybe Int
findSeqIndex [a]
xs [a]
xss = ([a] -> Bool) -> [[a]] -> Maybe Int
forall a. (a -> Bool) -> [a] -> Maybe Int
findIndex ([a] -> [a] -> Bool
forall a. Eq a => [a] -> [a] -> Bool
isPrefixOf [a]
xss) ([[a]] -> Maybe Int) -> [[a]] -> Maybe Int
forall a b. (a -> b) -> a -> b
$ [a] -> [[a]]
forall a. [a] -> [[a]]
tails [a]
xs
completionFunctionWith :: String -> [String] -> IO [String]
completionFunctionWith :: String -> [String] -> IO [String]
completionFunctionWith String
cmd [String]
args = String -> [String]
lines (String -> [String]) -> IO String -> IO [String]
forall (f :: * -> *) a b. Functor f => (a -> b) -> f a -> f b
<$> String -> [String] -> String -> IO String
forall (m :: * -> *).
MonadIO m =>
String -> [String] -> String -> m String
runProcessWithInput String
cmd [String]
args String
""
launcherPrompt :: XPConfig -> [XPMode] -> X()
launcherPrompt :: XPConfig -> [XPMode] -> X ()
launcherPrompt XPConfig
config [XPMode]
modes = [XPMode] -> XPConfig -> X ()
mkXPromptWithModes [XPMode]
modes XPConfig
config
defaultLauncherModes :: LauncherConfig -> [XPMode]
defaultLauncherModes :: LauncherConfig -> [XPMode]
defaultLauncherModes LauncherConfig
cnf = let
ph :: String
ph = LauncherConfig -> String
pathToHoogle LauncherConfig
cnf
in [ String -> String -> XPMode
hoogleMode String
ph (String -> XPMode) -> String -> XPMode
forall a b. (a -> b) -> a -> b
$ LauncherConfig -> String
browser LauncherConfig
cnf
, XPMode
calcMode]
hoogleMode :: FilePath -> String -> XPMode
hoogleMode :: String -> String -> XPMode
hoogleMode String
pathToHoogleBin String
browser' = HoogleMode -> XPMode
forall p. XPrompt p => p -> XPMode
XPT (HoogleMode -> XPMode) -> HoogleMode -> XPMode
forall a b. (a -> b) -> a -> b
$ String -> String -> HoogleMode
HMode String
pathToHoogleBin String
browser'
calcMode :: XPMode
calcMode :: XPMode
calcMode = CalculatorMode -> XPMode
forall p. XPrompt p => p -> XPMode
XPT CalculatorMode
CalcMode