module XMonad.Prompt.Man (
manPrompt
, getCommandOutput
, Man
) where
import XMonad
import XMonad.Prelude
import XMonad.Prompt
import XMonad.Util.Run
import XMonad.Prompt.Shell (split)
import System.Directory
import System.FilePath (dropExtensions, (</>))
import System.IO
import System.Process
import qualified Control.Exception as E
data Man = Man
instance XPrompt Man where
showXPrompt :: Man -> String
showXPrompt Man
Man = String
"Manual page: "
manPrompt :: XPConfig -> X ()
manPrompt :: XPConfig -> X ()
manPrompt XPConfig
c = do
mans <- IO [String] -> X [String]
forall (m :: * -> *) a. MonadIO m => IO a -> m a
io IO [String]
getMans
mkXPrompt Man c (manCompl c mans) $ runInTerm "" . (++) "man "
getMans :: IO [String]
getMans :: IO [String]
getMans = do
paths <- do
let getout :: String -> IO String
getout String
cmd = String -> IO String
getCommandOutput String
cmd IO String -> (SomeException -> IO String) -> IO String
forall e a. Exception e => IO a -> (e -> IO a) -> IO a
`E.catch` \E.SomeException{} -> String -> IO String
forall a. a -> IO a
forall (m :: * -> *) a. Monad m => a -> m a
return String
""
p1 <- String -> IO String
getout String
"manpath -g 2>/dev/null"
p2 <- getout "manpath 2>/dev/null"
return $ intercalate ":" $ lines $ p1 ++ p2
let sects = [String
"man" String -> String -> String
forall a. [a] -> [a] -> [a]
++ Int -> String
forall a. Show a => a -> String
show Int
n | Int
n <- [Int
1..Int
9 :: Int]]
dirs = [String
d String -> String -> String
</> String
s | String
d <- Char -> String -> [String]
forall a. Eq a => a -> [a] -> [[a]]
split Char
':' String
paths, String
s <- [String]
sects]
mans <- forM (nub dirs) $ \String
d -> do
exists <- String -> IO Bool
doesDirectoryExist String
d
if exists
then map dropExtensions <$> getDirectoryContents d
else return []
return $ uniqSort $ concat mans
manCompl :: XPConfig -> [String] -> String -> IO [String]
manCompl :: XPConfig -> [String] -> ComplFunction
manCompl XPConfig
c [String]
mans String
s | String
s String -> String -> Bool
forall a. Eq a => a -> a -> Bool
== String
"" Bool -> Bool -> Bool
|| String -> Char
forall a. HasCallStack => [a] -> a
last String
s Char -> Char -> Bool
forall a. Eq a => a -> a -> Bool
== Char
' ' = [String] -> IO [String]
forall a. a -> IO a
forall (m :: * -> *) a. Monad m => a -> m a
return []
| Bool
otherwise = do
f <- String -> [String]
lines (String -> [String]) -> IO String -> IO [String]
forall (f :: * -> *) a b. Functor f => (a -> b) -> f a -> f b
<$> String -> IO String
getCommandOutput (String
"bash -c 'compgen -A file " String -> String -> String
forall a. [a] -> [a] -> [a]
++ String
s String -> String -> String
forall a. [a] -> [a] -> [a]
++ String
"'")
mkComplFunFromList c (f ++ mans) s
getCommandOutput :: String -> IO String
getCommandOutput :: String -> IO String
getCommandOutput String
s = do
(pin, pout, perr, _) <- String -> IO (Handle, Handle, Handle, ProcessHandle)
runInteractiveCommand String
s
hClose pin
output <- hGetContents pout
E.evaluate (length output)
hClose perr
return output