]> glassweightruler.freedombox.rocks Git - xdg-ninja.git/commitdiff
Replace >>= with do-notation
authorSebastian Willenbrink <sebastian.willenbrink@tum.de>
Mon, 16 May 2022 22:08:16 +0000 (00:08 +0200)
committerSebastian Willenbrink <sebastian.willenbrink@tum.de>
Tue, 17 May 2022 12:05:18 +0000 (14:05 +0200)
app/add-program.hs

index 4d71ea6289e6b788cb054768a5ffb762867d47c7..134934972a3dfe875ebab117111dda66b14f5ecb 100644 (file)
@@ -46,29 +46,23 @@ save program = do
   B.writeFile path (encodePretty program)
 
 getHelp :: IO String
   B.writeFile path (encodePretty program)
 
 getHelp :: IO String
-getHelp =
-  toString <$> Data.UUID.V4.nextRandom >>= \id ->
-    appendFile ("/tmp/xdg-ninja." ++ id ++ ".md") "Export the following environment variables:\n\n```bash\n\n```"
-      >> (getEnv "EDITOR") >>= \editor ->
-        createProcess (shell (editor ++ " /tmp/xdg-ninja." ++ id ++ ".md")) >>= \r ->
-          case r of
-            (_, _, _, p) ->
-              waitForProcess p
-                >>= ( \f -> case f of
-                        ExitSuccess -> readFile ("/tmp/xdg-ninja." ++ id ++ ".md")
-                        (ExitFailure a) -> return ""
-                    )
+getHelp = do
+  id <- toString <$> Data.UUID.V4.nextRandom
+  editor <- appendFile ("/tmp/xdg-ninja." ++ id ++ ".md") "Export the following environment variables:\n\n```bash\n\n```" >> (getEnv "EDITOR")
+  (_, _, _, p) <- createProcess (shell (editor ++ " /tmp/xdg-ninja." ++ id ++ ".md"))
+  f <- waitForProcess p
+  case f of
+    ExitSuccess -> readFile ("/tmp/xdg-ninja." ++ id ++ ".md")
+    ExitFailure a -> return ""
 
 getProp :: T.Text -> T.Text -> IO String
 getProp prompt placeholder = do
   let string_prompt = T.unpack prompt
   let string_placholder = T.unpack placeholder
 
 getProp :: T.Text -> T.Text -> IO String
 getProp prompt placeholder = do
   let string_prompt = T.unpack prompt
   let string_placholder = T.unpack placeholder
-  let fuck = runInputT defaultSettings (getInputLineWithInitial string_prompt (string_placholder, ""))
-  fuck
-    >>= ( \x -> case x of
-            (Just s) -> return s
-            Nothing -> return ""
-        )
+  x <- runInputT defaultSettings (getInputLineWithInitial string_prompt (string_placholder, ""))
+  case x of
+    Just s -> return s
+    Nothing -> return ""
 
 data Answer = Yes | No | Unknown
 
 
 data Answer = Yes | No | Unknown
 
@@ -83,48 +77,50 @@ stringToBool s = case lower s of
   _ -> Unknown
 
 promptBool :: T.Text -> T.Text -> T.Text -> IO Bool
   _ -> Unknown
 
 promptBool :: T.Text -> T.Text -> T.Text -> IO Bool
-promptBool prompt prompt_unrecognised placeholder =
-  getProp prompt placeholder >>= \x -> case stringToBool x of
+promptBool prompt prompt_unrecognised placeholder = do
+  x <- getProp prompt placeholder
+  case stringToBool x of
     Yes -> return True
     No -> return False
     Unknown -> printf "%s\n" prompt_unrecognised >> promptBool prompt prompt_unrecognised placeholder
 
 getFile :: IO File
     Yes -> return True
     No -> return False
     Unknown -> printf "%s\n" prompt_unrecognised >> promptBool prompt prompt_unrecognised placeholder
 
 getFile :: IO File
-getFile =
-  getProp (blue "Path to file: ") "$HOME/."
-    >>= \path ->
-      promptBool (blue "Can the file be moved? (y/n) ") (red "Please provide a valid answer.") "y"
-        >>= \movable ->
-          getHelp
-            >>= \help -> return File {path = path, movable = movable, help = help}
+getFile = do
+  path <- getProp (blue "Path to file: ") "$HOME/."
+  movable <- promptBool (blue "Can the file be moved? (y/n) ") (red "Please provide a valid answer.") "y"
+  help <- getHelp
+  return File {path = path, movable = movable, help = help}
 
 getFiles :: [File] -> IO [File]
 getFiles files =
   if Data.List.Extra.null files
 
 getFiles :: [File] -> IO [File]
 getFiles files =
   if Data.List.Extra.null files
-    then getFile >>= \newFile -> getFiles (newFile : files)
-    else
-      promptBool (green "Add another file? (y/n) ") (red "Please provide a valid answer.") "" >>= \new ->
-        if new
-          then getFile >>= \newFile -> getFiles (newFile : files)
-          else return files
+  then do
+    newFile <- getFile
+    getFiles (newFile : files)
+  else do
+    new <- promptBool (green "Add another file? (y/n) ") (red "Please provide a valid answer.") ""
+    if new
+    then do
+      newFile <- getFile
+      getFiles (newFile : files)
+    else return files
 
 getProgram :: IO Program
 
 getProgram :: IO Program
-getProgram =
-  printf "%s\n" (T.unpack (bold (cyan "XDG-ninja Configuration Wizard")))
-    >> printf "%s\n" (T.unpack (faint (italic (cyan "First, tell me what program you're creating a configuration for."))))
-    >> getProp (yellow "Program name: ") ""
-    >>= \name ->
-      printf "%s\n" (T.unpack (faint (italic (cyan "Alright, now let's configure which files belong to this program."))))
-        >> printf "%s\n" (T.unpack (faint (italic (cyan "I'm going to ask you for the path to the file, please use $HOME instead of ~."))))
-        >> printf "%s\n" (T.unpack (faint (italic (cyan "I'll then ask you wether or not this file can be moved to a different directory."))))
-        >> printf "%s\n" (T.unpack (faint (italic (cyan "Finally, your editor is going to open a markdown document. Enter instructions on moving the file in question, then save and close."))))
-        >> getFiles [] >>= \files ->
-          return Program {name = T.pack name, files = files}
+getProgram = do
+  name <- printf "%s\n" (T.unpack (bold (cyan "XDG-ninja Configuration Wizard")))
+          >> printf "%s\n" (T.unpack (faint (italic (cyan "First, tell me what program you're creating a configuration for."))))
+          >> getProp (yellow "Program name: ") ""
+  files <- printf "%s\n" (T.unpack (faint (italic (cyan "Alright, now let's configure which files belong to this program."))))
+           >> printf "%s\n" (T.unpack (faint (italic (cyan "I'm going to ask you for the path to the file, please use $HOME instead of ~."))))
+           >> printf "%s\n" (T.unpack (faint (italic (cyan "I'll then ask you wether or not this file can be moved to a different directory."))))
+           >> printf "%s\n" (T.unpack (faint (italic (cyan "Finally, your editor is going to open a markdown document. Enter instructions on moving the file in question, then save and close."))))
+           >> getFiles []
+  return Program {name = T.pack name, files = files}
 
 main :: IO ()
 
 main :: IO ()
-main =
-  getProgram >>= \program ->
-    promptBool (green "Save? (y/n) ") (red "Please provide a valid answer.") "" >>= \do_save ->
-      if do_save
-        then save program
-        else return ()
+main = do
+  program <- getProgram
+  do_save <- promptBool (green "Save? (y/n) ") (red "Please provide a valid answer.") ""
+  if do_save
+  then save program
+  else return ()