1 {-# LANGUAGE DeriveGeneric #-}
2 {-# LANGUAGE OverloadedStrings #-}
4 -- I do not know haskell, this code is probably shit
6 module AddProgram where
9 import Data.Aeson.Encode.Pretty
10 import Data.Aeson.Types
11 import qualified Data.ByteString.Lazy as B
12 import Data.List.Extra
14 import qualified Data.Text as T
18 import GHC.Float (double2Float)
22 import System.Console.Haskeline
23 import System.Environment (getEnv)
27 import Text.Printf (printf)
29 getTemplate :: SupportLevel -> String
30 getTemplate Unsupported = "Currently unsupported.\n\n_Relevant issue:_ https://github.com/user/repo/issues/nr\n"
31 getTemplate EnvVars = "Export the following environment variables:\n\n```bash\n\n```"
32 getTemplate Alias = "Alias PROGRAM to use a custom configuration location:\n\n```bash\nalias PROGRAM=PROGRAM --config \"$XDG_CONFIG_HOME\"/PROGRAM/config\n```\n"
33 getTemplate Supported = "Supported since _VERSION_.\n\nYou can move the file to _XDG_CONFIG_HOME/PROGRAM/CONFIG.\n"
35 getHelp :: SupportLevel -> IO String
36 getHelp supportLevel = getInputMarkdown (getTemplate supportLevel)
38 getSupportLevel :: IO SupportLevel
40 movable <- promptBool (blue "Can the file be moved? (y/n) ") (red "Please provide a valid answer.") "y"
43 envVars <- promptBool (blue "Do you have to export environment variables? (y/n) ") (red "Please provide a valid answer.") "y"
47 alias <- promptBool (blue "Do you have to set an alias? (y/n) ") (red "Please provide a valid answer.") "y"
51 else return Unsupported
55 path <- getProp (blue "Path to file: ") "$HOME/."
56 supportLevel <- getSupportLevel
57 help <- getHelp supportLevel
58 return File {path = path, supportLevel = supportLevel, help = help}
60 getFiles :: [File] -> IO [File]
62 if Data.List.Extra.null files
65 getFiles (newFile : files)
67 new <- promptBool (green "Add another file? (y/n) ") (red "Please provide a valid answer.") ""
71 getFiles (newFile : files)
74 getProgram :: IO Program
76 name <- printf "%s\n" (T.unpack (bold (cyan "XDG-ninja Configuration Wizard")))
77 >> printf "%s\n" (T.unpack (faint (italic (cyan "First, tell me what program you're creating a configuration for."))))
78 >> getProp (yellow "Program name: ") ""
79 files <- printf "%s\n" (T.unpack (faint (italic (cyan "Alright, now let's configure which files belong to this program."))))
80 >> 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 ~."))))
81 >> printf "%s\n" (T.unpack (faint (italic (cyan "I'll then ask you whether or not this file can be moved to a different directory."))))
82 >> 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."))))
84 return Program {name = T.pack name, files = files}
89 do_save <- promptBool (green "Save? (y/n) ") (red "Please provide a valid answer.") ""
91 then save (makeFilename (name program)) program