summaryrefslogtreecommitdiffhomepage
path: root/UI
diff options
context:
space:
mode:
authorJoey Hess <joeyh@joeyh.name>2016-09-01 11:43:01 -0400
committerJoey Hess <joeyh@joeyh.name>2016-09-01 11:43:01 -0400
commit3100ed23049f264e2fc43a02b8b65e7ce4578609 (patch)
treeab2cf2ce36b83893debc25ceffc769805c12c708 /UI
parentf6931d56b182c0cac2e3551c4baec4352c5185da (diff)
downloadkeysafe-3100ed23049f264e2fc43a02b8b65e7ce4578609.tar.gz
Fix bug that prevented keysafe --server from running when there was no controlling terminal and zenity was not installed.
Diffstat (limited to 'UI')
-rw-r--r--UI/NonInteractive.hs42
1 files changed, 42 insertions, 0 deletions
diff --git a/UI/NonInteractive.hs b/UI/NonInteractive.hs
new file mode 100644
index 0000000..f0010eb
--- /dev/null
+++ b/UI/NonInteractive.hs
@@ -0,0 +1,42 @@
+{- Copyright 2016 Joey Hess <id@joeyh.name>
+ -
+ - Licensed under the GNU AGPL version 3 or higher.
+ -}
+
+module UI.NonInteractive (noninteractiveUI) where
+
+import Types.UI
+import System.IO
+import Control.Exception
+
+noninteractiveUI :: UI
+noninteractiveUI = UI
+ { isAvailable = return True
+ , showError = myShowError
+ , showInfo = myShowInfo
+ , promptQuestion = myPrompt
+ , promptName = \t d _ -> myPrompt t d
+ , promptPassword = \b t d -> myPrompt t d b
+ , promptKeyId = myPrompt
+ , withProgress = myWithProgress
+ }
+
+myShowError :: Desc -> IO ()
+myShowError desc = hPutStrLn stderr $ "Error: " ++ desc
+
+myShowInfo :: Title -> Desc -> IO ()
+myShowInfo _title desc = putStrLn desc
+
+myPrompt :: Title -> Desc -> x -> IO a
+myPrompt _title desc _ = do
+ putStrLn desc
+ error "Not running at a terminal and zenity is not installed; cannot interact with user."
+
+myWithProgress :: Title -> Desc -> ((Percent -> IO ()) -> IO a) -> IO a
+myWithProgress _title desc a = bracket_ setup cleanup (a sendpercent)
+ where
+ setup = putStrLn desc
+ sendpercent p = do
+ putStr (show p ++ "% ")
+ hFlush stdout
+ cleanup = putStrLn "done"