diff --git a/Package.swift b/Package.swift index b24140d..93047ba 100644 --- a/Package.swift +++ b/Package.swift @@ -34,10 +34,7 @@ let package = Package( exclude: [ "Models/PHEvent/README.md" ], - resources: [ - .process("Models/PHConfig/config.toml"), - .process("Models/PHTheme/theme.toml"), - ] + resources: [.copy("config")] ), .testTarget( name: "phbarTests", diff --git a/Sources/phbar/Commands/PHBar+Install.swift b/Sources/phbar/Commands/PHBar+Install.swift new file mode 100644 index 0000000..e5a82dc --- /dev/null +++ b/Sources/phbar/Commands/PHBar+Install.swift @@ -0,0 +1,69 @@ +import ArgumentParser +import Foundation + +extension PHBar { + struct Install: ParsableCommand { + static let configuration = CommandConfiguration( + commandName: "install", + abstract: "Start the status bar.", + discussion: """ + Launch the bar as a non-stopping background process. + This command is meant to be used for both testing and production. + + When testing, you can provide the `--debug` flag to get visual + guidance that helps you configure the blocks and a verbose output from + the shell scripts to debug the behaviour. + """ + ) + + private var source: URL? { + Bundle.module.url(forResource: "config", withExtension: "") + } + + private var destination: URL { + let environment = ProcessInfo.processInfo.environment + + let homeDirectory = FileManager.default.homeDirectoryForCurrentUser + let standardConfigDirectory = homeDirectory.appending(path: ".config") + let xdgConfigDirectory = environment["XDG_CONFIG_HOME"]?.trimmingCharacters( + in: .whitespaces) + var candidateDirectory: URL + + if let xdgConfigDirectory, xdgConfigDirectory.hasPrefix("/") { + candidateDirectory = URL(fileURLWithPath: xdgConfigDirectory) + } else if FileManager.default.fileExists(atPath: standardConfigDirectory.relativePath) { + candidateDirectory = standardConfigDirectory + } else { + candidateDirectory = homeDirectory + } + + return candidateDirectory.appending(path: "phbar") + } + + mutating func run() throws { + do { + guard let source else { throw PHBar.Error("Failed to locate bundled config") } + try FileManager.default.copyItem(at: source, to: destination) + try makeExecutable() + } catch { + throw PHBar.Error("Failed to create config directory", underlyingError: error) + } + + throw CleanExit.message("Config directory created at \(destination.relativePath)") + } + + private func makeExecutable() throws { + let scriptsDirectory = destination.appending(path: "scripts", directoryHint: .isDirectory) + let scripts = try FileManager.default.contentsOfDirectory( + atPath: scriptsDirectory.relativePath + ) + + for script in scripts { + let path = scriptsDirectory.appending(path: script) + var attributes: [FileAttributeKey: Any] = [:] + attributes[.posixPermissions] = 0o755 // rwxr-xr-x + try FileManager.default.setAttributes(attributes, ofItemAtPath: path.relativePath) + } + } + } +} diff --git a/Sources/phbar/PHBar.swift b/Sources/phbar/PHBar.swift index cadcebd..f5dbf70 100644 --- a/Sources/phbar/PHBar.swift +++ b/Sources/phbar/PHBar.swift @@ -39,7 +39,7 @@ struct PHBar: ParsableCommand { the same command to start the bar automatically at login. """, version: "1.0.0", - subcommands: [Start.self, Refresh.self] + subcommands: [Start.self, Install.self] ) } diff --git a/Sources/phbar/config/blocks/default.toml b/Sources/phbar/config/blocks/default.toml new file mode 100644 index 0000000..36531ac --- /dev/null +++ b/Sources/phbar/config/blocks/default.toml @@ -0,0 +1,31 @@ +[[block]] +name = "version" +style = "elevated" + +[[block]] +name = "_space 14" + +[[block]] +name = "attribution" +command = 'echo "by Panini House"' +text = "italic" + +[[block]] +name = "_space" + +[[block]] +name = "gesture" + +[[block]] +name = "_space 14" + +[[block]] +name = "clock" +refresh = 1 +style = "elevated" + +[[block]] +name = "greetings" +command = 'echo "􀟱 Hi $USER", welcome to phbar!' +text = "italic" +centered = true diff --git a/Sources/phbar/Models/PHConfig/config.toml b/Sources/phbar/config/config.toml similarity index 89% rename from Sources/phbar/Models/PHConfig/config.toml rename to Sources/phbar/config/config.toml index 0d5e92c..f90bcfc 100644 --- a/Sources/phbar/Models/PHConfig/config.toml +++ b/Sources/phbar/config/config.toml @@ -6,19 +6,17 @@ theme = "default" window = "default" blocks = "default" -# Optional: override the theme, window, and/or block set per monitor. +# Optional: override the window and/or block set per monitor. # - quoted numeric key → NSScreen index # - string key → NSScreen.localizedName (stable across replugs) # Precedence: monitor name → monitor index → the globals above. # Either field may be omitted to inherit its global value. # # [monitor."1"] -# theme = "default" # window = "bottom" # blocks = "laptop" # # [monitor."DELL U2723QE"] -# theme = "external" # window = "clock" # blocks = "external" @@ -32,6 +30,5 @@ blocks = "default" # theme (e.g. `ACCENT = "#ff0000"`, then `color = "$ACCENT"` in the theme) # # [env] -# TEST = "ciao" -# TEST_2 = "hello world" # PATH = "$PATH:/opt/homebrew/bin" +# MY_CUSTOM_VAR = "hello world" diff --git a/Sources/phbar/config/scripts/clock b/Sources/phbar/config/scripts/clock new file mode 100644 index 0000000..1443386 --- /dev/null +++ b/Sources/phbar/config/scripts/clock @@ -0,0 +1,3 @@ +#!/bin/sh + +echo "􀐬 $(date "+%a %d, %H:%M:%S")" diff --git a/Sources/phbar/config/scripts/gesture b/Sources/phbar/config/scripts/gesture new file mode 100644 index 0000000..9387c67 --- /dev/null +++ b/Sources/phbar/config/scripts/gesture @@ -0,0 +1,12 @@ +#!/bin/sh + +text="Click me" + +case $GESTURE in + "left_mouse_down") text="That's left mouse button!" ;; + "right_mouse_down") text="That's right mouse button!" ;; + "other_mouse_down") text="That's mouse button $GESTURE_INFO!" ;; + "scroll_wheel") text="Yes, you can even scroll!" ;; +esac + +echo "􀭆 $text" diff --git a/Sources/phbar/config/scripts/version b/Sources/phbar/config/scripts/version new file mode 100644 index 0000000..a93a4e1 --- /dev/null +++ b/Sources/phbar/config/scripts/version @@ -0,0 +1,3 @@ +#!/bin/sh + +echo "phbar v$(phbar --version)" diff --git a/Sources/phbar/Models/PHTheme/theme.toml b/Sources/phbar/config/themes/default.toml similarity index 62% rename from Sources/phbar/Models/PHTheme/theme.toml rename to Sources/phbar/config/themes/default.toml index c110940..06e22bf 100644 --- a/Sources/phbar/Models/PHTheme/theme.toml +++ b/Sources/phbar/config/themes/default.toml @@ -8,39 +8,29 @@ anchor = "top" height = 30 width = "100%" -[[window]] -name = "bottom" -anchor = "bottom" -height = 30 -width = "100%" - [[text]] name = "default" -font = "Comic Code" +font = "monospace" size = 14 weight = "regular" style = "normal" -offset = -0.5 +offset = -1 [[text]] name = "italic" -font = "Comic Code" +font = "monospace" size = 14 weight = "regular" style = "italic" -offset = -0.5 +offset = -1 [[style]] name = "default" foreground = { color = "#aed3f3" } -background = { color = "#010408", alpha = 0.825 } +background = { color = "#010408" } [[style]] -name = "floating" -foreground = { color = "#aed3f3" } - -[[style]] -name = "tinted" +name = "elevated" foreground = { color = "#aed3f3" } background = { color = "#0f304a" } padding = { left = 12.0, right = 12.0 }