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
21 import Text.Printf (printf)
28 deriving (Generic, Show)
30 instance ToJSON File where
31 toEncoding = genericToEncoding defaultOptions
33 data Program = Program
37 deriving (Generic, Show)
39 instance ToJSON Program where
40 toEncoding = genericToEncoding defaultOptions
42 save :: Program -> IO ()
44 let path = ( "./programs/" ++ (T.unpack (name program) ) ++ ".json")
45 B.writeFile path (encodePretty program)
49 toString <$> Data.UUID.V4.nextRandom >>= \id ->
50 appendFile ( "/tmp/xdg-ninja." ++ id ++ ".md" ) "Export the following environment variables:\n\n```bash\n\n```" >>
51 createProcess (shell ("nvim /tmp/xdg-ninja." ++ id ++ ".md")) >>= \r ->
56 ExitSuccess -> readFile ("/tmp/xdg-ninja." ++ id ++ ".md")
57 (ExitFailure a) -> return ""
60 getProp :: T.Text -> T.Text -> IO String
61 getProp prompt placeholder = do
62 let string_prompt = T.unpack prompt
63 let string_placholder = T.unpack placeholder
64 let fuck = runInputT defaultSettings (getInputLineWithInitial string_prompt (string_placholder, ""))
71 data Answer = Yes | No | Unknown
73 stringToBool :: String -> Answer
74 stringToBool s = case lower s of
83 promptBool :: T.Text -> T.Text -> T.Text -> IO Bool
84 promptBool prompt prompt_unrecognised placeholder =
85 getProp prompt placeholder >>= \x -> case stringToBool x of
88 Unknown -> printf "%s\n" prompt_unrecognised >> promptBool prompt prompt_unrecognised placeholder
92 getProp (blue "Path to file: ") "$HOME/."
94 promptBool (blue "Can the file be moved? (y/n) ") (red "Please provide a valid answer.") "y"
97 >>= \help -> return File {path = path, movable = movable, help = help}
99 getFiles :: [File] -> IO [File]
101 if Data.List.Extra.null files
102 then getFile >>= \newFile -> getFiles (newFile : files)
104 promptBool (green "Add another file? (y/n) ") (red "Please provide a valid answer.") "" >>= \new ->
106 then getFile >>= \newFile -> getFiles (newFile : files)
109 getProgram :: IO Program
111 printf "%s\n" (T.unpack (bold (cyan "XDG-ninja Configuration Wizard")))
112 >> printf "%s\n" (T.unpack (faint (italic (cyan "First, tell me what program you're creating a configuration for."))))
113 >> getProp (yellow "Program name: ") ""
115 printf "%s\n" (T.unpack (faint (italic (cyan "Alright, now let's configure which files belong to this program.")))) >>
116 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 ~.")))) >>
117 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.")))) >>
118 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.")))) >>
119 getFiles [] >>= \files ->
120 return Program {name = T.pack name, files = files}