Repository navigation
Fix cmux.json named workspace colors #3149
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Merged
Merged
Changes from all commits
Commits
Show all changes
6 commits
Select commit
Hold shift + click to select a range
4b44481
Add cmux.json named color regression test
lawrencecchen 4c55836
Accept named colors in cmux.json
lawrencecchen 74c0cdf
Merge remote-tracking branch 'origin/main' into issue-3075-cmux-json-…
lawrencecchen b2b829a
Merge remote-tracking branch 'origin/main' into issue-3075-cmux-json-…
lawrencecchen d11eb4d
Invalidate cmux config color cache on palette changes
lawrencecchen 8969041
Make named color negative test hermetic
lawrencecchen File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,36 @@ | ||
| import Foundation | ||
|
|
||
| struct CmuxWorkspaceDefinition: Codable, Sendable { | ||
| var name: String? | ||
| var cwd: String? | ||
| var color: String? | ||
| var layout: CmuxLayoutNode? | ||
|
|
||
| init(name: String? = nil, cwd: String? = nil, color: String? = nil, layout: CmuxLayoutNode? = nil) { | ||
| self.name = name | ||
| self.cwd = cwd | ||
| self.color = color | ||
| self.layout = layout | ||
| } | ||
|
|
||
| init(from decoder: Decoder) throws { | ||
| let container = try decoder.container(keyedBy: CodingKeys.self) | ||
| name = try container.decodeIfPresent(String.self, forKey: .name) | ||
| cwd = try container.decodeIfPresent(String.self, forKey: .cwd) | ||
| layout = try container.decodeIfPresent(CmuxLayoutNode.self, forKey: .layout) | ||
|
|
||
| if let rawColor = try container.decodeIfPresent(String.self, forKey: .color) { | ||
| let defaults = decoder.userInfo[.cmuxWorkspaceColorDefaults] as? UserDefaults ?? .standard | ||
| guard let normalized = WorkspaceTabColorSettings.resolvedColorHex(rawColor, defaults: defaults) else { | ||
| throw DecodingError.dataCorruptedError( | ||
| forKey: .color, | ||
| in: container, | ||
| debugDescription: "Invalid color \"\(rawColor)\". Expected 6-digit hex format (#RRGGBB) or a workspace color name" | ||
| ) | ||
| } | ||
| color = normalized | ||
| } else { | ||
| color = nil | ||
| } | ||
| } | ||
| } |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,22 @@ | ||
| import Foundation | ||
|
|
||
| extension WorkspaceTabColorSettings { | ||
| static func resolvedColorHex(_ raw: String, defaults: UserDefaults = .standard) -> String? { | ||
| if let normalized = normalizedHex(raw) { | ||
| return normalized | ||
| } | ||
|
|
||
| let trimmed = raw.trimmingCharacters(in: .whitespacesAndNewlines) | ||
| guard !trimmed.isEmpty else { return nil } | ||
| return resolvedPaletteMap(defaults: defaults) | ||
| .first { name, _ in name.caseInsensitiveCompare(trimmed) == .orderedSame }? | ||
| .value | ||
| } | ||
|
|
||
| static func paletteCacheFingerprint(defaults: UserDefaults = .standard) -> String { | ||
| resolvedPaletteMap(defaults: defaults) | ||
| .sorted { lhs, rhs in lhs.key.localizedStandardCompare(rhs.key) == .orderedAscending } | ||
| .map { "\($0.key)=\($0.value)" } | ||
| .joined(separator: "\n") | ||
| } | ||
| } |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,100 @@ | ||
| import XCTest | ||
|
|
||
| #if canImport(cmux_DEV) | ||
| @testable import cmux_DEV | ||
| #elseif canImport(cmux) | ||
| @testable import cmux | ||
| #endif | ||
|
|
||
| final class CmuxConfigNamedColorTests: XCTestCase { | ||
| private func decode(_ json: String, colorDefaults: UserDefaults? = nil) throws -> CmuxConfigFile { | ||
| let data = json.data(using: .utf8)! | ||
| let decoder = JSONDecoder() | ||
| if let colorDefaults { | ||
| decoder.userInfo[.cmuxWorkspaceColorDefaults] = colorDefaults | ||
| } | ||
| return try decoder.decode(CmuxConfigFile.self, from: data) | ||
| } | ||
|
|
||
| func testDecodeWorkspaceCommandAcceptsNamedColor() throws { | ||
| let suiteName = "cmux-config-named-color-\(UUID().uuidString)" | ||
| let defaults = UserDefaults(suiteName: suiteName)! | ||
| defer { defaults.removePersistentDomain(forName: suiteName) } | ||
| WorkspaceTabColorSettings.persistPaletteMap(["Indigo": "#283593"], defaults: defaults) | ||
|
|
||
| let json = """ | ||
| { | ||
| "commands": [{ | ||
| "name": "Dev env", | ||
| "workspace": { | ||
| "name": "Development", | ||
| "color": "Indigo" | ||
| } | ||
| }] | ||
| } | ||
| """ | ||
| let config = try decode(json, colorDefaults: defaults) | ||
| XCTAssertEqual(config.commands[0].workspace?.color, "#283593") | ||
| } | ||
|
|
||
| func testDecodeWorkspaceCommandRejectsUnknownNamedColor() { | ||
| let suiteName = "cmux-config-unknown-color-\(UUID().uuidString)" | ||
| let defaults = UserDefaults(suiteName: suiteName)! | ||
| defer { defaults.removePersistentDomain(forName: suiteName) } | ||
|
|
||
| let json = """ | ||
| { | ||
| "commands": [{ | ||
| "name": "Dev env", | ||
| "workspace": { | ||
| "name": "Development", | ||
| "color": "Definitely Not A Palette Color" | ||
| } | ||
| }] | ||
| } | ||
| """ | ||
| XCTAssertThrowsError(try decode(json, colorDefaults: defaults)) | ||
| } | ||
|
|
||
| @MainActor | ||
| func testConfigParseCacheInvalidatesWhenWorkspaceColorPaletteChanges() throws { | ||
| let root = FileManager.default.temporaryDirectory.appendingPathComponent( | ||
| "cmux-config-store-\(UUID().uuidString)", | ||
| isDirectory: true | ||
| ) | ||
| try FileManager.default.createDirectory(at: root, withIntermediateDirectories: true) | ||
| defer { try? FileManager.default.removeItem(at: root) } | ||
|
|
||
| let previousPalette = UserDefaults.standard.dictionary(forKey: WorkspaceTabColorSettings.paletteKey) | ||
| defer { | ||
| if let previousPalette { | ||
| UserDefaults.standard.set(previousPalette, forKey: WorkspaceTabColorSettings.paletteKey) | ||
| } else { | ||
| UserDefaults.standard.removeObject(forKey: WorkspaceTabColorSettings.paletteKey) | ||
| } | ||
| } | ||
|
|
||
| let configURL = root.appendingPathComponent("cmux.json") | ||
| let json = """ | ||
| { | ||
| "commands": [{ | ||
| "name": "Dev env", | ||
| "workspace": { | ||
| "name": "Development", | ||
| "color": "Codex Test" | ||
| } | ||
| }] | ||
| } | ||
| """ | ||
| try json.write(to: configURL, atomically: true, encoding: .utf8) | ||
|
|
||
| let store = CmuxConfigStore(globalConfigPath: configURL.path, startFileWatchers: false) | ||
| WorkspaceTabColorSettings.persistPaletteMap(["Codex Test": "#111111"]) | ||
| store.loadAll() | ||
| XCTAssertEqual(store.loadedCommands.first?.workspace?.color, "#111111") | ||
|
|
||
| WorkspaceTabColorSettings.persistPaletteMap(["Codex Test": "#222222"]) | ||
| store.loadAll() | ||
| XCTAssertEqual(store.loadedCommands.first?.workspace?.color, "#222222") | ||
| } | ||
| } | ||
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
Uh oh!
There was an error while loading. Please reload this page.