Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Use mkAbsolutePath in doAppend (XMonad.Prompt.AppendFile) #744

Merged
merged 2 commits into from
Aug 21, 2022
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
11 changes: 11 additions & 0 deletions CHANGES.md
Original file line number Diff line number Diff line change
Expand Up @@ -94,6 +94,11 @@

### Bug Fixes and Minor Changes

* `XMonad.Prompt.AppendFile`

- Use `XMonad.Prelude.mkAbsolutePath` to force names to be relative to the
home directory and support `~/` prefixes.

* `XMonad.Prompt.OrgMode`

- Fixes the date parsing issue such that entries with format of
Expand Down Expand Up @@ -184,6 +189,10 @@
- Added `WindowScreen`, which is a type synonym for the specialized `Screen`
type, that results from the `WindowSet` definition in `XMonad.Core`.

- Modified `mkAbsolutePath` to support a leading environment variable, so
things like `$HOME/NOTES` work. If you want more general environment
variable support, comment on [this PR].

* `XMonad.Util.XUtils`

- Added `withSimpleWindow`, `showSimpleWindow`, `WindowConfig`, and
Expand Down Expand Up @@ -221,6 +230,8 @@

- Added a `Default` instance for `UrgencyConfig` and `DzenUrgencyHook`.

[this PR]: https://github.com/xmonad/xmonad-contrib/pull/744

### Other changes

* Migrated the sample build scripts from the deprecated `xmonad-testing` repo to
Expand Down
26 changes: 17 additions & 9 deletions XMonad/Prelude.hs
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
{-# LANGUAGE BangPatterns #-}
{-# LANGUAGE LambdaCase #-}
{-# LANGUAGE BangPatterns #-}
{-# LANGUAGE LambdaCase #-}
{-# LANGUAGE ScopedTypeVariables #-}
--------------------------------------------------------------------
-- |
-- Module : XMonad.Prelude
Expand Down Expand Up @@ -60,6 +61,8 @@ import Data.List.NonEmpty (NonEmpty ((:|)))
import Data.Tuple (swap)
import GHC.Stack
import System.Directory (getHomeDirectory)
import System.Environment (getEnv)
import Control.Exception (SomeException, handle)
import qualified XMonad.StackSet as W

-- | Short for 'fromIntegral'.
Expand All @@ -80,7 +83,7 @@ chunksOf i xs = chunk : chunksOf i rest
(!?) xs n | n < 0 = Nothing
| otherwise = listToMaybe $ drop n xs

-- | Multivariant composition.
-- | Multivariable composition.
--
-- > f .: g ≡ (f .) . g ≡ \c d -> f (g c d)
(.:) :: (a -> b) -> (c -> d -> a) -> c -> d -> b
Expand Down Expand Up @@ -108,14 +111,19 @@ safeGetWindowAttributes w = withDisplay $ \dpy -> io . alloca $ \p ->
-- * If it starts with @~\/@, replace that with the actual home
-- * directory.
--
-- * Otherwise, prepend a @\/@ to the path.
-- * If it starts with @$@, read the name of an environment
-- * variable and replace it with the contents of that.
--
-- * Otherwise, prepend the home directory and @\/@ to the path.
mkAbsolutePath :: MonadIO m => FilePath -> m FilePath
mkAbsolutePath ps = do
home <- liftIO getHomeDirectory
pure $ case ps of
'/' : _ -> ps
'~' : '/' : _ -> home <> drop 1 ps
_ -> home <> ('/' : ps)
home <- io getHomeDirectory
case ps of
'/' : _ -> pure ps
'~' : '/' : _ -> pure (home <> drop 1 ps)
'$' : _ -> let (v,ps') = span (`elem` ("_"<>['A'..'Z']<>['a'..'z']<>['0'..'9'])) (drop 1 ps)
geekosaur marked this conversation as resolved.
Show resolved Hide resolved
in io ((\(_ :: SomeException) -> pure "") `handle` getEnv v) Exports.<&> (<> ps')
_ -> pure (home <> ('/' : ps))
{-# SPECIALISE mkAbsolutePath :: FilePath -> IO FilePath #-}
{-# SPECIALISE mkAbsolutePath :: FilePath -> X FilePath #-}

Expand Down
3 changes: 2 additions & 1 deletion XMonad/Prompt/AppendFile.hs
Original file line number Diff line number Diff line change
Expand Up @@ -30,6 +30,7 @@ module XMonad.Prompt.AppendFile (

import XMonad.Core
import XMonad.Prompt
import XMonad.Prelude (mkAbsolutePath)

import System.IO

Expand Down Expand Up @@ -91,4 +92,4 @@ appendFilePrompt' c trans fn = mkXPrompt (AppendFile fn)

-- | Append a string to a file.
doAppend :: (String -> String) -> FilePath -> String -> X ()
doAppend trans fn = io . withFile fn AppendMode . flip hPutStrLn . trans
doAppend trans fn s = mkAbsolutePath fn >>= \f -> (io . withFile f AppendMode . flip hPutStrLn . trans) s