REF clean up nondry code

This commit is contained in:
Nathan Dwarshuis 2023-02-22 23:02:04 -05:00
parent c3fc38d785
commit 04c430efc6
6 changed files with 43 additions and 58 deletions

View File

@ -9,25 +9,16 @@ import RIO
import RIO.Directory import RIO.Directory
import qualified RIO.Text as T import qualified RIO.Text as T
import Rofi.Command import Rofi.Command
import Rofi.IO
import System.FilePath.Posix import System.FilePath.Posix
import System.Process import System.Process
import UnliftIO.Environment import UnliftIO.Environment
main :: IO () main :: IO ()
main = runSimpleApp $ do main = runSimpleApp $ do
runChecks checkExe "autorandr"
getArgs >>= runPrompt 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] newtype ARClientConf = ARClientConf [T.Text]
instance HasRofiConf ARClientConf where instance HasRofiConf ARClientConf where

View File

@ -18,8 +18,8 @@ module Main (main) where
import Bitwarden.Internal import Bitwarden.Internal
import RIO import RIO
import RIO.Directory
import qualified RIO.Text as T import qualified RIO.Text as T
import Rofi.IO
import UnliftIO.Environment import UnliftIO.Environment
main :: IO () main :: IO ()
@ -42,12 +42,4 @@ usage =
] ]
runChecks :: (HasLogFunc c, MonadReader c m, MonadUnliftIO m) => m () runChecks :: (HasLogFunc c, MonadReader c m, MonadUnliftIO m) => m ()
runChecks = checkExe "bw" >> checkExe "rofi" runChecks = checkExe "bw"
-- 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

View File

@ -21,6 +21,7 @@ import qualified RIO.Map as M
import qualified RIO.NonEmpty as NE import qualified RIO.NonEmpty as NE
import qualified RIO.Text as T import qualified RIO.Text as T
import Rofi.Command import Rofi.Command
import Rofi.IO
import System.Console.GetOpt import System.Console.GetOpt
import System.FilePath.Posix import System.FilePath.Posix
import System.Posix.User (getEffectiveUserName) import System.Posix.User (getEffectiveUserName)
@ -684,7 +685,7 @@ getMTPDevices = do
getMTPActions :: MIO [(Header, ProtoAction)] getMTPActions :: MIO [(Header, ProtoAction)]
getMTPActions = mountableToAction getMTPDevices getMTPActions = mountableToAction getMTPDevices
mtpExeInstalled :: IO Bool mtpExeInstalled :: MonadIO m => m Bool
mtpExeInstalled = isJust <$> findExecutable mtpExe mtpExeInstalled = isJust <$> findExecutable mtpExe
instance Actionable MTPFS where instance Actionable MTPFS where
@ -695,28 +696,13 @@ instance Actionable MTPFS where
-------------------------------------------------------------------------------- --------------------------------------------------------------------------------
-- Notifications -- Notifications
data NotifyIcon = IconError | IconInfo notifyMountResult :: MonadIO m => Bool -> T.Text -> MountResult -> m ()
instance Show NotifyIcon where
show IconError = "dialog-error-symbolic"
show IconInfo = "dialog-information-symbolic"
notifyMountResult :: Bool -> T.Text -> MountResult -> IO ()
notifyMountResult mounted label result = case result of notifyMountResult mounted label result = case result of
MountError e -> notify IconError (T.unwords ["Failed", "to", verb, label]) $ Just e MountError e -> notify IconError (T.unwords ["Failed", "to", verb, label]) $ Just e
MountSuccess -> notify IconInfo (T.concat ["Successfully ", verb, "ed ", label]) Nothing MountSuccess -> notify IconInfo (T.concat ["Successfully ", verb, "ed ", label]) Nothing
where where
verb = if mounted then "unmount" else "mount" :: T.Text 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 -- Mount commands

View File

@ -7,7 +7,7 @@ module Main (main) where
import RIO import RIO
import qualified RIO.Text as T import qualified RIO.Text as T
import Rofi.Command import Rofi.Command
import System.Process import Rofi.IO
import UnliftIO.Environment import UnliftIO.Environment
main :: IO () main :: IO ()
@ -46,7 +46,7 @@ getServers = do
running <- daemonIsRunning running <- daemonIsRunning
if running if running
then Just <$> getStatus 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 :: MonadIO m => m VPNStatus
getStatus = do getStatus = do
@ -66,7 +66,7 @@ getAvailableServers :: MonadIO m => m [VPNServer]
getAvailableServers = procOut =<< readCmdSuccess eVPN ["ls"] "" getAvailableServers = procOut =<< readCmdSuccess eVPN ["ls"] ""
where where
procOut Nothing = do procOut Nothing = do
notify IconError "failed to get list of servers" notifyEVPN IconError "failed to get list of servers"
return [] return []
-- ASSUME the output has a useless header that ends in a line that starts -- 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 -- 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' :: MonadIO m => T.Text -> [T.Text] -> m Bool
readCmdSuccess' cmd args = isJust <$> readCmdSuccess cmd args "" 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 :: MonadIO m => Bool -> T.Text -> T.Text -> m ()
notifyIf True s _ = notify IconInfo s notifyIf True s _ = notifyEVPN IconInfo s
notifyIf False _ s = notify IconError s notifyIf False _ s = notifyEVPN IconError s
notify :: MonadIO m => NotifyIcon -> T.Text -> m () notifyEVPN :: MonadIO m => NotifyIcon -> T.Text -> m ()
notify icon body = liftIO $ void $ spawnProcess "notify-send" $ args ++ [T.unpack body] notifyEVPN icon = notify icon "ExpressVPN" . Just
where
args = ["-i", show icon, summary]
summary = "ExpressVPN"

29
lib/Rofi/IO.hs Normal file
View File

@ -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

View File

@ -87,9 +87,6 @@ dependencies:
library: library:
source-dirs: lib/ source-dirs: lib/
exposed-modules:
- Bitwarden.Internal
- Rofi.Command
executables: executables:
pinentry-rofi: pinentry-rofi: