Parcourir la source

code improvements

milan jurkulák il y a 2 ans
Parent
commit
bb14433f2f

+ 8 - 1
src/main/kotlin/eu/mjdev/desktop/Main.kt

@@ -11,10 +11,17 @@ fun main(
     args = args.toList(),
     exitProcessOnExit = true
 ) {
+    // net devices  : ls /sys/class/net
+    // wifi list    : nmcli -t -f ALL dev wifi
+    // wave mon     : apt install wavemon
+    // connect gui  : nmtui
+    // eth settings : iwconfig
     MainWindow()
     DisposableEffect(Unit) {
         println("App started with args: $args")
-        Shell.autoStartApps()
+        Shell {
+            autoStartApps()
+        }
         onDispose {
             println("App ended.")
         }

+ 13 - 11
src/main/kotlin/eu/mjdev/desktop/data/App.kt

@@ -65,17 +65,19 @@ class App(
         println("Starting app: $name [$windowClass].")
         println("dex -w ${desktopFile?.absolutePath}")
         triggerStart()
-        Shell.startApp(
-            app = this@App,
-            onStarted = {
-                println("App started app: $name [$windowClass].")
-                triggerStarted()
-            },
-            onStopped = { e ->
-                println("App stopped: $name [$windowClass].")
-                triggerStop(e)
-            }
-        )
+        Shell {
+            startApp(
+                app = this@App,
+                onStarted = {
+                    println("App started app: $name [$windowClass].")
+                    triggerStarted()
+                },
+                onStopped = { e ->
+                    println("App stopped: $name [$windowClass].")
+                    triggerStop(e)
+                }
+            )
+        }
     }.onFailure { e ->
         triggerStop(e)
     }

+ 15 - 3
src/main/kotlin/eu/mjdev/desktop/helpers/system/Environment.kt

@@ -1,11 +1,23 @@
 package eu.mjdev.desktop.helpers.system
 
+import eu.mjdev.desktop.managers.GnomeManager.Companion.THEME_MJDEV
+
 class Environment(
     val data: List<String> = Shell.executeAndReadLines("env")
 ) : HashMap<String, String>() {
     init {
         putAll(System.getenv())
-        put("DBUS_SESSION_BUS_ADDRESS", "unix:path=\$XDG_RUNTIME_DIR/bus")
-        put("XDG_CURRENT_DESKTOP", "ubuntu:GNOME")
+        put(DBUS_SESSION_BUS_ADDRESS, DBUS_SESSION_BUS_ADDRESS_DEFAULT)
+        put(XDG_CURRENT_DESKTOP, DESKTOP_UBUNTU_GNOME)
+        put(GTK_THEME, THEME_MJDEV)
+    }
+
+    companion object {
+        const val DBUS_SESSION_BUS_ADDRESS = "DBUS_SESSION_BUS_ADDRESS"
+        const val XDG_CURRENT_DESKTOP = "XDG_CURRENT_DESKTOP"
+        const val GTK_THEME = "GTK_THEME"
+
+        const val DBUS_SESSION_BUS_ADDRESS_DEFAULT = "unix:path=\$XDG_RUNTIME_DIR/bus"
+        const val DESKTOP_UBUNTU_GNOME = "ubuntu:GNOME"
     }
-}
+}

+ 48 - 34
src/main/kotlin/eu/mjdev/desktop/helpers/system/Shell.kt

@@ -1,40 +1,51 @@
 package eu.mjdev.desktop.helpers.system
 
 import eu.mjdev.desktop.data.App
+import eu.mjdev.desktop.extensions.Custom.consoleOutput
 import eu.mjdev.desktop.helpers.exception.ErrorException.Companion.error
 import eu.mjdev.desktop.helpers.exception.SuccessException.Companion.success
-import kotlinx.coroutines.CoroutineScope
-import kotlinx.coroutines.Dispatchers
-import kotlinx.coroutines.launch
-import java.io.BufferedReader
-import java.util.*
 
-private val Process.consoleOutput: String
-    get() {
-        val sj = StringJoiner(System.lineSeparator())
-        val bfr = BufferedReader(inputReader())
-        bfr.lines().iterator().forEachRemaining { s: String? -> sj.add(s) }
-        return sj.toString()
+@Suppress("MemberVisibilityCanBePrivate", "unused")
+class Shell(
+    block: ShellScope.() -> Unit
+) {
+    val environment by lazy { Environment() }
+
+    init {
+        ShellScope(this).apply(block)
     }
 
-@Suppress("MemberVisibilityCanBePrivate", "unused")
-object Shell {
+    class ShellScope(
+        val shell: Shell,
+        val environment: Environment = shell.environment
+    ) {
+        fun autoStartApps() = shell.autoStartApps()
 
-    val environment: Environment
-        get() = Environment()
+        fun startApp(
+            app: App,
+            onStarted: () -> Unit = {},
+            onStopped: (e: Throwable) -> Unit = {},
+        ) = shell.startApp(app, onStarted, onStopped)
 
-    val Process.handle: ProcessHandle
-        get() = toHandle()
+        fun executeAndRead(
+            cmd: String,
+            vararg args: String
+        ): String = executeAndRead(cmd, *args)
 
-    fun autoStartApps() = CoroutineScope(Dispatchers.IO).launch {
-        ProcessBuilder("dex", "-a").apply {
-            environment().putAll(environment)
-        }.also { processBuilder ->
-            runCatching {
-                processBuilder.start()
-            }.onFailure { e ->
-                e.printStackTrace()
-            }
+        fun executeAndReadLines(
+            cmd: String,
+            vararg args: String
+        ): List<String> = executeAndReadLines(cmd, *args)
+    }
+
+    fun autoStartApps() = ProcessBuilder("dex", "-a").apply {
+        environment().putAll(environment)
+    }.also { processBuilder ->
+        runCatching {
+            processBuilder.start()
+
+        }.onFailure { e ->
+            e.printStackTrace()
         }
     }
 
@@ -66,11 +77,11 @@ object Shell {
         }
     }
 
-    fun executeAndRead(
-        cmd: String,
-        vararg args: String
-    ): String =
-        Runtime.getRuntime().exec(
+    companion object {
+        fun executeAndRead(
+            cmd: String,
+            vararg args: String
+        ): String = Runtime.getRuntime().exec(
             mutableListOf<String>().apply {
                 add(cmd)
                 addAll(args)
@@ -79,8 +90,10 @@ object Shell {
             waitFor()
         }.inputReader().readText()
 
-    fun executeAndReadLines(cmd: String, vararg args: String): List<String> =
-        Runtime.getRuntime().exec(
+        fun executeAndReadLines(
+            cmd: String,
+            vararg args: String
+        ): List<String> = Runtime.getRuntime().exec(
             mutableListOf<String>().apply {
                 add(cmd)
                 addAll(args)
@@ -88,4 +101,5 @@ object Shell {
         ).apply {
             waitFor()
         }.inputReader().readText().lines()
-}
+    }
+}