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