]> glassweightruler.freedombox.rocks Git - xdg-ninja.git/blob - haskell/lib/AddProgram.hs
Add GHCi (#460)
[xdg-ninja.git] / haskell / lib / AddProgram.hs
1 {-# LANGUAGE DeriveGeneric #-}
2 {-# LANGUAGE OverloadedStrings #-}
3
4 -- I do not know haskell, this code is probably shit
5
6 module AddProgram where
7
8 import Data.Aeson
9 import Data.Aeson.Encode.Pretty
10 import Data.Aeson.Types
11 import qualified Data.ByteString.Lazy as B
12 import Data.List.Extra
13 import Data.Maybe
14 import qualified Data.Text as T
15 import Data.Text.ANSI
16 import Data.UUID
17 import Data.UUID.V4
18 import GHC.Float (double2Float)
19 import GHC.Generics
20 import Program
21 import Prompts
22 import System.Console.Haskeline
23 import System.Environment (getEnv)
24 import System.Exit
25 import System.IO
26 import System.Process
27 import Text.Printf (printf)
28
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"
34
35 getHelp :: SupportLevel -> IO String
36 getHelp supportLevel = getInputMarkdown (getTemplate supportLevel)
37
38 getSupportLevel :: IO SupportLevel
39 getSupportLevel = do
40 movable <- promptBool (blue "Can the file be moved? (y/n) ") (red "Please provide a valid answer.") "y"
41 if movable
42 then do
43 envVars <- promptBool (blue "Do you have to export environment variables? (y/n) ") (red "Please provide a valid answer.") "y"
44 if envVars
45 then return EnvVars
46 else do
47 alias <- promptBool (blue "Do you have to set an alias? (y/n) ") (red "Please provide a valid answer.") "y"
48 if alias
49 then return Alias
50 else return Supported
51 else return Unsupported
52
53 getFile :: IO File
54 getFile = do
55 path <- getProp (blue "Path to file: ") "$HOME/."
56 supportLevel <- getSupportLevel
57 help <- getHelp supportLevel
58 return File {path = path, supportLevel = supportLevel, help = help}
59
60 getFiles :: [File] -> IO [File]
61 getFiles files =
62 if Data.List.Extra.null files
63 then do
64 newFile <- getFile
65 getFiles (newFile : files)
66 else do
67 new <- promptBool (green "Add another file? (y/n) ") (red "Please provide a valid answer.") ""
68 if new
69 then do
70 newFile <- getFile
71 getFiles (newFile : files)
72 else return files
73
74 getProgram :: IO Program
75 getProgram = do
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."))))
83 >> getFiles []
84 return Program {name = T.pack name, files = files}
85
86 saveProgram :: IO ()
87 saveProgram = do
88 program <- getProgram
89 do_save <- promptBool (green "Save? (y/n) ") (red "Please provide a valid answer.") ""
90 if do_save
91 then save (makeFilename (name program)) program
92 else return ()