From ef5977c86b6080ef38a51af1ed16d8588786a7ea Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Romain=20G=C3=89RARD?= Date: Sun, 2 Sep 2018 23:31:47 +0200 Subject: [PATCH] Add option to not trim spaces from selection --- src/Clipboard.hs | 3 +-- src/Main.hs | 26 +++++++++++++++++++------- 2 files changed, 20 insertions(+), 9 deletions(-) diff --git a/src/Clipboard.hs b/src/Clipboard.hs index 95e7465..a06327a 100644 --- a/src/Clipboard.hs +++ b/src/Clipboard.hs @@ -23,7 +23,6 @@ import System.IO (hClose, stderr, stdin, stdout) import System.Posix.Process (forkProcess) import Data.ByteString (unpack) -import qualified Data.Text as T import Foreign (alloca, castPtr, peek, peekArray) import Foreign.C.Types (CUChar) import Foreign.Marshal.Array (withArrayLen) @@ -162,7 +161,7 @@ getSelection ctx@XorgContext{..} clipboard = do if mimeTarget == unsafeIndex mimesPriorities 0 then PNG selContent else if mimeTarget == unsafeIndex mimesPriorities 1 then JPEG selContent else if mimeTarget == unsafeIndex mimesPriorities 2 then BITMAP selContent - else UTF8 (T.strip $ toS selContent) + else UTF8 $ toS selContent -- getContentIncrementally acc = do -- _ <- xDeleteProperty display ownWindow selectionTarget diff --git a/src/Main.hs b/src/Main.hs index d9e6c5e..4a61e07 100644 --- a/src/Main.hs +++ b/src/Main.hs @@ -40,6 +40,7 @@ data Config = Config , imageCachePath :: Text , usePrimarySelectionAsInput :: Bool , blacklistedApps :: [Text] + , trimSpaceFromSelection :: Bool } deriving (Show, Read) type ClipHistory = Vector Clip.Selection @@ -76,13 +77,15 @@ storeHistory history = do appendToHistory :: (MonadIO m, MonadReader Config m) => Clip.Selection -> ClipHistory -> m (ClipHistory, ClipHistory) -appendToHistory sel history' = +appendToHistory sel history' = do + trimSelection <- view $ to trimSpaceFromSelection case sel of - Clip.Selection _ (Clip.UTF8 _) -> appendGeneric sel history' + Clip.Selection appName (Clip.UTF8 txt) -> appendGeneric (if trimSelection then Clip.Selection appName (Clip.UTF8 (T.strip txt)) else sel) history' Clip.Selection _ (Clip.PNG bytes) -> appendImage Clip.PNG ".png" bytes Clip.Selection _ (Clip.JPEG bytes) -> appendImage Clip.JPEG ".jpeg" bytes Clip.Selection _ (Clip.BITMAP bytes) -> appendImage Clip.BITMAP ".bmp" bytes + where appendImage imgCtr extension bytes = do cachePth <- view (to imageCachePath) @@ -217,10 +220,19 @@ getConfig = do let cfgPath = home <> "/.config/greenclip.cfg" cfgStr <- readFile cfgPath `catchAll` const mempty - let unprettyCfg = cfgStr & T.strip . T.replace "\n" "" . toS - let cfg = fromMaybe defaultConfig (readMaybe $ toS unprettyCfg) - let prettyCfg = show cfg & T.replace "," ",\n" . T.replace "{" "{\n " . T.replace "}" "\n}" - writeFile cfgPath prettyCfg + + let unprettyCfg' = cfgStr & T.strip . T.replace "\n" "" . toS + let unprettyCfg = if "trimSpaceFromSelection" `T.isInfixOf` unprettyCfg' + then unprettyCfg' + else T.replace "}" ", trimSpaceFromSelection = True }" unprettyCfg' + let cfgMaybe = readMaybe $ toS unprettyCfg + let cfg = fromMaybe defaultConfig cfgMaybe + + -- Write back the config file if the current one was invalid + _ <- if isNothing cfgMaybe || unprettyCfg /= unprettyCfg' + then let prettyCfg = show cfg & T.replace "," ",\n" . T.replace "{" "{\n " . T.replace "}" "\n}" + in writeFile cfgPath prettyCfg + else return () historyPath' <- expandHomeDir $ cfg ^. to historyPath staticHistoryPath' <- expandHomeDir $ cfg ^. to staticHistoryPath @@ -232,7 +244,7 @@ getConfig = do } where - defaultConfig = Config 50 "~/.cache/greenclip.history" "~/.cache/greenclip.staticHistory" "/tmp/" False [] + defaultConfig = Config 50 "~/.cache/greenclip.history" "~/.cache/greenclip.staticHistory" "/tmp/" False [] True expandHomeDir str = (toS . fromMaybe (toS str) . listToMaybe <$> wordexp (toS str)) `catchAll` (\_ -> return $ toS str)