diff --git a/Sources/phbar/Models/PHBlock.swift b/Sources/phbar/Models/PHBlock.swift index e147c1f..fbdd233 100644 --- a/Sources/phbar/Models/PHBlock.swift +++ b/Sources/phbar/Models/PHBlock.swift @@ -4,8 +4,8 @@ import Foundation final class PHBlock: ObservableObject, Decodable, Identifiable { let id = UUID() - let command: String - let name: String? + let name: String + let _command: String? let textName: String? @Published var text: PHThemeText = .default let styleName: String? @@ -14,6 +14,10 @@ final class PHBlock: ObservableObject, Decodable, Identifiable { let centered: Bool? var debug: Bool = false + var command: String { + _command ?? PHPaths.configDirectory.appending(path: "scripts/\(name)").relativePath + } + var visible: Bool { label != nil && !label!.isEmpty } @@ -39,7 +43,8 @@ final class PHBlock: ObservableObject, Decodable, Identifiable { var updateScheduled = false private enum CodingKeys: String, CodingKey { - case command, name + case name + case _command = "command" case textName = "text" case styleName = "style" case refresh, events, centered diff --git a/Sources/phbar/Models/PHBlock/PHBlockKind.swift b/Sources/phbar/Models/PHBlock/PHBlockKind.swift index 38e85da..58bced8 100644 --- a/Sources/phbar/Models/PHBlock/PHBlockKind.swift +++ b/Sources/phbar/Models/PHBlock/PHBlockKind.swift @@ -4,7 +4,7 @@ enum PHBlockKind: String { extension PHBlock { var kind: PHBlockKind { - if command.starts(with: "_space") { return .space } + if name.starts(with: "_space") { return .space } return .text } } diff --git a/Sources/phbar/Views/SpaceBlockView.swift b/Sources/phbar/Views/SpaceBlockView.swift index 4d41199..c961f60 100644 --- a/Sources/phbar/Views/SpaceBlockView.swift +++ b/Sources/phbar/Views/SpaceBlockView.swift @@ -4,7 +4,7 @@ struct SpaceBlockView: View { @ObservedObject var block: PHBlock var width: CGFloat? { - guard let arg = block.command.split(separator: " ").last else { return nil } + guard let arg = block.name.split(separator: " ").last else { return nil } guard let width = Double(arg) else { return nil } return CGFloat(width) } diff --git a/Tests/phbarTests/BarControllerTests.swift b/Tests/phbarTests/BarControllerTests.swift index 61b3832..0232c28 100644 --- a/Tests/phbarTests/BarControllerTests.swift +++ b/Tests/phbarTests/BarControllerTests.swift @@ -20,9 +20,11 @@ import Testing let blocks = try PHBlock.load(from: """ [[block]] command = "printf a" + name = "a" [[block]] command = "printf b" + name = "b" """) let config = try PHConfig.load() diff --git a/Tests/phbarTests/PHBlockTests.swift b/Tests/phbarTests/PHBlockTests.swift index 3c73c19..41ace43 100644 --- a/Tests/phbarTests/PHBlockTests.swift +++ b/Tests/phbarTests/PHBlockTests.swift @@ -16,6 +16,7 @@ import Testing [[block]] command = "echo world" + name = "world" """ let blocks = try PHBlock.load(from: toml) @@ -25,7 +26,7 @@ import Testing #expect(blocks[0].name == "greeting") #expect(blocks[0].refresh == 5.0) #expect(blocks[0].label == nil) - #expect(blocks[1].name == nil) + #expect(blocks[1].name == "world") #expect(blocks[1].refresh == nil) } @@ -73,11 +74,28 @@ private func makeBlocksConfigDir() throws -> URL { // MARK: - Compute +@MainActor +@Test func computeSeesBlockEnvironment() async throws { + // The `[env]` section (overlaid by `BarController`) must reach the script's + // process environment — this is the wiring the `env` config documents. + let block = try PHBlock.load(from: """ + [[block]] + name = "env" + command = "echo $PHBAR_TEST_VAR" + """)[0] + block.environment = PHEnvironment(variables: ["PHBAR_TEST_VAR": "panini"]) + + let output = await block.compute() + + #expect(output == "panini") +} + @MainActor @Test func computeReturnsCommandStdout() async throws { let blocks = try PHBlock.load(from: """ [[block]] command = "printf panini" + name = "panini" """) let output = await blocks[0].compute() @@ -95,6 +113,7 @@ private func makeBlocksConfigDir() throws -> URL { let blocks = try PHBlock.load(from: """ [[block]] command = "pwd" + name = "pwd" """) let output = await blocks[0].compute() @@ -112,6 +131,7 @@ private func makeBlocksConfigDir() throws -> URL { let block = try PHBlock.load(from: """ [[block]] command = "printf panini" + name = "panini" """)[0] #expect(block.label == nil) @@ -126,6 +146,7 @@ private func makeBlocksConfigDir() throws -> URL { let block = try PHBlock.load(from: """ [[block]] command = "printf hi" + name = "hi" """)[0] block.startAutoRefresh() @@ -149,6 +170,7 @@ private func makeBlocksConfigDir() throws -> URL { let toml = """ [[block]] command = "n=$(cat \(path) 2>/dev/null || echo 0); n=$((n+1)); echo $n > \(path); echo $n" + name = "counter" refresh = 0.05 """ diff --git a/Tests/phbarTests/PHEventTests.swift b/Tests/phbarTests/PHEventTests.swift index f755821..9dab2c7 100644 --- a/Tests/phbarTests/PHEventTests.swift +++ b/Tests/phbarTests/PHEventTests.swift @@ -32,6 +32,7 @@ private final class FakeEventSource: PHEventSource { let toml = """ [[block]] command = "echo hi" + name = "a" events = ["volume", "network", "appearance", "power", "mpd"] """ @@ -47,6 +48,7 @@ private final class FakeEventSource: PHEventSource { let blocks = try PHBlock.load(from: """ [[block]] command = "echo hi" + name = "b" events = ["totally_made_up"] """) @@ -58,6 +60,7 @@ private final class FakeEventSource: PHEventSource { let blocks = try PHBlock.load(from: """ [[block]] command = "echo hi" + name = "c" """) #expect(blocks[0].events == nil) @@ -161,6 +164,7 @@ private final class FakeEventSource: PHEventSource { let block = try PHBlock.load(from: """ [[block]] command = "n=$(cat \(path) 2>/dev/null || echo 0); n=$((n+1)); echo $n > \(path); echo $n" + name = "d" events = ["volume"] """)[0] block.registry = registry @@ -197,6 +201,7 @@ private final class FakeEventSource: PHEventSource { let block = try PHBlock.load(from: """ [[block]] command = "n=$(cat \(path) 2>/dev/null || echo 0); n=$((n+1)); echo $n > \(path); echo $n" + name = "e" refresh = 0.2 events = ["volume"] """)[0] diff --git a/Tests/phbarTests/PHThemeWindowTests.swift b/Tests/phbarTests/PHThemeWindowTests.swift index e4ab281..db78d7d 100644 --- a/Tests/phbarTests/PHThemeWindowTests.swift +++ b/Tests/phbarTests/PHThemeWindowTests.swift @@ -109,7 +109,7 @@ private struct DimensionWrapper: Decodable { styles: nil ) let config = PHConfig(theme: "default", window: "default", blocks: nil, env: nil, monitors: nil) - let blocks = try PHBlock.load(from: "[[block]]\ncommand = \"echo x\"") + let blocks = try PHBlock.load(from: "[[block]]\nname = \"x\"\ncommand = \"echo x\"") let controller = BarController(config: config, screen: screen, theme: theme, blocks: blocks, debug: false) let frame = BarWindow.computeFrame(from: controller) @@ -128,7 +128,7 @@ private struct DimensionWrapper: Decodable { styles: nil ) let config = PHConfig(theme: "default", window: "abs", blocks: nil, env: nil, monitors: nil) - let blocks = try PHBlock.load(from: "[[block]]\ncommand = \"echo x\"") + let blocks = try PHBlock.load(from: "[[block]]\nname = \"x\"\ncommand = \"echo x\"") let controller = BarController(config: config, screen: screen, theme: theme, blocks: blocks, debug: false) let frame = BarWindow.computeFrame(from: controller) @@ -155,7 +155,7 @@ private struct DimensionWrapper: Decodable { styles: nil ) let config = PHConfig(theme: "default", window: "inset", blocks: nil, env: nil, monitors: nil) - let blocks = try PHBlock.load(from: "[[block]]\ncommand = \"echo x\"") + let blocks = try PHBlock.load(from: "[[block]]\nname = \"x\"\ncommand = \"echo x\"") let controller = BarController(config: config, screen: screen, theme: theme, blocks: blocks, debug: false) let frame = BarWindow.computeFrame(from: controller)