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 toString <$> Data.UUID.V4.nextRandom >>= \id ->
51 appendFile ("/tmp/xdg-ninja." ++ id ++ ".md") "Export the following environment variables:\n\n```bash\n\n```"
52 >> (getEnv "EDITOR") >>= \editor ->
53 createProcess (shell (editor ++ " /tmp/xdg-ninja." ++ id ++ ".md")) >>= \r ->
58 ExitSuccess -> readFile ("/tmp/xdg-ninja." ++ id ++ ".md")
59 (ExitFailure a) -> return ""
62 getProp :: T.Text -> T.Text -> IO String
63 getProp prompt placeholder = do
64 let string_prompt = T.unpack prompt
65 let string_placholder = T.unpack placeholder
66 let fuck = runInputT defaultSettings (getInputLineWithInitial string_prompt (string_placholder, ""))
73 data Answer = Yes | No | Unknown
75 stringToBool :: String -> Answer
76 stringToBool s = case lower s of
85 promptBool :: T.Text -> T.Text -> T.Text -> IO Bool
86 promptBool prompt prompt_unrecognised placeholder =
87 getProp prompt placeholder >>= \x -> case stringToBool x of
90 Unknown -> printf "%s\n" prompt_unrecognised >> promptBool prompt prompt_unrecognised placeholder
94 getProp (blue "Path to file: ") "$HOME/."
96 promptBool (blue "Can the file be moved? (y/n) ") (red "Please provide a valid answer.") "y"
99 >>= \help -> return File {path = path, movable = movable, help = help}
101 getFiles :: [File] -> IO [File]
103 if Data.List.Extra.null files
104 then getFile >>= \newFile -> getFiles (newFile : files)
106 promptBool (green "Add another file? (y/n) ") (red "Please provide a valid answer.") "" >>= \new ->
108 then getFile >>= \newFile -> getFiles (newFile : files)
111 getProgram :: IO Program
113 printf "%s\n" (T.unpack (bold (cyan "XDG-ninja Configuration Wizard")))
114 >> printf "%s\n" (T.unpack (faint (italic (cyan "First, tell me what program you're creating a configuration for."))))
115 >> getProp (yellow "Program name: ") ""
117 printf "%s\n" (T.unpack (faint (italic (cyan "Alright, now let's configure which files belong to this program."))))
118 >> 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 ~."))))
119 >> 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."))))
120 >> 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."))))
121 >> getFiles [] >>= \files ->
122 return Program {name = T.pack name, files = files}
126 getProgram >>= \program ->
127 promptBool (green "Save? (y/n) ") (red "Please provide a valid answer.") "" >>= \do_save ->