Skip to content

Commit

Permalink
Minor syntactic sugar for collections and opening URLs
Browse files Browse the repository at this point in the history
  • Loading branch information
ptsochantaris committed May 16, 2021
1 parent 24f2387 commit 9a33a29
Show file tree
Hide file tree
Showing 15 changed files with 52 additions and 57 deletions.
4 changes: 2 additions & 2 deletions Sources/trailer/API/BatchGroup.swift
Original file line number Diff line number Diff line change
Expand Up @@ -62,7 +62,7 @@ struct BatchGroup: Ingesting {

let page = pageOfIds
let newIds = idsToGroups.keys.filter { !page.contains($0) }
if !newIds.isEmpty {
if newIds.hasItems {
let nextPage = Query(name: query.name, rootElement: BatchGroup(templateGroup: originalTemplate, idList: newIds, startingCount: nextCount, perNodeBlock: perNodeBlock), parent: parent, subQuery: true)
extraQueries.append(nextPage)
}
Expand All @@ -75,7 +75,7 @@ struct BatchGroup: Ingesting {
}
}

if !extraQueries.isEmpty {
if extraQueries.hasItems {
log(level: .debug, indent: level, "\(name) will need further paging")
}
return extraQueries
Expand Down
4 changes: 2 additions & 2 deletions Sources/trailer/API/Group.swift
Original file line number Diff line number Diff line change
Expand Up @@ -48,7 +48,7 @@ struct Group: Ingesting {
}
}

if !brackets.isEmpty {
if brackets.hasItems {
query += "(" + brackets.joined(separator: ", ") + ")"
}

Expand Down Expand Up @@ -133,7 +133,7 @@ struct Group: Ingesting {
}
}

if !extraQueries.isEmpty {
if extraQueries.hasItems {
log(level: .debug, indent: level, "\(name) will need further paging")
}
return extraQueries
Expand Down
2 changes: 1 addition & 1 deletion Sources/trailer/API/Query.swift
Original file line number Diff line number Diff line change
Expand Up @@ -59,7 +59,7 @@ struct Query {
static func batching(_ name: String, fields: [Element], idList: [String], perNodeBlock: BatchGroup.NodeBlock? = nil) -> [Query] {
var list = idList
var segments = [[String]]()
while !list.isEmpty {
while list.hasItems {
let p = min(config.pageSize, list.count)
segments.append(Array(list[0..<p]))
list = Array(list[p...])
Expand Down
4 changes: 2 additions & 2 deletions Sources/trailer/Actions/Actions-Open.swift
Original file line number Diff line number Diff line change
Expand Up @@ -76,7 +76,7 @@ extension Actions {
log("[![*> *]!]\(r.nameWithOwner)")
}
} else {
repos.first!.openURL()
open(url: repos.first!.url)
}
break
default:
Expand All @@ -87,7 +87,7 @@ extension Actions {
static private func openItemURL(_ number: Int, includePrs: Bool, includeIssues: Bool) -> Bool {
if let items = findItems(number: number, includePrs: includePrs, includeIssues: includeIssues, warnIfMultiple: true) {
if items.count == 1, let item = items.first {
item.openURL()
item.openUrl()
}
return items.count > 0
}
Expand Down
6 changes: 3 additions & 3 deletions Sources/trailer/Actions/Actions.swift
Original file line number Diff line number Diff line change
Expand Up @@ -25,7 +25,7 @@ struct Actions {

private static func checkArguments() -> [String]? {
// Very rough sanity check to catch typos, should be more fine-grained per action
let invalidArguments = CommandLine.arguments.filter({ $0.hasPrefix("-") }).map { $0.lowercased() }.filter { arg in
let invalidArguments = CommandLine.arguments.filter { $0.hasPrefix("-") }.map { $0.lowercased() }.filter { arg in
switch arg {
case "-v", "-debug", "-server", "-token", "-r", "-o", "-t", "-a", "-l", "-h", "-b", "-c", "-comments", "-refresh", "-body", "-page-size",
"-mine", "-participated", "-mentioned", "-mergeable", "-conflict", "-red", "-green", "-e", "-before", "-within", "-n", "-purge", "-from", "-sort",
Expand Down Expand Up @@ -126,7 +126,7 @@ struct Actions {
}
line += (word + " ")
}
if !line.isEmpty {
if line.hasItems {
log("[!\(line)!]")
}
}
Expand All @@ -152,7 +152,7 @@ struct Actions {
}
line += (word + " ")
}
if !line.isEmpty {
if line.hasItems {
dumpLine()
}
}
Expand Down
6 changes: 3 additions & 3 deletions Sources/trailer/Actions/Notifications.swift
Original file line number Diff line number Diff line change
Expand Up @@ -24,14 +24,14 @@ struct Notifications {
}()

func go() {
if let title = title, !title.isEmpty {
if let title = title, title.hasItems {
let d = Notification.formatter.string(from: relatedDate)
log("[!\(d) \(title)!]")
}
if let subtitle = subtitle, !subtitle.isEmpty {
if let subtitle = subtitle, subtitle.hasItems {
log("[*\(subtitle)*]")
}
if let details = details, !details.isEmpty {
if let details = details, details.hasItems {
log(details)
}
log()
Expand Down
2 changes: 1 addition & 1 deletion Sources/trailer/Core/CommandLine.swift
Original file line number Diff line number Diff line change
Expand Up @@ -84,7 +84,7 @@ private struct ListSortDefinition {

let components = CommandLine.value(for: "-sort")?.split(separator: ",")
let s = components?.compactMap { Criterion(rawValue: String($0)) }
if let s = s, !s.isEmpty {
if let s = s, s.hasItems {
criteria = s
} else {
criteria = [.number, .title, .created]
Expand Down
19 changes: 19 additions & 0 deletions Sources/trailer/Core/Globals.swift
Original file line number Diff line number Diff line change
Expand Up @@ -34,3 +34,22 @@ func log(level: LogLevel = .info, indent: Int = 0, _ message: @autoclosure ()->S
}
}
}

func open(url: URL) {
log("Opening url: [*\(url)*]")
let p = Process()
p.arguments = [url.absoluteString]
#if os(OSX)
p.launchPath = "/usr/bin/open"
p.launch()
#else
p.executableURL = URL(fileURLWithPath: "/usr/bin/open")
try? p.run()
#endif
}

extension Collection {
var hasItems: Bool {
return !self.isEmpty
}
}
6 changes: 3 additions & 3 deletions Sources/trailer/Data/Accessories/ItemState.swift
Original file line number Diff line number Diff line change
Expand Up @@ -12,9 +12,9 @@ enum ItemState: String, Codable {
case open, closed, merged
init?(rawValue: String) {
switch rawValue.lowercased() {
case "open": self = ItemState.open
case "closed": self = ItemState.closed
case "merged": self = ItemState.merged
case "open": self = .open
case "closed": self = .closed
case "merged": self = .merged
default: return nil
}
}
Expand Down
17 changes: 3 additions & 14 deletions Sources/trailer/Data/Accessories/ListableItem.swift
Original file line number Diff line number Diff line change
Expand Up @@ -102,23 +102,12 @@ enum ListableItem: Equatable, Sortable {
}
}

func openURL() {
let url: URL
func openUrl() {
switch self {
case .pullRequest(let i):
url = i.url
open(url: i.url)
case .issue(let i):
url = i.url
open(url: i.url)
}
log("Opening url: [*\(url)*]")
let p = Process()
p.arguments = [url.absoluteString]
#if os(OSX)
p.launchPath = "/usr/bin/open"
p.launch()
#else
p.executableURL = URL(fileURLWithPath: "/usr/bin/open")
try? p.run()
#endif
}
}
2 changes: 1 addition & 1 deletion Sources/trailer/Data/Models/Comment.swift
Original file line number Diff line number Diff line change
Expand Up @@ -124,7 +124,7 @@ struct Comment: Item, Announceable {
log(body.trimmingCharacters(in: .whitespacesAndNewlines), unformatted: true)

let react = reactions
if !react.isEmpty {
if react.hasItems {
let reactionList: [String] = react.compactMap {
if let u = $0.user {
return "[\($0.emoji) @\(u.login)]"
Expand Down
4 changes: 2 additions & 2 deletions Sources/trailer/Data/Models/Issue.swift
Original file line number Diff line number Diff line change
Expand Up @@ -237,7 +237,7 @@ struct Issue: Item, Announceable, Closeable, Sortable {
log()

let react = reactions
if !react.isEmpty {
if react.hasItems {
log("[!Reactions!]")
for r in react {
if let u = r.user {
Expand All @@ -258,7 +258,7 @@ struct Issue: Item, Announceable, Closeable, Sortable {

if CommandLine.argument(exists: "-comments") {
let co = comments.sorted(by: { $0.createdAt < $1.createdAt })
if !co.isEmpty {
if co.hasItems {
for c in co {
c.printDetails()
}
Expand Down
16 changes: 8 additions & 8 deletions Sources/trailer/Data/Models/PullRequest.swift
Original file line number Diff line number Diff line change
Expand Up @@ -239,14 +239,14 @@ struct PullRequest: Item, Announceable, Closeable, Sortable {
let x = components.popLast()! + "!]"
components.append(x)

if listFieldsDefinition.labels, !labels.isEmpty {
if listFieldsDefinition.labels, labels.hasItems {
let l = labels.map { $0.id }.joined(separator: "] [")
components.append("[\(l)]")
}
if listFieldsDefinition.repo, let r = repo {
components.append("(\(r.nameWithOwner))")
}
if listFieldsDefinition.branch, !headRefName.isEmpty {
if listFieldsDefinition.branch, headRefName.hasItems {
components.append("(\(headRefName))")
}
if listFieldsDefinition.author, let a = author {
Expand Down Expand Up @@ -327,7 +327,7 @@ struct PullRequest: Item, Announceable, Closeable, Sortable {
if let r = repo {
log(" [$Repo!] \(r.nameWithOwner)")
}
if !headRefName.isEmpty {
if headRefName.hasItems {
log(" [$Branch!] \(headRefName)")
}
log(" [$URL!] \(url.absoluteString)")
Expand Down Expand Up @@ -366,7 +366,7 @@ struct PullRequest: Item, Announceable, Closeable, Sortable {
log()

let react = reactions
if !react.isEmpty {
if react.hasItems {
log("[!Reactions!]")
for r in react {
if let u = r.user {
Expand All @@ -377,7 +377,7 @@ struct PullRequest: Item, Announceable, Closeable, Sortable {
}

let st = latestStatuses
if !st.isEmpty {
if st.hasItems {
log("[!Statuses")
for s in st {
let char: String
Expand All @@ -395,11 +395,11 @@ struct PullRequest: Item, Announceable, Closeable, Sortable {
}

let latest = latestReviews
if !latest.isEmpty {
if latest.hasItems {
let approvingReviewers = latest.filter { $0.state == .approved }.compactMap { $0.author?.login }.map { "@"+$0 }
let blockingReviewers = latest.filter { $0.state == .changes_requested }.compactMap { $0.author?.login }.map { "@"+$0 }
let pendingReviewers = reviewRequests.compactMap { $0.reviewer?.login }.map { "@"+$0 }.filter { !(approvingReviewers.contains($0) || blockingReviewers.contains($0)) }
if !approvingReviewers.isEmpty || !blockingReviewers.isEmpty || !pendingReviewers.isEmpty {
if approvingReviewers.hasItems || blockingReviewers.hasItems || pendingReviewers.hasItems {
log("[!Reviews")
if approvingReviewers.count > 0 {
log("[G*[+] " + approvingReviewers.joined(separator: ", ") + " approved changes")
Expand Down Expand Up @@ -433,7 +433,7 @@ struct PullRequest: Item, Announceable, Closeable, Sortable {

if CommandLine.argument(exists: "-comments") {
let co = commentItems.sorted(by: { $0.createdAt < $1.createdAt })
if !co.isEmpty {
if co.hasItems {
for c in co {
c.printDetails()
}
Expand Down
13 changes: 0 additions & 13 deletions Sources/trailer/Data/Models/Repo.swift
Original file line number Diff line number Diff line change
Expand Up @@ -215,18 +215,5 @@ struct Repo: Item, Announceable {
Field(name: "id"),
Group(name: "issues", fields: [Field(name: "id")], extraParams: ["states": "OPEN"], usePaging: true),
])

func openURL() {
log("Opening url: [*\(url)*]")
let p = Process()
p.arguments = [url.absoluteString]
#if os(OSX)
p.launchPath = "/usr/bin/open"
p.launch()
#else
p.executableURL = URL(fileURLWithPath: "/usr/bin/open")
try? p.run()
#endif
}
}

4 changes: 2 additions & 2 deletions Sources/trailer/Data/Models/Review.swift
Original file line number Diff line number Diff line change
Expand Up @@ -114,7 +114,7 @@ struct Review: Item, Announceable {
d = "[\(r)] @\(a) reviewed [G*(approving)*]"
case .changes_requested:
d = "[\(r)] @\(a) reviewed [R*(requesting changes)*]"
case .commented where !body.isEmpty:
case .commented where body.hasItems:
d = "[\(r)] @\(a) reviewed"
default:
return
Expand All @@ -138,7 +138,7 @@ struct Review: Item, Announceable {

func printDetails() {
printHeader()
if !body.isEmpty {
if body.hasItems {
log(body.trimmingCharacters(in: .whitespacesAndNewlines), unformatted: true)
log()
}
Expand Down

0 comments on commit 9a33a29

Please sign in to comment.