-----------------------------------------------------------------------------
-- |
-- Module      :  XMonad.Prompt.Directory
-- Copyright   :  (C) 2007 Andrea Rossato, David Roundy
-- License     :  BSD3
--
-- Maintainer  :
-- Stability   :  unstable
-- Portability :  unportable
--
-- A directory prompt for XMonad
--
-----------------------------------------------------------------------------

module XMonad.Prompt.Directory (
                             -- * Usage
                             -- $usage
                             directoryPrompt,
                             directoryMultipleModes,
                             Dir
                              ) where

import XMonad
import XMonad.Prompt
import XMonad.Util.Run ( runProcessWithInput )

-- $usage
-- For an example usage see "XMonad.Layout.WorkspaceDir"

data Dir = Dir String (String -> X ())

instance XPrompt Dir where
    showXPrompt :: Dir -> String
showXPrompt (Dir x :: String
x _) = String
x
    completionFunction :: Dir -> ComplFunction
completionFunction _ = ComplFunction
getDirCompl
    modeAction :: Dir -> String -> String -> X ()
modeAction (Dir _ f :: String -> X ()
f) buf :: String
buf auto :: String
auto =
      let dir :: String
dir = if String -> Bool
forall (t :: * -> *) a. Foldable t => t a -> Bool
null String
auto then String
buf else String
auto
      in String -> X ()
f String
dir

directoryPrompt :: XPConfig -> String -> (String -> X ()) -> X ()
directoryPrompt :: XPConfig -> String -> (String -> X ()) -> X ()
directoryPrompt c :: XPConfig
c prom :: String
prom f :: String -> X ()
f = Dir -> XPConfig -> ComplFunction -> (String -> X ()) -> X ()
forall p.
XPrompt p =>
p -> XPConfig -> ComplFunction -> (String -> X ()) -> X ()
mkXPrompt (String -> (String -> X ()) -> Dir
Dir String
prom String -> X ()
f) XPConfig
c ComplFunction
getDirCompl String -> X ()
f

-- | A @XPType@ entry suitable for using with @mkXPromptWithModes@.
directoryMultipleModes :: String            -- ^ Prompt.
                       -> (String -> X ())  -- ^ Action.
                       -> XPType
directoryMultipleModes :: String -> (String -> X ()) -> XPType
directoryMultipleModes p :: String
p f :: String -> X ()
f = Dir -> XPType
forall p. XPrompt p => p -> XPType
XPT (String -> (String -> X ()) -> Dir
Dir String
p String -> X ()
f)

getDirCompl :: String -> IO [String]
getDirCompl :: ComplFunction
getDirCompl s :: String
s = ((String -> Bool) -> [String] -> [String]
forall a. (a -> Bool) -> [a] -> [a]
filter String -> Bool
notboring ([String] -> [String])
-> (String -> [String]) -> String -> [String]
forall b c a. (b -> c) -> (a -> b) -> a -> c
. String -> [String]
lines) (String -> [String]) -> IO String -> IO [String]
forall (f :: * -> *) a b. Functor f => (a -> b) -> f a -> f b
`fmap`
                String -> [String] -> String -> IO String
forall (m :: * -> *).
MonadIO m =>
String -> [String] -> String -> m String
runProcessWithInput "bash" [] ("compgen -A directory " String -> String -> String
forall a. [a] -> [a] -> [a]
++ String
s String -> String -> String
forall a. [a] -> [a] -> [a]
++ "\n")

notboring :: String -> Bool
notboring :: String -> Bool
notboring ('.':'.':_) = Bool
True
notboring ('.':_) = Bool
False
notboring _ = Bool
True