-----------------------------------------------------------------------------
-- |
-- 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 String
x String -> X ()
_) = String
x
    completionFunction :: Dir -> ComplFunction
completionFunction Dir
_ = ComplFunction
getDirCompl
    modeAction :: Dir -> String -> String -> X ()
modeAction (Dir String
_ String -> X ()
f) String
buf 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 XPConfig
c String
prom 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 String
p 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 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 String
"bash" [] (String
"compgen -A directory " String -> String -> String
forall a. [a] -> [a] -> [a]
++ String
s String -> String -> String
forall a. [a] -> [a] -> [a]
++ String
"\n")

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