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 Data.Aeson.Types
9 import qualified Data.ByteString.Lazy as B
10 import Data.List.Extra
12 import qualified Data.Text as T
16 import GHC.Float (double2Float)
18 import System.Console.Haskeline
19 import System.Environment (getEnv)
23 import Text.Printf (printf)
27 supportLevel :: SupportLevel,
30 deriving (Generic, Show)
32 instance ToJSON File where
33 toEncoding (File path supportLevel help) = pairs ("path" .= path <> "movable" .= supportLevel <> "help" .= help)
35 data Program = Program
39 deriving (Generic, Show)
41 instance ToJSON Program where
42 toEncoding = genericToEncoding defaultOptions
44 save :: Program -> IO ()
46 let path = ("./programs/" ++ (T.unpack (name program)) ++ ".json")
47 B.writeFile path (encodePretty program)
49 data SupportLevel = Unsupported | Alias | EnvVars | Supported
50 deriving (Generic, Show)
52 instance ToJSON SupportLevel where
53 toEncoding Unsupported = toEncoding ( Bool False )
54 toEncoding _ = toEncoding ( Bool True )
56 getTemplate :: SupportLevel -> String
57 getTemplate Unsupported = "Currently unsupported.\n\n_Relevant issue:_ https://github.com/user/repo/issues/nr\n"
58 getTemplate EnvVars = "Export the following environment variables:\n\n```bash\n\n```"
59 getTemplate Alias = "Alias PROGRAM to use a custom configuration location:\n\n```bash\nalias PROGRAM=PROGRAM --config \"$XDG_CONFIG_HOME\"/PROGRAM/config\n```\n"
60 getTemplate Supported = "Supported since _VERSION_.\n\nYou can move the file to _XDG_CONFIG_HOME/PROGRAM/CONFIG.\n"
63 getHelp :: SupportLevel -> IO String
64 getHelp supportLevel = do
65 id <- toString <$> Data.UUID.V4.nextRandom
66 editor <- appendFile ("/tmp/xdg-ninja." ++ id ++ ".md") (getTemplate supportLevel) >> (getEnv "EDITOR")
67 (_, _, _, p) <- createProcess (shell (editor ++ " /tmp/xdg-ninja." ++ id ++ ".md"))
70 ExitSuccess -> readFile ("/tmp/xdg-ninja." ++ id ++ ".md")
71 ExitFailure a -> return ""
73 getProp :: T.Text -> T.Text -> IO String
74 getProp prompt placeholder = do
75 let string_prompt = T.unpack prompt
76 let string_placholder = T.unpack placeholder
77 x <- runInputT defaultSettings (getInputLineWithInitial string_prompt (string_placholder, ""))
82 data Answer = Yes | No | Unknown
84 stringToBool :: String -> Answer
85 stringToBool s = case lower s of
94 promptBool :: T.Text -> T.Text -> T.Text -> IO Bool
95 promptBool prompt prompt_unrecognised placeholder = do
96 x <- getProp prompt placeholder
97 case stringToBool x of
100 Unknown -> printf "%s\n" prompt_unrecognised >> promptBool prompt prompt_unrecognised placeholder
102 getSupportLevel :: IO SupportLevel
104 movable <- promptBool (blue "Can the file be moved? (y/n) ") (red "Please provide a valid answer.") "y"
107 envVars <- promptBool (blue "Do you have to export environment variables? (y/n) ") (red "Please provide a valid answer.") "y"
111 alias <- promptBool (blue "Do you have to set an alias? (y/n) ") (red "Please provide a valid answer.") "y"
114 else return Supported
115 else return Unsupported
119 path <- getProp (blue "Path to file: ") "$HOME/."
120 supportLevel <- getSupportLevel
121 help <- getHelp supportLevel
122 return File {path = path, supportLevel = supportLevel, help = help}
124 getFiles :: [File] -> IO [File]
126 if Data.List.Extra.null files
129 getFiles (newFile : files)
131 new <- promptBool (green "Add another file? (y/n) ") (red "Please provide a valid answer.") ""
135 getFiles (newFile : files)
138 getProgram :: IO Program
140 name <- printf "%s\n" (T.unpack (bold (cyan "XDG-ninja Configuration Wizard")))
141 >> printf "%s\n" (T.unpack (faint (italic (cyan "First, tell me what program you're creating a configuration for."))))
142 >> getProp (yellow "Program name: ") ""
143 files <- printf "%s\n" (T.unpack (faint (italic (cyan "Alright, now let's configure which files belong to this program."))))
144 >> 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 ~."))))
145 >> 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."))))
146 >> 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."))))
148 return Program {name = T.pack name, files = files}
152 program <- getProgram
153 do_save <- promptBool (green "Save? (y/n) ") (red "Please provide a valid answer.") ""