]> glassweightruler.freedombox.rocks Git - xdg-ninja.git/blob - lib/Output.hs
Add mongosh config (#130)
[xdg-ninja.git] / lib / Output.hs
1 module Output where
2
3 import Data.Char (isSpace)
4 import qualified Data.Text as T
5 import Data.Text.ANSI
6 import Data.UUID
7 import Data.UUID.V4
8 import Program
9 import System.Exit
10 import System.Process
11
12 getFilename :: IO String
13 getFilename = do
14 id <- toString <$> Data.UUID.V4.nextRandom
15 return ("/tmp/xdg-ninja." ++ id ++ ".txt")
16
17 renderMarkdown :: String -> IO String
18 renderMarkdown content = do
19 filenameInput <- getFilename
20 filenameOutput <- getFilename
21 editor <- appendFile (filenameInput) content
22 (_, _, _, p) <- createProcess (shell ("glow -s dark " ++ filenameInput ++ " > " ++ filenameOutput))
23 f <- waitForProcess p
24 case f of
25 ExitSuccess -> readFile filenameOutput
26 ExitFailure a -> return ""
27
28 line :: (T.Text -> T.Text) -> String -> String -> String
29 line color name filename = T.unpack ((T.pack "[") <> bold (color (T.pack name)) <> (T.pack "]: ") <> bold (italic (T.pack filename)))
30
31 data Mode = ERR | WARN | INFO | SUCS | HELP
32
33 log :: Mode -> String -> String -> String -> IO ()
34 log mode name filename help = case mode of
35 ERR -> do
36 putStrLn (line red name filename)
37 Output.log HELP name filename help
38 WARN -> do
39 putStrLn (line yellow name filename)
40 Output.log HELP name filename help
41 INFO -> do
42 putStrLn (line cyan name filename)
43 Output.log HELP name filename help
44 SUCS -> putStrLn (line green name filename)
45 HELP -> do
46 md <- case (all isSpace help) of
47 True -> renderMarkdown "_No help available._"
48 False -> renderMarkdown help
49 putStr md
50
51 logFile :: T.Text -> File -> Bool -> IO ()
52 logFile programName file onFilesystem = case onFilesystem of
53 False -> Output.log SUCS (T.unpack programName) (path file) (help file)
54 True -> case (supportLevel file) of
55 Unsupported -> Output.log ERR (T.unpack programName) (path file) (help file)
56 _ -> Output.log WARN (T.unpack programName) (path file) (help file)