]> glassweightruler.freedombox.rocks Git - xdg-ninja.git/commitdiff
Added edit program command
authorb3nj4m1n <b3nj4m1n@gmx.net>
Sat, 21 May 2022 00:50:27 +0000 (02:50 +0200)
committerb3nj4m1n <b3nj4m1n@gmx.net>
Sat, 21 May 2022 00:50:27 +0000 (02:50 +0200)
lib/AddProgram.hs
lib/EditProgram.hs [new file with mode: 0644]
lib/Program.hs
programs/cargo.json
programs/steam.json
src/xdgnj.hs
xdgnj.cabal

index 5a145ad7027fd098fdff14df46a98ad7d06a1cd2..13f7304c30441d4bdc0e1c1159a009a0d650e0dc 100644 (file)
@@ -88,5 +88,5 @@ saveProgram = do
   program <- getProgram
   do_save <- promptBool (green "Save? (y/n) ") (red "Please provide a valid answer.") ""
   if do_save
-  then save program
+  then save (makeFilename (name program)) program
   else return ()
diff --git a/lib/EditProgram.hs b/lib/EditProgram.hs
new file mode 100644 (file)
index 0000000..a1a6728
--- /dev/null
@@ -0,0 +1,81 @@
+{-# LANGUAGE DeriveGeneric     #-}
+{-# LANGUAGE OverloadedStrings #-}
+
+-- I do not know haskell, this code is probably shit
+
+module EditProgram where
+
+import           Data.Aeson
+import           Data.Aeson.Encode.Pretty
+import           Data.Aeson.Parser
+import           Data.Aeson.Types
+import qualified Data.ByteString.Lazy     as B
+import           Data.List.Extra
+import           Data.Maybe
+import qualified Data.Text                as T
+import           Data.Text.ANSI
+import           Data.UUID
+import           Data.UUID.V4
+import           GHC.Float                (double2Float)
+import           GHC.Generics
+import           Program
+import           Prompts
+import           System.Console.Haskeline
+import           System.Environment       (getEnv)
+import           System.Exit
+import           System.IO
+import           System.Process
+import           Text.Printf              (printf)
+
+getSupportLevel :: SupportLevel -> IO SupportLevel
+getSupportLevel supportLevel = do
+  changed <- promptBool (blue "Has the support level changed? (y/n) ") (red "Please provide a valid answer.") "y"
+  if not changed
+  then return supportLevel
+  else do
+      movable <- promptBool (blue "Can the file be moved? (y/n) ") (red "Please provide a valid answer.") "y"
+      if movable
+      then do
+        envVars <- promptBool (blue "Do you have to export environment variables? (y/n) ") (red "Please provide a valid answer.") "y"
+        if envVars
+        then return EnvVars
+        else do
+          alias <- promptBool (blue "Do you have to set an alias? (y/n) ") (red "Please provide a valid answer.") "y"
+          if alias
+          then return Alias
+          else return Supported
+      else return Unsupported
+
+
+getHelp :: String -> IO String
+getHelp currentHelp = getInputMarkdown currentHelp
+
+editFile :: File -> IO File
+editFile f = do
+  printf "%s\n" (T.unpack (italic (cyan (T.pack (path f)))))
+  edit <- promptBool (green "Edit this file? (y/n) ") (red "Please provide a valid answer.") ""
+  if not edit
+  then return f
+  else do
+    supportLevel <- getSupportLevel (supportLevel f)
+    help <- getHelp (help f)
+    return File {path = path f, supportLevel = supportLevel, help = help}
+
+readProgram :: String -> IO ( Maybe Program )
+readProgram filename = do
+    json_data <- B.readFile filename
+    return (decode json_data)
+
+editProgram :: String -> IO ()
+editProgram filename = do
+  program <- readProgram filename
+  case program of
+    Nothing -> printf "%s %s\n" (T.unpack (bold (red "Error parsing file"))) (T.unpack (italic (red (T.pack filename))))
+    Just p -> do
+      printf "%s %s\n" (T.unpack (cyan ("Editing"))) (T.unpack (italic (cyan (name p))))
+      files <- return (files p)
+      newFiles <- sequence (map editFile files)
+      do_save <- promptBool (green "Save? (y/n) ") (red "Please provide a valid answer.") ""
+      if do_save
+      then save (T.pack filename) Program {name = (name p), files = newFiles}
+      else return ()
index 06adaa2d39024c31965c96ea1aa20e66121206ce..1f1ad958cf4fc5b09693368f80e2455c035bc617 100644 (file)
@@ -9,15 +9,11 @@ import qualified Data.ByteString.Lazy     as B
 import qualified Data.Text                as T
 import           GHC.Generics
 
-data File = File
-  { path         :: String,
-    supportLevel :: SupportLevel,
-    help         :: String
-  }
-  deriving (Generic, Show)
-
-instance ToJSON File where
-  toEncoding (File path supportLevel help) = pairs ("path" .= path <> "movable" .= supportLevel <> "help" .= help)
+instance FromJSON File where
+  parseJSON (Object v) = File
+    <$> v .: "path"
+    <*> v .: "movable"
+    <*> v .: "help"
 
 data Program = Program
   { name  :: T.Text,
@@ -26,17 +22,36 @@ data Program = Program
   deriving (Generic, Show)
 
 instance ToJSON Program where
-  toEncoding = genericToEncoding defaultOptions
+  toJSON (Program name files) = object [ "name" .= name, "files" .= files ]
+  toEncoding (Program name files) = pairs ("name" .= name <> "files" .= files)
+instance FromJSON Program
 
-save :: Program -> IO ()
-save program = do
-  let path = ("./programs/" ++ (T.unpack (name program)) ++ ".json")
-  B.writeFile path (encodePretty program)
+data File = File
+  { path         :: String,
+    supportLevel :: SupportLevel,
+    help         :: String
+  }
+  deriving (Generic, Show)
+
+instance ToJSON File where
+  toJSON (File path supportLevel help) = object [ "path" .= path, "movable" .= supportLevel, "help" .= help ]
+  toEncoding (File path supportLevel help) = pairs ("path" .= path <> "movable" .= supportLevel <> "help" .= help)
 
 data SupportLevel = Unsupported | Alias | EnvVars | Supported
   deriving (Generic, Show)
 
 instance ToJSON SupportLevel where
+  toJSON Unsupported = toJSON (Bool False)
+  toJSON _           = toJSON (Bool True)
   toEncoding Unsupported = toEncoding ( Bool False )
   toEncoding _           = toEncoding ( Bool True )
+instance FromJSON SupportLevel where
+  parseJSON (Bool False) = return Unsupported
+  parseJSON (Bool True)  = return EnvVars
+
+makeFilename :: T.Text -> T.Text
+makeFilename s = T.pack ( "./programs/" ++ T.unpack s ++ ".json" )
 
+save :: T.Text -> Program -> IO ()
+save filename program = do
+  B.writeFile (T.unpack filename) (encodePretty program)
index 13f47389124fc11245d7fa01e0b3dbffd186d21f..51ddeec68046c33d2573708d90a13aa2b536d5bd 100644 (file)
@@ -2,7 +2,7 @@
     "files": [
         {
             "path": "$HOME/.cargo",
-            "movable": true,
+            "movable": false,
             "help": "Export the following environment variables:\n\n```bash\nexport CARGO_HOME=\"$XDG_DATA_HOME\"/cargo\n```\n"
         }
     ],
index c75765dd177b04b19b3f32726b3c418bf6accd76..e948f5f0eebec6947267f1b4cb44e7756c517a3a 100644 (file)
@@ -2,18 +2,18 @@
     "files": [
         {
             "path": "$HOME/.steampid",
-            "movable": false,
+            "supportLevel": "Unsupported",
             "help": ""
         },
         {
             "path": "$HOME/.steampath",
-            "movable": false,
+            "supportLevel": "Supported",
             "help": ""
         },
         {
             "path": "$HOME/.steam",
-            "movable": false,
-            "help": "See the relevant [issue](https://github.com/ValveSoftware/steam-for-linux/issues/1890). It's a goldmine.\n\n> Day 3368\n> A drop or two and follows a river of tears\n> How the cries of the anguished fall on deaf ears\n> A $HOME I believed I'm the one to own\n> Excalibur lifted and this issue remains in stone\n\n"
+            "supportLevel": "Unsupported",
+            "help": "\n"
         }
     ],
     "name": "steam"
index d2566c3f8170ab528a6117d966633457386a461c..3c4cea018d27948337645544f8bb0a9b3a6a042a 100644 (file)
@@ -1,7 +1,9 @@
 module Main where
 
-import           AddProgram
+
+import qualified AddProgram          as PA
 import           Data.Semigroup      ((<>))
+import qualified EditProgram         as PE
 import           Options.Applicative
 
 data Args = AddProgram
@@ -37,5 +39,6 @@ main :: IO ()
 main = do
     args <- execParser args
     case args of
-        AddProgram -> saveProgram
-        _          -> print args
+        AddProgram           -> PA.saveProgram
+        EditProgram filename -> PE.editProgram filename
+        _                    -> print args
index 6005dd2bf4f08b3583fa6df30a2f81c39b5d1d94..a5c7cd269a053b31f16e2b251f22202e2cb9c538 100644 (file)
@@ -35,7 +35,7 @@ library
         aeson-pretty ^>=0.8.9,
     hs-source-dirs:   lib
     default-language: Haskell2010
-    exposed-modules: AddProgram, Program, Prompts
+    exposed-modules: AddProgram, Program, Prompts, EditProgram
 
 executable add-program
     main-is:          add-program.hs