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 = forall a. a -> a
id
completionFunction :: CalculatorMode -> ComplFunction
completionFunction CalculatorMode
CalcMode String
s = if forall (t :: * -> *) a. Foldable t => t a -> Bool
null String
s then forall (m :: * -> *) a. Monad m => a -> m a
return [] else
String -> [String]
lines forall (f :: * -> *) a b. Functor f => (a -> b) -> f a -> f b
<$> 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
_ = 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
_ = 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 <- forall (m :: * -> *) a. MonadIO m => IO a -> m a
liftIO 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 <- forall (t :: * -> *) a. Foldable t => (a -> Bool) -> t a -> Maybe a
find (forall a. Maybe a -> Bool
isJust forall b c a. (b -> c) -> (a -> b) -> a -> c
. \String
complStr -> forall a. Eq a => [a] -> [a] -> Maybe Int
findSeqIndex String
complStr String
result) [String]
completionsWithLink
Int
i <- forall a. Eq a => [a] -> [a] -> Maybe Int
findSeqIndex String
s String
"http://"
forall (m :: * -> *) a. Monad m => a -> m a
return forall a b. (a -> b) -> a -> b
$ forall a. Int -> [a] -> [a]
drop Int
i String
s
case Maybe String
link of
Just String
l -> forall (m :: * -> *). MonadIO m => String -> m ()
spawn forall a b. (a -> b) -> a -> b
$ String
browser' forall a. [a] -> [a] -> [a]
++ String
" " forall a. [a] -> [a] -> [a]
++ String
l
Maybe String
_ -> forall (m :: * -> *) a. Monad m => a -> m a
return ()
where
findSeqIndex :: (Eq a) => [a] -> [a] -> Maybe Int
findSeqIndex :: forall a. Eq a => [a] -> [a] -> Maybe Int
findSeqIndex [a]
xs [a]
xss = forall a. (a -> Bool) -> [a] -> Maybe Int
findIndex (forall a. Eq a => [a] -> [a] -> Bool
isPrefixOf [a]
xss) forall a b. (a -> b) -> a -> b
$ 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 forall (f :: * -> *) a b. Functor f => (a -> b) -> f a -> f b
<$> 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 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' = forall p. XPrompt p => p -> XPMode
XPT forall a b. (a -> b) -> a -> b
$ String -> String -> HoogleMode
HMode String
pathToHoogleBin String
browser'
calcMode :: XPMode
calcMode :: XPMode
calcMode = forall p. XPrompt p => p -> XPMode
XPT CalculatorMode
CalcMode