From c36aea2652214785b4da6787f5912cdf7db5f6df Mon Sep 17 00:00:00 2001 From: KC-2001MS Date: Sun, 28 Jul 2024 18:54:35 +0900 Subject: [PATCH] Adding Styles --- .../xcschemes/SwiftLI-Package.xcscheme | 102 ++++++++++++++++++ .../xcshareddata/xcschemes/sclt.xcscheme | 78 ++++++++++++++ .../SwiftLI/CLI Basic Components/Label.swift | 26 ++++- Sources/SwiftLI/Style/LabelStyle.swift | 64 +++++++++++ Sources/SwiftLI/View.swift | 7 +- Sources/sclt/Subcommands/Label.swift | 8 +- 6 files changed, 271 insertions(+), 14 deletions(-) create mode 100644 .swiftpm/xcode/xcshareddata/xcschemes/SwiftLI-Package.xcscheme create mode 100644 .swiftpm/xcode/xcshareddata/xcschemes/sclt.xcscheme create mode 100644 Sources/SwiftLI/Style/LabelStyle.swift diff --git a/.swiftpm/xcode/xcshareddata/xcschemes/SwiftLI-Package.xcscheme b/.swiftpm/xcode/xcshareddata/xcschemes/SwiftLI-Package.xcscheme new file mode 100644 index 0000000..448c1d7 --- /dev/null +++ b/.swiftpm/xcode/xcshareddata/xcschemes/SwiftLI-Package.xcscheme @@ -0,0 +1,102 @@ + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + diff --git a/.swiftpm/xcode/xcshareddata/xcschemes/sclt.xcscheme b/.swiftpm/xcode/xcshareddata/xcschemes/sclt.xcscheme new file mode 100644 index 0000000..9a15ef1 --- /dev/null +++ b/.swiftpm/xcode/xcshareddata/xcschemes/sclt.xcscheme @@ -0,0 +1,78 @@ + + + + + + + + + + + + + + + + + + + + + + + + + + + + + diff --git a/Sources/SwiftLI/CLI Basic Components/Label.swift b/Sources/SwiftLI/CLI Basic Components/Label.swift index cbd2aa6..e6658e3 100644 --- a/Sources/SwiftLI/CLI Basic Components/Label.swift +++ b/Sources/SwiftLI/CLI Basic Components/Label.swift @@ -14,6 +14,8 @@ public struct Label: View { let title: String let footer: Bool + + let style: LabelStyle /// Initializer to realize method chain /// - Parameters: @@ -23,12 +25,14 @@ public struct Label: View { header: String, title: String, image: String, - footer: Bool = false + footer: Bool = false, + style: LabelStyle = .automatic ) { self.header = "\(header)" self.title = title self.image = image self.footer = footer + self.style = style } public init( @@ -47,16 +51,32 @@ public struct Label: View { self.image = image self.title = title self.footer = false + self.style = .automatic + } + + public init( + image: String, + title: String + ) { + self.header = "" + self.image = image + self.title = title + self.footer = false + self.style = .automatic } /// What the view displays public var body: [any View] { - return [] + return style.makeBody(configuration: LabelStyleConfiguration(icon: Text(content: image), title: Text(content: title))) } /// Methods for rendering text public func render() { - print("\(header)\(image)\(title)\u{001B}[0m", terminator: footer ? "\n" : "") + Group(header: header, contents: body, footer: footer).render() + } + + public func labelStyle(_ style: LabelStyle) -> Self { + return .init(header: self.header, title: self.header, image: self.image, footer: self.footer, style: style) } /// Modifier to adapt foreground color to existing text diff --git a/Sources/SwiftLI/Style/LabelStyle.swift b/Sources/SwiftLI/Style/LabelStyle.swift new file mode 100644 index 0000000..4a09df4 --- /dev/null +++ b/Sources/SwiftLI/Style/LabelStyle.swift @@ -0,0 +1,64 @@ +// +// LabelStyle.swift +// SwiftLI +// +// Created by Keisuke Chinone on 2024/07/26. +// + + +public struct LabelStyleConfiguration { + var icon: View + + var title: View +} + +public protocol LabelStyle { + typealias Configuration = LabelStyleConfiguration + + @ViewBuilder + func makeBody(configuration: Self.Configuration) -> [any View] +} + +public struct DefaultLabelStyle: LabelStyle { + public func makeBody(configuration: Configuration) -> [any View] { + configuration.icon + Spacer() + configuration.title + } +} + +public struct IconOnlyLabelStyle: LabelStyle { + public func makeBody(configuration: Configuration) -> [any View] { + configuration.icon + } +} + +public struct TitleOnlyLabelStyle: LabelStyle { + public func makeBody(configuration: Configuration) -> [any View] { + configuration.title + } +} + +public struct TitleAndIconLabelStyle: LabelStyle { + public func makeBody(configuration: Configuration) -> [any View] { + configuration.icon + Spacer() + configuration.title + } +} + +public extension LabelStyle where Self == DefaultLabelStyle { + static var automatic: Self { .init() } +} + +public extension LabelStyle where Self == IconOnlyLabelStyle { + static var iconOnly: Self { .init() } +} + +public extension LabelStyle where Self == TitleOnlyLabelStyle { + static var titleOnly: Self { .init() } +} + +public extension LabelStyle where Self == TitleAndIconLabelStyle { + static var titleAndIcon: Self { .init() } +} diff --git a/Sources/SwiftLI/View.swift b/Sources/SwiftLI/View.swift index 562eb2f..2125775 100644 --- a/Sources/SwiftLI/View.swift +++ b/Sources/SwiftLI/View.swift @@ -61,10 +61,7 @@ public struct Text: View, Sendable, Equatable { self.contents = [String(repeating: repeating, count: count)] self.footer = footer } - /// Initializer to realize method chain - /// - Parameters: - /// - header: Hetter to specify display style - /// - contents: String to display + init( header: String, contents: Array, @@ -76,7 +73,7 @@ public struct Text: View, Sendable, Equatable { } init( - header: String, + header: String = "", content: String, footer: Bool = false ) { diff --git a/Sources/sclt/Subcommands/Label.swift b/Sources/sclt/Subcommands/Label.swift index 4117184..21d8d64 100644 --- a/Sources/sclt/Subcommands/Label.swift +++ b/Sources/sclt/Subcommands/Label.swift @@ -32,18 +32,14 @@ struct LabelCommand: ParsableCommand { Group { Group { - Text("init(_ title: String,unicodeImage: Int)") + Label("init(_ title: String,unicodeImage: Int)", unicodeImage: 0x2705) .forgroundColor(Color.cyan) Spacer() - Text("\"Label\",0x2705") + Text("\"init(_ title: String,unicodeImage: Int)\",0x2705") .fontWeight(.thin) .forgroundColor(.red) - - Spacer() - - Label("Label", unicodeImage: 0x2705) } .newLine() }