From: b3nj4m1n Date: Sun, 22 May 2022 12:13:55 +0000 (+0200) Subject: Added output haskell module X-Git-Tag: v0.2.0.0~29 X-Git-Url: https://glassweightruler.freedombox.rocks/gitweb/xdg-ninja.git/commitdiff_plain/64aa55a202aff2021f49fc7701b9f76a9c26907f?ds=sidebyside Added output haskell module --- diff --git a/lib/Output.hs b/lib/Output.hs new file mode 100644 index 0000000..c9f963d --- /dev/null +++ b/lib/Output.hs @@ -0,0 +1,53 @@ +module Output where + +import qualified Data.Text as T +import Data.Text.ANSI +import Data.UUID +import Data.UUID.V4 +import System.Exit +import System.Process +import Program + +getFilename :: IO String +getFilename = do + id <- toString <$> Data.UUID.V4.nextRandom + return ("/tmp/xdg-ninja." ++ id ++ ".txt") + +renderMarkdown :: String -> IO String +renderMarkdown content = do + filenameInput <- getFilename + filenameOutput <- getFilename + editor <- appendFile (filenameInput) content + (_, _, _, p) <- createProcess (shell ("glow -s dark " ++ filenameInput ++ " > " ++ filenameOutput)) + f <- waitForProcess p + case f of + ExitSuccess -> readFile filenameOutput + ExitFailure a -> return "" + +line :: (T.Text -> T.Text) -> String -> String -> String +line color name filename = T.unpack ((T.pack "[") <> bold (color (T.pack name)) <> (T.pack "]: ") <> bold (italic (T.pack filename))) + +data Mode = ERR | WARN | INFO | SUCS | HELP + +log :: Mode -> String -> String -> String -> IO () +log mode name filename help = case mode of + ERR -> do + putStrLn (line red name filename) + Output.log HELP name filename help + WARN -> do + putStrLn (line yellow name filename) + Output.log HELP name filename help + INFO -> do + putStrLn (line cyan name filename) + Output.log HELP name filename help + SUCS -> putStrLn (line green name filename) + HELP -> do + md <- renderMarkdown help + putStr md + +logFile :: T.Text -> File -> Bool -> IO () +logFile programName file onFilesystem = case onFilesystem of + False -> Output.log SUCS (T.unpack programName) (path file) (help file) + True -> case (supportLevel file) of + Unsupported -> Output.log WARN (T.unpack programName) (path file) (help file) + _ -> Output.log ERR (T.unpack programName) (path file) (help file) diff --git a/xdgnj.cabal b/xdgnj.cabal index a5c7cd2..dc11a1a 100644 --- a/xdgnj.cabal +++ b/xdgnj.cabal @@ -35,7 +35,7 @@ library aeson-pretty ^>=0.8.9, hs-source-dirs: lib default-language: Haskell2010 - exposed-modules: AddProgram, Program, Prompts, EditProgram + exposed-modules: AddProgram, Program, Prompts, EditProgram, Output executable add-program main-is: add-program.hs