-
Notifications
You must be signed in to change notification settings - Fork 5
fix: improve CLI text output formatting #21
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
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,101 @@ | ||
| import Darwin | ||
| import Testing | ||
| import XCDocs | ||
|
|
||
| @testable import XCDocsCLI | ||
|
|
||
| @Suite("Search Text Output") | ||
| struct SearchTextOutputTests { | ||
|
julianschiavo marked this conversation as resolved.
|
||
| @Test | ||
| func searchResultUsesExpandedMetadataLayout() throws { | ||
| guard #available(macOS 26, *) else { return } | ||
| try assertSearchResultUsesExpandedMetadataLayout() | ||
| } | ||
|
|
||
| @Test | ||
| func searchResultAlignsMetadataWithDoubleDigitIndex() throws { | ||
| guard #available(macOS 26, *) else { return } | ||
| try assertSearchResultAlignsMetadataWithDoubleDigitIndex() | ||
| } | ||
|
|
||
| @Test | ||
| func getOutputUsesNoLeadingIndentation() throws { | ||
| guard #available(macOS 26, *) else { return } | ||
| try assertGetOutputUsesNoLeadingIndentation() | ||
| } | ||
| } | ||
|
|
||
| @available(macOS 26, *) | ||
| private func assertSearchResultUsesExpandedMetadataLayout() throws { | ||
| let result = makeSearchResult( | ||
| score: 0.5405, | ||
| id: "/documentation/Metal/MTLClearColor/init(red:green:blue:alpha:)", | ||
| title: "init(red:green:blue:alpha:)" | ||
| ) | ||
|
|
||
| let output = try withEnvironment(variable: "CLICOLOR_FORCE", value: "1") { | ||
| renderTextEntry(result.entry, score: result.score, index: 1) | ||
| } | ||
|
|
||
| let expectedOutput = [ | ||
| bold("1. init(red:green:blue:alpha:)"), " \(bold("Kind:")) symbol", " \(bold("Relevance:")) 0.5405", | ||
| " \(bold("ID:")) /documentation/Metal/MTLClearColor/init(red:green:blue:alpha:)", "", " [content]", "", "", | ||
| ].joined(separator: "\n") | ||
|
|
||
| #expect(output == expectedOutput) | ||
| } | ||
|
|
||
| @available(macOS 26, *) | ||
| private func assertSearchResultAlignsMetadataWithDoubleDigitIndex() throws { | ||
| let result = makeSearchResult(score: 0.5120, id: "/documentation/Testing/result-12", title: "Result 12") | ||
|
|
||
| let output = try withEnvironment(variable: "CLICOLOR_FORCE", value: "1") { | ||
| renderTextEntry(result.entry, score: result.score, index: 12) | ||
| } | ||
|
|
||
| let expectedBlock = [ | ||
| bold("12. Result 12"), " \(bold("Kind:")) symbol", " \(bold("Relevance:")) 0.5120", | ||
| " \(bold("ID:")) /documentation/Testing/result-12", "", " [content]", "", "", | ||
| ].joined(separator: "\n") | ||
|
|
||
| #expect(output.contains(expectedBlock)) | ||
| } | ||
|
|
||
| @available(macOS 26, *) | ||
| private func assertGetOutputUsesNoLeadingIndentation() throws { | ||
| let entry = DocumentationEntry( | ||
| id: "/documentation/Metal/MTLClearColor/init(red:green:blue:alpha:)", | ||
| framework: "Metal", | ||
| kind: .symbol, | ||
| title: "init(red:green:blue:alpha:)", | ||
| content: "[content]" | ||
| ) | ||
|
|
||
| let output = try withEnvironment(variable: "CLICOLOR_FORCE", value: "1") { renderTextEntry(entry) } | ||
|
|
||
| let expectedOutput = [ | ||
| bold("init(red:green:blue:alpha:)"), "\(bold("Kind:")) symbol", | ||
| "\(bold("ID:")) /documentation/Metal/MTLClearColor/init(red:green:blue:alpha:)", "", "[content]", "", "", | ||
| ].joined(separator: "\n") | ||
|
|
||
| #expect(output == expectedOutput) | ||
| } | ||
|
|
||
| @available(macOS 26, *) | ||
| private func makeSearchResult(score: Double, id: String, title: String) -> SearchResult { | ||
| SearchResult( | ||
| score: score, | ||
| entry: DocumentationEntry(id: id, framework: "Metal", kind: .symbol, title: title, content: "[content]") | ||
| ) | ||
| } | ||
|
|
||
| private func bold(_ string: String) -> String { "\u{001B}[1m\(string)\u{001B}[22m" } | ||
|
|
||
| private func withEnvironment<T>(variable: String, value: String, _ work: () throws -> T) throws -> T { | ||
| let previousValue = getenv(variable).map { String(cString: $0) } | ||
| setenv(variable, value, 1) | ||
|
|
||
| defer { if let previousValue { setenv(variable, previousValue, 1) } else { unsetenv(variable) } } | ||
|
|
||
| return try work() | ||
| } | ||
Oops, something went wrong.
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.