Skip to content

Commit d9da1f2

Browse files
author
Luke
authored
Merge pull request #227 from lukepistrol/terminal-colors
2 parents a6eced1 + 92d03dc commit d9da1f2

File tree

13 files changed

+277
-56
lines changed

13 files changed

+277
-56
lines changed

CodeEdit/Documents/WorkspaceCodeFileView.swift

Lines changed: 4 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -14,7 +14,8 @@ struct WorkspaceCodeFileView: View {
1414
var windowController: NSWindowController
1515
@ObservedObject var workspace: WorkspaceDocument
1616

17-
@ViewBuilder var codeView: some View {
17+
@ViewBuilder
18+
var codeView: some View {
1819
if let item = workspace.selectionState.openFileItems.first(where: { file in
1920
if file.id == workspace.selectionState.selectedId {
2021
print("Item loaded is: ", file.url)
@@ -30,11 +31,6 @@ struct WorkspaceCodeFileView: View {
3031
BreadcrumbsView(item, workspace: workspace)
3132
}
3233
}
33-
.safeAreaInset(edge: .bottom) {
34-
if let url = workspace.fileURL {
35-
StatusBarView(workspaceURL: url)
36-
}
37-
}
3834
} else {
3935
Text("CodeEdit cannot open this file because its file type is not supported.")
4036
}
@@ -44,12 +40,7 @@ struct WorkspaceCodeFileView: View {
4440
}
4541

4642
var body: some View {
47-
HSplitView {
48-
codeView
49-
.frame(maxWidth: .infinity, maxHeight: .infinity)
50-
51-
InspectorSidebar(workspace: workspace, windowController: windowController)
52-
.frame(minWidth: 250, maxWidth: 250, maxHeight: .infinity)
53-
}
43+
codeView
44+
.frame(maxWidth: .infinity, maxHeight: .infinity)
5445
}
5546
}

CodeEdit/Settings/SettingsView.swift

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -20,7 +20,7 @@ struct SettingsView: View {
2020
}
2121
}
2222
.padding()
23-
.frame(width: 450, height: 200)
23+
.frame(width: 600, height: 250)
2424
}
2525
}
2626

CodeEdit/Settings/TerminalSettingsView.swift

Lines changed: 34 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -15,6 +15,8 @@ struct TerminalSettingsView: View {
1515
@AppStorage(TerminalFontName.storageKey) var terminalFontName: String = TerminalFontName.default
1616
@AppStorage(TerminalFontSize.storageKey) var terminalFontSize: Int = TerminalFontSize.default
1717

18+
@StateObject private var colors = AnsiColors.shared
19+
1820
var body: some View {
1921
Form {
2022
Picker("Terminal Shell".localized(), selection: $shellType) {
@@ -37,9 +39,41 @@ struct TerminalSettingsView: View {
3739
FontPicker("\(terminalFontName) \(terminalFontSize)", name: $terminalFontName, size: $terminalFontSize)
3840
}
3941
}
42+
Divider()
43+
Section {
44+
VStack(alignment: .leading, spacing: 0) {
45+
HStack(spacing: 0) {
46+
ansiColorPicker($colors.colors[0])
47+
ansiColorPicker($colors.colors[1])
48+
ansiColorPicker($colors.colors[2])
49+
ansiColorPicker($colors.colors[3])
50+
ansiColorPicker($colors.colors[4])
51+
ansiColorPicker($colors.colors[5])
52+
ansiColorPicker($colors.colors[6])
53+
ansiColorPicker($colors.colors[7])
54+
Text("Normal").padding(.leading, 4)
55+
}
56+
HStack(spacing: 0) {
57+
ansiColorPicker($colors.colors[8])
58+
ansiColorPicker($colors.colors[9])
59+
ansiColorPicker($colors.colors[10])
60+
ansiColorPicker($colors.colors[11])
61+
ansiColorPicker($colors.colors[12])
62+
ansiColorPicker($colors.colors[13])
63+
ansiColorPicker($colors.colors[14])
64+
ansiColorPicker($colors.colors[15])
65+
Text("Bright").padding(.leading, 4)
66+
}
67+
}
68+
}
4069
}
4170
.padding()
4271
}
72+
73+
private func ansiColorPicker(_ color: Binding<Color>) -> some View {
74+
ColorPicker(selection: color, supportsOpacity: false) { }
75+
.labelsHidden()
76+
}
4377
}
4478

4579
struct TerminalSettingsView_Previews: PreviewProvider {

CodeEdit/WorkspaceView.swift

Lines changed: 21 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -7,6 +7,7 @@
77

88
import SwiftUI
99
import WorkspaceClient
10+
import StatusBar
1011

1112
struct WorkspaceView: View {
1213
init(windowController: NSWindowController, workspace: WorkspaceDocument) {
@@ -28,10 +29,7 @@ struct WorkspaceView: View {
2829
var body: some View {
2930
NavigationView {
3031
if workspace.workspaceClient != nil {
31-
LeadingSidebar(workspace: workspace, windowController: windowController)
32-
.frame(minWidth: 250)
33-
WorkspaceCodeFileView(windowController: windowController,
34-
workspace: workspace)
32+
content
3533
} else {
3634
EmptyView()
3735
}
@@ -49,6 +47,25 @@ struct WorkspaceView: View {
4947
}
5048
}
5149
}
50+
51+
@ViewBuilder
52+
private var content: some View {
53+
LeadingSidebar(workspace: workspace, windowController: windowController)
54+
.frame(minWidth: 250)
55+
HSplitView {
56+
ZStack {
57+
WorkspaceCodeFileView(windowController: windowController,
58+
workspace: workspace)
59+
}
60+
.safeAreaInset(edge: .bottom) {
61+
if let url = workspace.fileURL {
62+
StatusBarView(workspaceURL: url)
63+
}
64+
}
65+
InspectorSidebar(workspace: workspace, windowController: windowController)
66+
.frame(minWidth: 250, maxWidth: 250, maxHeight: .infinity)
67+
}
68+
}
5269
}
5370

5471
struct WorkspaceView_Previews: PreviewProvider {

CodeEditModules/Modules/StatusBar/src/Model/StatusBarModel.swift

Lines changed: 7 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -48,6 +48,7 @@ public class StatusBarModel: ObservableObject {
4848
/// A GitClient instance
4949
private (set) var gitClient: GitClient
5050

51+
/// The base URL of the workspace
5152
private (set) var workspaceURL: URL
5253

5354
/// The maximum height of the drawer
@@ -62,8 +63,8 @@ public class StatusBarModel: ObservableObject {
6263
// TODO: Add @Published vars for indentation, encoding, linebreak
6364

6465
/// Initialize with a GitClient
65-
/// - Parameter gitClient: a GitClient
66-
66+
/// - Parameter workspaceURL: the current workspace URL
67+
///
6768
public init(workspaceURL: URL) {
6869
self.workspaceURL = workspaceURL
6970
self.gitClient = GitClient.default(directoryURL: workspaceURL)
@@ -72,5 +73,9 @@ public class StatusBarModel: ObservableObject {
7273
} else {
7374
self.selectedBranch = gitClient.getCurrentBranchName()
7475
}
76+
StatusBarModel.shared = self
7577
}
78+
79+
/// shared instance that persists though session
80+
public static var shared: StatusBarModel?
7681
}

CodeEditModules/Modules/StatusBar/src/StatusBar.swift

Lines changed: 10 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -25,13 +25,16 @@ public struct StatusBarView: View {
2525
/// Initialize with GitClient
2626
/// - Parameter gitClient: a GitClient
2727
public init(workspaceURL: URL) {
28-
self.model = .init(workspaceURL: workspaceURL)
28+
self.model = .shared ?? .init(workspaceURL: workspaceURL)
2929
}
3030

3131
public var body: some View {
3232
VStack(spacing: 0) {
3333
bar
34-
StatusBarDrawer(model: model)
34+
if model.isExpanded {
35+
StatusBarDrawer(model: model)
36+
.transition(.move(edge: .bottom))
37+
}
3538
}
3639
// removes weird light gray bar above when in light mode
3740
.padding(.top, -8) // (comment out to make it look normal in preview)
@@ -63,6 +66,11 @@ public struct StatusBarView: View {
6366
.overlay(alignment: .top) {
6467
Divider()
6568
}
69+
.overlay(alignment: .bottom) {
70+
if model.isExpanded {
71+
Divider()
72+
}
73+
}
6674
.frame(height: 32)
6775
.gesture(dragGesture)
6876
.onHover { isHovering($0, isDragging: model.isDragging, cursor: .resizeUpDown) }

CodeEditModules/Modules/StatusBar/src/StatusBarItems/StatusBarDrawer.swift

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -17,6 +17,7 @@ internal struct StatusBarDrawer: View {
1717
}
1818

1919
internal var body: some View {
20+
// Rectangle()
2021
TerminalEmulatorView(url: model.workspaceURL)
2122
.frame(minHeight: 0,
2223
idealHeight: model.isExpanded ? model.currentHeight : 0,
Lines changed: 34 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,34 @@
1+
//
2+
// SwiftTerm+Color+Init.swift
3+
//
4+
//
5+
// Created by Lukas Pistrol on 24.03.22.
6+
//
7+
8+
import SwiftTerm
9+
10+
internal extension SwiftTerm.Color {
11+
/// 0.0-1.0
12+
convenience init(dRed red: Double, green: Double, blue: Double) {
13+
let multiplier: Double = 65535
14+
self.init(red: UInt16(red * multiplier),
15+
green: UInt16(green * multiplier),
16+
blue: UInt16(blue * multiplier))
17+
}
18+
19+
/// 0-255
20+
convenience init(iRed red: UInt8, green: UInt8, blue: UInt8) {
21+
let divisor: Double = 255
22+
self.init(dRed: Double(red) / divisor,
23+
green: Double(green) / divisor,
24+
blue: Double(blue) / divisor)
25+
}
26+
27+
/// 0x000000 - 0xFFFFFF
28+
convenience init(hex: Int) {
29+
let red = UInt8((hex >> 16) & 0xFF)
30+
let green = UInt8((hex >> 8) & 0xFF)
31+
let blue = UInt8(hex & 0xFF)
32+
self.init(iRed: red, green: green, blue: blue)
33+
}
34+
}
Lines changed: 75 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,75 @@
1+
//
2+
// File.swift
3+
//
4+
//
5+
// Created by Lukas Pistrol on 23.03.22.
6+
//
7+
8+
import SwiftUI
9+
10+
public class AnsiColors: ObservableObject {
11+
// public static let `default` =
12+
public static let storageKey: String = "ANSIColors"
13+
public static let shared: AnsiColors = .init()
14+
15+
public init() {
16+
guard let loadColors = UserDefaults.standard.object(forKey: Self.storageKey) as? [Int] else {
17+
colors.append(Color(red: 0.000, green: 0.000, blue: 0.000))
18+
colors.append(Color(red: 0.600, green: 0.000, blue: 0.000))
19+
colors.append(Color(red: 0.000, green: 0.651, blue: 0.004))
20+
colors.append(Color(red: 0.600, green: 0.600, blue: 0.000))
21+
colors.append(Color(red: 0.000, green: 0.031, blue: 0.702))
22+
colors.append(Color(red: 0.702, green: 0.020, blue: 0.702))
23+
colors.append(Color(red: 0.000, green: 0.647, blue: 0.702))
24+
colors.append(Color(red: 0.749, green: 0.749, blue: 0.749))
25+
colors.append(Color(red: 0.400, green: 0.400, blue: 0.400))
26+
colors.append(Color(red: 0.902, green: 0.000, blue: 0.004))
27+
colors.append(Color(red: 0.004, green: 0.851, blue: 0.000))
28+
colors.append(Color(red: 0.902, green: 0.898, blue: 0.012))
29+
colors.append(Color(red: 0.000, green: 0.063, blue: 1.000))
30+
colors.append(Color(red: 0.902, green: 0.035, blue: 0.902))
31+
colors.append(Color(red: 0.008, green: 0.902, blue: 0.898))
32+
colors.append(Color(red: 0.902, green: 0.902, blue: 0.902))
33+
return
34+
}
35+
print("loaded")
36+
self.mappedColors = loadColors
37+
}
38+
39+
@Published public var mappedColors: [Int] = [] {
40+
didSet {
41+
UserDefaults.standard.set(mappedColors, forKey: AnsiColors.storageKey)
42+
print("saved")
43+
}
44+
}
45+
46+
public var colors: [Color] {
47+
get {
48+
let mapped = self.mappedColors.map { Color(hex: $0) }
49+
return mapped
50+
}
51+
set {
52+
self.mappedColors = newValue.map { $0.hex }
53+
}
54+
55+
}
56+
}
57+
58+
public extension Color {
59+
init(hex: Int) {
60+
let red = (hex >> 16) & 0xFF
61+
let green = (hex >> 8) & 0xFF
62+
let blue = hex & 0xFF
63+
self.init(.sRGB, red: Double(red) / 255, green: Double(green) / 255, blue: Double(blue) / 255, opacity: 1)
64+
}
65+
66+
var hex: Int {
67+
guard let components = cgColor?.components, components.count >= 3 else { return 0 }
68+
69+
let red = Int(components[0] * 255) << 16
70+
let green = Int(components[1] * 255) << 8
71+
let blue = Int(components[2] * 255)
72+
73+
return red | green | blue
74+
}
75+
}

CodeEditModules/Modules/TerminalEmulator/src/TerminalFont.swift renamed to CodeEditModules/Modules/TerminalEmulator/src/Model/TerminalFont.swift

File renamed without changes.

0 commit comments

Comments
 (0)