Skip to content

Commit

Permalink
Support ~ expansion in config file #15
Browse files Browse the repository at this point in the history
  • Loading branch information
erebe committed Nov 11, 2017
1 parent f097dd5 commit 72de5c3
Show file tree
Hide file tree
Showing 3 changed files with 12 additions and 4 deletions.
1 change: 1 addition & 0 deletions package.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -24,6 +24,7 @@ dependencies:
- utf8-string
- unix
- directory
- wordexp


executables:
Expand Down
12 changes: 9 additions & 3 deletions src/Main.hs
Original file line number Diff line number Diff line change
Expand Up @@ -19,6 +19,7 @@ import qualified System.Directory as Dir
import System.Environment (lookupEnv)
import System.IO (IOMode (..), openFile)
import System.Timeout (timeout)
import System.Wordexp.Simple


data Command = DAEMON | PRINT | COPY Text | CLEAR | HELP deriving (Show, Read)
Expand Down Expand Up @@ -128,12 +129,17 @@ getConfig = do

cfgStr <- (readFile cfgPath <&> T.strip . decodeUtf8) `catchAnyDeep` const mempty
let cfg = fromMaybe (defaultConfig home) (readMay cfgStr)

writeFile cfgPath (fromString $ show cfg)
return cfg

historyPath' <- expandHomeDir $ historyPath cfg
staticHistoryPath' <- expandHomeDir $ staticHistoryPath cfg

let cfg' = cfg {historyPath = historyPath', staticHistoryPath = staticHistoryPath'}
return cfg'

where
defaultConfig home = Config 25 (home </> ".cache/greenclip.history") (home </> ".cache/greenclip.staticHistory") False
expandHomeDir str = (fromMaybe str . listToMaybe <$> wordexp str) `catch` (\(_ :: SomeException) -> return str)


parseArgs :: [Text] -> Command
Expand All @@ -153,7 +159,7 @@ run cmd = do
-- Should rename COPY into ADVERTISE but as greenclip is already used I don't want to break configs
-- of other people
COPY sel -> advertiseSelection sel
HELP -> putStrLn $ "greenclip v2.0 -- Recyle your clipboard selections\n\n" <>
HELP -> putStrLn $ "greenclip v2.1 -- Recyle your clipboard selections\n\n" <>
"Available commands\n" <>
"daemon: Spawn the daemon that will listen to selections\n" <>
"print: Display all selections history\n" <>
Expand Down
3 changes: 2 additions & 1 deletion stack.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -40,7 +40,8 @@ packages:

# Dependency packages to be pulled from upstream that are not in the resolver
# (e.g., acme-missiles-0.3)
extra-deps: []
extra-deps:
- 'wordexp-0.2.0.0'

# Override default flag values for local packages and extra-deps
flags: {}
Expand Down

0 comments on commit 72de5c3

Please sign in to comment.