REF clean up nondry code
This commit is contained in:
parent
c3fc38d785
commit
04c430efc6
|
@ -9,25 +9,16 @@ import RIO
|
|||
import RIO.Directory
|
||||
import qualified RIO.Text as T
|
||||
import Rofi.Command
|
||||
import Rofi.IO
|
||||
import System.FilePath.Posix
|
||||
import System.Process
|
||||
import UnliftIO.Environment
|
||||
|
||||
main :: IO ()
|
||||
main = runSimpleApp $ do
|
||||
runChecks
|
||||
checkExe "autorandr"
|
||||
getArgs >>= runPrompt
|
||||
|
||||
runChecks :: (MonadReader c m, HasLogFunc c, MonadIO m) => m ()
|
||||
runChecks = checkExe "autorandr" >> checkExe "rofi"
|
||||
|
||||
checkExe :: (MonadReader c m, HasLogFunc c, MonadIO m) => String -> m ()
|
||||
checkExe cmd = do
|
||||
res <- findExecutable cmd
|
||||
unless (isJust res) $ do
|
||||
logError $ displayBytesUtf8 $ encodeUtf8 $ T.append "Could not find executable: " $ T.pack cmd
|
||||
exitWith $ ExitFailure 1
|
||||
|
||||
newtype ARClientConf = ARClientConf [T.Text]
|
||||
|
||||
instance HasRofiConf ARClientConf where
|
||||
|
|
|
@ -18,8 +18,8 @@ module Main (main) where
|
|||
|
||||
import Bitwarden.Internal
|
||||
import RIO
|
||||
import RIO.Directory
|
||||
import qualified RIO.Text as T
|
||||
import Rofi.IO
|
||||
import UnliftIO.Environment
|
||||
|
||||
main :: IO ()
|
||||
|
@ -42,12 +42,4 @@ usage =
|
|||
]
|
||||
|
||||
runChecks :: (HasLogFunc c, MonadReader c m, MonadUnliftIO m) => m ()
|
||||
runChecks = checkExe "bw" >> checkExe "rofi"
|
||||
|
||||
-- TODO not DRY
|
||||
checkExe :: (HasLogFunc c, MonadReader c m, MonadUnliftIO m) => String -> m ()
|
||||
checkExe cmd = do
|
||||
res <- findExecutable cmd
|
||||
unless (isJust res) $ do
|
||||
logError $ displayBytesUtf8 $ encodeUtf8 $ T.append "Could not find executable: " $ T.pack cmd
|
||||
exitWith $ ExitFailure 1
|
||||
runChecks = checkExe "bw"
|
||||
|
|
|
@ -21,6 +21,7 @@ import qualified RIO.Map as M
|
|||
import qualified RIO.NonEmpty as NE
|
||||
import qualified RIO.Text as T
|
||||
import Rofi.Command
|
||||
import Rofi.IO
|
||||
import System.Console.GetOpt
|
||||
import System.FilePath.Posix
|
||||
import System.Posix.User (getEffectiveUserName)
|
||||
|
@ -684,7 +685,7 @@ getMTPDevices = do
|
|||
getMTPActions :: MIO [(Header, ProtoAction)]
|
||||
getMTPActions = mountableToAction getMTPDevices
|
||||
|
||||
mtpExeInstalled :: IO Bool
|
||||
mtpExeInstalled :: MonadIO m => m Bool
|
||||
mtpExeInstalled = isJust <$> findExecutable mtpExe
|
||||
|
||||
instance Actionable MTPFS where
|
||||
|
@ -695,28 +696,13 @@ instance Actionable MTPFS where
|
|||
--------------------------------------------------------------------------------
|
||||
-- Notifications
|
||||
|
||||
data NotifyIcon = IconError | IconInfo
|
||||
|
||||
instance Show NotifyIcon where
|
||||
show IconError = "dialog-error-symbolic"
|
||||
show IconInfo = "dialog-information-symbolic"
|
||||
|
||||
notifyMountResult :: Bool -> T.Text -> MountResult -> IO ()
|
||||
notifyMountResult :: MonadIO m => Bool -> T.Text -> MountResult -> m ()
|
||||
notifyMountResult mounted label result = case result of
|
||||
MountError e -> notify IconError (T.unwords ["Failed", "to", verb, label]) $ Just e
|
||||
MountSuccess -> notify IconInfo (T.concat ["Successfully ", verb, "ed ", label]) Nothing
|
||||
where
|
||||
verb = if mounted then "unmount" else "mount" :: T.Text
|
||||
|
||||
notify :: NotifyIcon -> T.Text -> Maybe T.Text -> IO ()
|
||||
notify icon summary body =
|
||||
void $
|
||||
spawnProcess "notify-send" $
|
||||
maybe args (\b -> args ++ [b]) $
|
||||
fmap T.unpack body
|
||||
where
|
||||
args = ["-i", show icon, T.unpack summary]
|
||||
|
||||
--------------------------------------------------------------------------------
|
||||
-- Mount commands
|
||||
|
||||
|
|
|
@ -7,7 +7,7 @@ module Main (main) where
|
|||
import RIO
|
||||
import qualified RIO.Text as T
|
||||
import Rofi.Command
|
||||
import System.Process
|
||||
import Rofi.IO
|
||||
import UnliftIO.Environment
|
||||
|
||||
main :: IO ()
|
||||
|
@ -46,7 +46,7 @@ getServers = do
|
|||
running <- daemonIsRunning
|
||||
if running
|
||||
then Just <$> getStatus
|
||||
else notify IconError "ExpressVPN daemon not running" >> return Nothing
|
||||
else notifyEVPN IconError "ExpressVPN daemon not running" >> return Nothing
|
||||
|
||||
getStatus :: MonadIO m => m VPNStatus
|
||||
getStatus = do
|
||||
|
@ -66,7 +66,7 @@ getAvailableServers :: MonadIO m => m [VPNServer]
|
|||
getAvailableServers = procOut =<< readCmdSuccess eVPN ["ls"] ""
|
||||
where
|
||||
procOut Nothing = do
|
||||
notify IconError "failed to get list of servers"
|
||||
notifyEVPN IconError "failed to get list of servers"
|
||||
return []
|
||||
-- ASSUME the output has a useless header that ends in a line that starts
|
||||
-- with "-----", after which is the stuff we care about, which is followed
|
||||
|
@ -138,19 +138,9 @@ disconnect server = do
|
|||
readCmdSuccess' :: MonadIO m => T.Text -> [T.Text] -> m Bool
|
||||
readCmdSuccess' cmd args = isJust <$> readCmdSuccess cmd args ""
|
||||
|
||||
-- TODO not DRY
|
||||
data NotifyIcon = IconError | IconInfo
|
||||
|
||||
instance Show NotifyIcon where
|
||||
show IconError = "dialog-error-symbolic"
|
||||
show IconInfo = "dialog-information-symbolic"
|
||||
|
||||
notifyIf :: MonadIO m => Bool -> T.Text -> T.Text -> m ()
|
||||
notifyIf True s _ = notify IconInfo s
|
||||
notifyIf False _ s = notify IconError s
|
||||
notifyIf True s _ = notifyEVPN IconInfo s
|
||||
notifyIf False _ s = notifyEVPN IconError s
|
||||
|
||||
notify :: MonadIO m => NotifyIcon -> T.Text -> m ()
|
||||
notify icon body = liftIO $ void $ spawnProcess "notify-send" $ args ++ [T.unpack body]
|
||||
where
|
||||
args = ["-i", show icon, summary]
|
||||
summary = "ExpressVPN"
|
||||
notifyEVPN :: MonadIO m => NotifyIcon -> T.Text -> m ()
|
||||
notifyEVPN icon = notify icon "ExpressVPN" . Just
|
||||
|
|
|
@ -0,0 +1,29 @@
|
|||
module Rofi.IO where
|
||||
|
||||
import RIO
|
||||
import RIO.Directory
|
||||
import qualified RIO.Text as T
|
||||
import System.Process
|
||||
|
||||
data NotifyIcon = IconError | IconInfo
|
||||
|
||||
instance Show NotifyIcon where
|
||||
show IconError = "dialog-error-symbolic"
|
||||
show IconInfo = "dialog-information-symbolic"
|
||||
|
||||
notify :: MonadIO m => NotifyIcon -> T.Text -> Maybe T.Text -> m ()
|
||||
notify icon summary body =
|
||||
liftIO $
|
||||
void $
|
||||
spawnProcess "notify-send" $
|
||||
maybe args (\b -> args ++ [b]) $
|
||||
fmap T.unpack body
|
||||
where
|
||||
args = ["-i", show icon, T.unpack summary]
|
||||
|
||||
checkExe :: (HasLogFunc c, MonadReader c m, MonadUnliftIO m) => FilePath -> m ()
|
||||
checkExe cmd = do
|
||||
res <- findExecutable cmd
|
||||
unless (isJust res) $ do
|
||||
logError $ displayBytesUtf8 $ encodeUtf8 $ T.append "Could not find executable: " $ T.pack cmd
|
||||
exitWith $ ExitFailure 1
|
|
@ -87,9 +87,6 @@ dependencies:
|
|||
|
||||
library:
|
||||
source-dirs: lib/
|
||||
exposed-modules:
|
||||
- Bitwarden.Internal
|
||||
- Rofi.Command
|
||||
|
||||
executables:
|
||||
pinentry-rofi:
|
||||
|
|
Loading…
Reference in New Issue