1 {-# LANGUAGE DeriveGeneric #-}
2 {-# LANGUAGE OverloadedStrings #-}
4 -- I do not know haskell, this code is probably shit
7 import Data.Aeson.Encode.Pretty
8 import qualified Data.ByteString.Lazy as B
11 import qualified Data.Text as T
15 import GHC.Float (double2Float)
17 import System.Console.Haskeline
18 import System.Environment (getEnv)
22 import Text.Printf (printf)
29 deriving (Generic, Show)
31 instance ToJSON File where
32 toEncoding = genericToEncoding defaultOptions
34 data Program = Program
38 deriving (Generic, Show)
40 instance ToJSON Program where
41 toEncoding = genericToEncoding defaultOptions
43 save :: Program -> IO ()
45 let path = ("./programs/" ++ (T.unpack (name program)) ++ ".json")
46 B.writeFile path (encodePretty program)
50 id <- toString <$> Data.UUID.V4.nextRandom
51 editor <- appendFile ("/tmp/xdg-ninja." ++ id ++ ".md") "Export the following environment variables:\n\n```bash\n\n```" >> (getEnv "EDITOR")
52 (_, _, _, p) <- createProcess (shell (editor ++ " /tmp/xdg-ninja." ++ id ++ ".md"))
55 ExitSuccess -> readFile ("/tmp/xdg-ninja." ++ id ++ ".md")
56 ExitFailure a -> return ""
58 getProp :: T.Text -> T.Text -> IO String
59 getProp prompt placeholder = do
60 let string_prompt = T.unpack prompt
61 let string_placholder = T.unpack placeholder
62 x <- runInputT defaultSettings (getInputLineWithInitial string_prompt (string_placholder, ""))
67 data Answer = Yes | No | Unknown
69 stringToBool :: String -> Answer
70 stringToBool s = case lower s of
79 promptBool :: T.Text -> T.Text -> T.Text -> IO Bool
80 promptBool prompt prompt_unrecognised placeholder = do
81 x <- getProp prompt placeholder
82 case stringToBool x of
85 Unknown -> printf "%s\n" prompt_unrecognised >> promptBool prompt prompt_unrecognised placeholder
89 path <- getProp (blue "Path to file: ") "$HOME/."
90 movable <- promptBool (blue "Can the file be moved? (y/n) ") (red "Please provide a valid answer.") "y"
92 return File {path = path, movable = movable, help = help}
94 getFiles :: [File] -> IO [File]
96 if Data.List.Extra.null files
99 getFiles (newFile : files)
101 new <- promptBool (green "Add another file? (y/n) ") (red "Please provide a valid answer.") ""
105 getFiles (newFile : files)
108 getProgram :: IO Program
110 name <- printf "%s\n" (T.unpack (bold (cyan "XDG-ninja Configuration Wizard")))
111 >> printf "%s\n" (T.unpack (faint (italic (cyan "First, tell me what program you're creating a configuration for."))))
112 >> getProp (yellow "Program name: ") ""
113 files <- printf "%s\n" (T.unpack (faint (italic (cyan "Alright, now let's configure which files belong to this program."))))
114 >> 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 ~."))))
115 >> 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."))))
116 >> 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."))))
118 return Program {name = T.pack name, files = files}
122 program <- getProgram
123 do_save <- promptBool (green "Save? (y/n) ") (red "Please provide a valid answer.") ""