Skip to content
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

Add the remaining search options #9

Merged
merged 1 commit into from
Apr 13, 2023
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
19 changes: 19 additions & 0 deletions osub/Client/Client.swift
Original file line number Diff line number Diff line change
Expand Up @@ -64,12 +64,31 @@ public final class Client: ClientProtocol {

public func url(path: String, with queryItems: [URLQueryItem]) throws -> URL {
var url = try url(path: path)

guard !queryItems.isEmpty else {
return url.absoluteURL
}

let queryItems: [URLQueryItem] =
[
// Crutch. Some items cannot be the first, so it is necessary to insert
// a dummy item at the beginning.
URLQueryItem(name: "&", value: nil)
]
+ queryItems.compactMap { queryItem in
guard let value = queryItem.value else {
return nil
}
return URLQueryItem(
name: queryItem.name,
value: value.replacingOccurrences(of: " ", with: "+")
)
}

guard url.append2(queryItems: queryItems) else {
throw ClientError.cannotCreateURL
}

return url.absoluteURL
}

Expand Down
9 changes: 9 additions & 0 deletions osub/Command/AuthenticationCommand.swift
Original file line number Diff line number Diff line change
Expand Up @@ -256,6 +256,15 @@ extension AuthenticationStatusCommand {
.remainingDownloads
]
}

var text: String {
switch self {
case .remainingDownloads:
return "remaining_downloads"
case .userID:
return "user_id"
}
}
}
}

Expand Down
37 changes: 37 additions & 0 deletions osub/Command/Command.swift
Original file line number Diff line number Diff line change
Expand Up @@ -32,3 +32,40 @@ extension ClientProtocol {
)
}
}

indirect enum ValueName {
case array(ValueName)
case `enum`
case int
case path
case string

var rawValue: String {
switch self {
case .array(let valueName):
return "[\(valueName.rawValue)]"
case .enum:
return "enum"
case .int:
return "int"
case .path:
return "path"
case .string:
return "string"
}
}
}

extension ArgumentHelp {
init(
_ abstract: String = "",
discussion: String = "",
valueName: ValueName? = nil
) {
self.init(
abstract,
discussion: discussion,
valueName: valueName?.rawValue
)
}
}
2 changes: 1 addition & 1 deletion osub/Command/DownloadCommand.swift
Original file line number Diff line number Diff line change
Expand Up @@ -15,7 +15,7 @@ struct DownloadCommand: AsyncParsableCommand {
name: .shortAndLong,
help: ArgumentHelp(
"The file ID from subtitles search results.",
valueName: "int"
valueName: .int
)
)
var fileID: Int
Expand Down
8 changes: 3 additions & 5 deletions osub/Command/Formatting.swift
Original file line number Diff line number Diff line change
Expand Up @@ -3,25 +3,23 @@ import TablePrinter

protocol FormattingField: RawRepresentable<String>, CaseIterable, ExpressibleByArgument {
static var defaultValues: [Self] { get }
var text: String { get }
}

struct FormattingOptions<Field>: ParsableArguments where Field: FormattingField {
@Option(
parsing: .upToNextOption,
help: ArgumentHelp(
"Space-separated list of fields to print.",
discussion: "The list of available fields: \(Field.allValueStrings.joined(separator: ", "))."
valueName: .array(.enum)
)
)
var fields = Field.defaultValues

func printer() -> TablePrinter {
var printer = TablePrinter()
fields.forEach { field in
let header = field
.rawValue
.replacingOccurrences(of: "_", with: " ")
.uppercased()
let header = field.text.uppercased()
printer.append(header)
}
printer.next()
Expand Down
4 changes: 4 additions & 0 deletions osub/Command/LanguagesCommand.swift
Original file line number Diff line number Diff line change
Expand Up @@ -61,5 +61,9 @@ extension LanguagesCommand {
.name
]
}

var text: String {
rawValue
}
}
}
Loading