Skip to content

Commit

Permalink
Add option to not trim spaces from selection
Browse files Browse the repository at this point in the history
  • Loading branch information
Romain GÉRARD committed Sep 2, 2018
1 parent 85ec183 commit ef5977c
Show file tree
Hide file tree
Showing 2 changed files with 20 additions and 9 deletions.
3 changes: 1 addition & 2 deletions src/Clipboard.hs
Original file line number Diff line number Diff line change
Expand Up @@ -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)
Expand Down Expand Up @@ -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
Expand Down
26 changes: 19 additions & 7 deletions src/Main.hs
Original file line number Diff line number Diff line change
Expand Up @@ -40,6 +40,7 @@ data Config = Config
, imageCachePath :: Text
, usePrimarySelectionAsInput :: Bool
, blacklistedApps :: [Text]
, trimSpaceFromSelection :: Bool
} deriving (Show, Read)

type ClipHistory = Vector Clip.Selection
Expand Down Expand Up @@ -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)
Expand Down Expand Up @@ -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
Expand All @@ -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)


Expand Down

0 comments on commit ef5977c

Please sign in to comment.