]> glassweightruler.freedombox.rocks Git - xdg-ninja.git/commitdiff
Added output haskell module
authorb3nj4m1n <b3nj4m1n@gmx.net>
Sun, 22 May 2022 12:13:55 +0000 (14:13 +0200)
committerb3nj4m1n <b3nj4m1n@gmx.net>
Sun, 22 May 2022 12:13:55 +0000 (14:13 +0200)
lib/Output.hs [new file with mode: 0644]
xdgnj.cabal

diff --git a/lib/Output.hs b/lib/Output.hs
new file mode 100644 (file)
index 0000000..c9f963d
--- /dev/null
@@ -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)
index a5c7cd269a053b31f16e2b251f22202e2cb9c538..dc11a1a53911135830a6f4e330c89c88c6d77866 100644 (file)
@@ -35,7 +35,7 @@ library
         aeson-pretty ^>=0.8.9,
     hs-source-dirs:   lib
     default-language: Haskell2010
         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
 
 executable add-program
     main-is:          add-program.hs