More prompts
[xyz.git] / src / Prompts.hs
index aa54876..846d019 100644 (file)
@@ -1,12 +1,15 @@
-module Prompts (entryPrompt, initPrompt, pushPrompt) where
+module Prompts (entryPrompt, initPrompt, pushPrompt, editorPrompt) where
 
 import Util
 import Prelude hiding (getLine, null, putStrLn, putStr, unlines)
 import System.IO hiding (putStr, putStrLn, getLine)
-import Control.Monad (join)
+import Control.Monad (join, filterM)
+import Control.Exception (finally)
 import Data.Text (Text, unlines, append, toLower, null, pack, unpack)
 import Data.Text.IO (getLine, putStrLn, putStr, getLine)
 import Data.Maybe
+import System.Process
+import System.Exit
 
 getOneText :: IO Text
 getOneText = getChar >>= return . pack . return
@@ -43,13 +46,14 @@ yornPrompt xs = do
                    "y" -> Right True
                    "n" -> Right False
                    _ -> Left "Please answer with y or n"
-  prompt (xs +++ " (y/n): ") getOneText v
+  finally (prompt (xs +++ " (y/n): ") getOneText v) $ putStr "\n"
 
 listPrompt :: Text -> [Text] -> IO Text
-listPrompt xs options = do
-  putStrLn $ xs +++ "\n\n" +++ cue
-  prompt ("Make your selection " +++ validRange +++ ": ") getOneText validator where
+listPrompt xs options = finally (putCue >> choicePrompt) $ putStr "\n"
+  where
     cue = unlines $ map (\x -> (pack . show $ fst x) +++ ") " +++ snd x) themeList
+    putCue = putStrLn $ xs +++ "\n\n" +++ cue
+    choicePrompt = prompt ("Make your selection " +++ validRange +++ ": ") getOneText validator
     validator x = if choice > 0 && choice <= upper
       then Right (fromJust (lookup choice themeList))
       else Left $ "\nPick a valid number " +++ validRange where
@@ -69,11 +73,19 @@ entryPrompt = subPrompt entryNamePrompt videoPrompt (not . null) where
   videoPrompt = dependantTextPrompt "Video ID (leave blank to skip): " "Local video filename: "
 
 initPrompt :: [Text] -> IO [(String, Text)]
-initPrompt themes = fmap (zip ["projectName", "description", "theme"]) $ sequence prompts where
+initPrompt themes = fmap (zip ["name", "description", "theme"]) $ sequence prompts where
   prompts = [
     nonEmptyTextPrompt "Enter a name for your project: ",
     nonEmptyTextPrompt "Enter a short description for your project: ",
-    listPrompt "Which theme would you like to use?" themes
+    listPrompt "\nWhich theme would you like to use?" themes
             ]
 
+editorPrompt :: IO Text
+editorPrompt = filterM (system . unpack . (+++ " > /dev/null") . ("which " +++)  >>=  return . fmap codeToBool) commonEditors
+  >>= listPrompt "Which editor would you like to use? Here are some common ones I found on your system:" where
+    codeToBool x = case x of
+      ExitFailure _ -> False
+      ExitSuccess -> True
+    commonEditors =   ["vi", "vim", "nano", "emacs"]
+
 pushPrompt = yornPrompt "Do you want to push this entry?"