make name the core block info

This commit is contained in:
2026-07-12 18:19:41 +02:00
parent f5e1ba1104
commit 9a7710d675
7 changed files with 43 additions and 9 deletions
+8 -3
View File
@@ -4,8 +4,8 @@ import Foundation
final class PHBlock: ObservableObject, Decodable, Identifiable { final class PHBlock: ObservableObject, Decodable, Identifiable {
let id = UUID() let id = UUID()
let command: String let name: String
let name: String? let _command: String?
let textName: String? let textName: String?
@Published var text: PHThemeText = .default @Published var text: PHThemeText = .default
let styleName: String? let styleName: String?
@@ -14,6 +14,10 @@ final class PHBlock: ObservableObject, Decodable, Identifiable {
let centered: Bool? let centered: Bool?
var debug: Bool = false var debug: Bool = false
var command: String {
_command ?? PHPaths.configDirectory.appending(path: "scripts/\(name)").relativePath
}
var visible: Bool { var visible: Bool {
label != nil && !label!.isEmpty label != nil && !label!.isEmpty
} }
@@ -39,7 +43,8 @@ final class PHBlock: ObservableObject, Decodable, Identifiable {
var updateScheduled = false var updateScheduled = false
private enum CodingKeys: String, CodingKey { private enum CodingKeys: String, CodingKey {
case command, name case name
case _command = "command"
case textName = "text" case textName = "text"
case styleName = "style" case styleName = "style"
case refresh, events, centered case refresh, events, centered
@@ -4,7 +4,7 @@ enum PHBlockKind: String {
extension PHBlock { extension PHBlock {
var kind: PHBlockKind { var kind: PHBlockKind {
if command.starts(with: "_space") { return .space } if name.starts(with: "_space") { return .space }
return .text return .text
} }
} }
+1 -1
View File
@@ -4,7 +4,7 @@ struct SpaceBlockView: View {
@ObservedObject var block: PHBlock @ObservedObject var block: PHBlock
var width: CGFloat? { 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 } guard let width = Double(arg) else { return nil }
return CGFloat(width) return CGFloat(width)
} }
@@ -20,9 +20,11 @@ import Testing
let blocks = try PHBlock.load(from: """ let blocks = try PHBlock.load(from: """
[[block]] [[block]]
command = "printf a" command = "printf a"
name = "a"
[[block]] [[block]]
command = "printf b" command = "printf b"
name = "b"
""") """)
let config = try PHConfig.load() let config = try PHConfig.load()
+23 -1
View File
@@ -16,6 +16,7 @@ import Testing
[[block]] [[block]]
command = "echo world" command = "echo world"
name = "world"
""" """
let blocks = try PHBlock.load(from: toml) let blocks = try PHBlock.load(from: toml)
@@ -25,7 +26,7 @@ import Testing
#expect(blocks[0].name == "greeting") #expect(blocks[0].name == "greeting")
#expect(blocks[0].refresh == 5.0) #expect(blocks[0].refresh == 5.0)
#expect(blocks[0].label == nil) #expect(blocks[0].label == nil)
#expect(blocks[1].name == nil) #expect(blocks[1].name == "world")
#expect(blocks[1].refresh == nil) #expect(blocks[1].refresh == nil)
} }
@@ -73,11 +74,28 @@ private func makeBlocksConfigDir() throws -> URL {
// MARK: - Compute // 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 @MainActor
@Test func computeReturnsCommandStdout() async throws { @Test func computeReturnsCommandStdout() async throws {
let blocks = try PHBlock.load(from: """ let blocks = try PHBlock.load(from: """
[[block]] [[block]]
command = "printf panini" command = "printf panini"
name = "panini"
""") """)
let output = await blocks[0].compute() let output = await blocks[0].compute()
@@ -95,6 +113,7 @@ private func makeBlocksConfigDir() throws -> URL {
let blocks = try PHBlock.load(from: """ let blocks = try PHBlock.load(from: """
[[block]] [[block]]
command = "pwd" command = "pwd"
name = "pwd"
""") """)
let output = await blocks[0].compute() let output = await blocks[0].compute()
@@ -112,6 +131,7 @@ private func makeBlocksConfigDir() throws -> URL {
let block = try PHBlock.load(from: """ let block = try PHBlock.load(from: """
[[block]] [[block]]
command = "printf panini" command = "printf panini"
name = "panini"
""")[0] """)[0]
#expect(block.label == nil) #expect(block.label == nil)
@@ -126,6 +146,7 @@ private func makeBlocksConfigDir() throws -> URL {
let block = try PHBlock.load(from: """ let block = try PHBlock.load(from: """
[[block]] [[block]]
command = "printf hi" command = "printf hi"
name = "hi"
""")[0] """)[0]
block.startAutoRefresh() block.startAutoRefresh()
@@ -149,6 +170,7 @@ private func makeBlocksConfigDir() throws -> URL {
let toml = """ let toml = """
[[block]] [[block]]
command = "n=$(cat \(path) 2>/dev/null || echo 0); n=$((n+1)); echo $n > \(path); echo $n" command = "n=$(cat \(path) 2>/dev/null || echo 0); n=$((n+1)); echo $n > \(path); echo $n"
name = "counter"
refresh = 0.05 refresh = 0.05
""" """
+5
View File
@@ -32,6 +32,7 @@ private final class FakeEventSource: PHEventSource {
let toml = """ let toml = """
[[block]] [[block]]
command = "echo hi" command = "echo hi"
name = "a"
events = ["volume", "network", "appearance", "power", "mpd"] events = ["volume", "network", "appearance", "power", "mpd"]
""" """
@@ -47,6 +48,7 @@ private final class FakeEventSource: PHEventSource {
let blocks = try PHBlock.load(from: """ let blocks = try PHBlock.load(from: """
[[block]] [[block]]
command = "echo hi" command = "echo hi"
name = "b"
events = ["totally_made_up"] events = ["totally_made_up"]
""") """)
@@ -58,6 +60,7 @@ private final class FakeEventSource: PHEventSource {
let blocks = try PHBlock.load(from: """ let blocks = try PHBlock.load(from: """
[[block]] [[block]]
command = "echo hi" command = "echo hi"
name = "c"
""") """)
#expect(blocks[0].events == nil) #expect(blocks[0].events == nil)
@@ -161,6 +164,7 @@ private final class FakeEventSource: PHEventSource {
let block = try PHBlock.load(from: """ let block = try PHBlock.load(from: """
[[block]] [[block]]
command = "n=$(cat \(path) 2>/dev/null || echo 0); n=$((n+1)); echo $n > \(path); echo $n" command = "n=$(cat \(path) 2>/dev/null || echo 0); n=$((n+1)); echo $n > \(path); echo $n"
name = "d"
events = ["volume"] events = ["volume"]
""")[0] """)[0]
block.registry = registry block.registry = registry
@@ -197,6 +201,7 @@ private final class FakeEventSource: PHEventSource {
let block = try PHBlock.load(from: """ let block = try PHBlock.load(from: """
[[block]] [[block]]
command = "n=$(cat \(path) 2>/dev/null || echo 0); n=$((n+1)); echo $n > \(path); echo $n" command = "n=$(cat \(path) 2>/dev/null || echo 0); n=$((n+1)); echo $n > \(path); echo $n"
name = "e"
refresh = 0.2 refresh = 0.2
events = ["volume"] events = ["volume"]
""")[0] """)[0]
+3 -3
View File
@@ -109,7 +109,7 @@ private struct DimensionWrapper: Decodable {
styles: nil styles: nil
) )
let config = PHConfig(theme: "default", window: "default", blocks: nil, env: nil, monitors: 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 controller = BarController(config: config, screen: screen, theme: theme, blocks: blocks, debug: false)
let frame = BarWindow.computeFrame(from: controller) let frame = BarWindow.computeFrame(from: controller)
@@ -128,7 +128,7 @@ private struct DimensionWrapper: Decodable {
styles: nil styles: nil
) )
let config = PHConfig(theme: "default", window: "abs", blocks: nil, env: nil, monitors: 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 controller = BarController(config: config, screen: screen, theme: theme, blocks: blocks, debug: false)
let frame = BarWindow.computeFrame(from: controller) let frame = BarWindow.computeFrame(from: controller)
@@ -155,7 +155,7 @@ private struct DimensionWrapper: Decodable {
styles: nil styles: nil
) )
let config = PHConfig(theme: "default", window: "inset", blocks: nil, env: nil, monitors: 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 controller = BarController(config: config, screen: screen, theme: theme, blocks: blocks, debug: false)
let frame = BarWindow.computeFrame(from: controller) let frame = BarWindow.computeFrame(from: controller)