Formatted code

Brought over and applied Swift Argument Parser's code formatting script
This commit is contained in:
Tony Bark 2026-09-01 13:29:09 -04:00
parent dc464de1c4
commit c699007cbf
9 changed files with 593 additions and 563 deletions

View file

@ -9,7 +9,7 @@ let package = Package(
.package(
url: "https://github.com/moreSwift/swift-cross-ui",
.upToNextMinor(from: "0.7.0"),
),
)
],
targets: [
.executableTarget(
@ -18,6 +18,6 @@ let package = Package(
.product(name: "SwiftCrossUI", package: "swift-cross-ui"),
.product(name: "DefaultBackend", package: "swift-cross-ui"),
],
),
)
],
)

View file

@ -7,7 +7,6 @@ enum AmtrakError: Error {
case stationNotFound(String)
}
struct TrainStationLookUp: Sendable {
var trainNumbers: [String]
var timeZone: TimeZone?
@ -52,7 +51,6 @@ struct AmtrakClient {
.map(String.init) ?? trainId
}
func trainStationLookup(stationCode code: String) async throws -> TrainStationLookUp {
let reponse = try await fetchStation(code: code)
@ -78,7 +76,7 @@ struct AmtrakClient {
throw AmtrakError.invalidResponse
}
guard (200 ..< 300).contains(http.statusCode) else {
guard (200..<300).contains(http.statusCode) else {
throw AmtrakError.httpStatus(http.statusCode)
}

View file

@ -18,17 +18,23 @@ struct AmtrakDateFormatting {
}()
func parsedDate(from isoString: String?) -> Date? {
guard let isoString, !isoString.isEmpty, let date = isoFormatter.date(
from: isoString) else {
guard let isoString, !isoString.isEmpty,
let date = isoFormatter.date(
from: isoString)
else {
return nil
}
return isoFormatter.date(from: isoString)
}
func time(from isoString: String?, in zone: TimeZone?, comparedTo originZone: TimeZone? = nil) -> String? {
guard let isoString, !isoString.isEmpty, let date = isoFormatter.date(
from: isoString) else {
func time(from isoString: String?, in zone: TimeZone?, comparedTo originZone: TimeZone? = nil)
-> String?
{
guard let isoString, !isoString.isEmpty,
let date = isoFormatter.date(
from: isoString)
else {
return nil
}
@ -48,7 +54,6 @@ struct AmtrakDateFormatting {
return "\(formatted) \(abbreviation)"
}
func isOnTime(actual: String?, scheduled: String?) -> LateTrain {
guard
@ -70,7 +75,6 @@ struct AmtrakDateFormatting {
}
}
func localTime(timeZone: TimeZone?) -> String {
let now = Date()
timeFormatter.timeZone = timeZone
@ -83,8 +87,10 @@ struct AmtrakDateFormatting {
}
func date(from isoString: String?) -> String? {
guard let isoString, !isoString.isEmpty, let date = isoFormatter.date(
from: isoString) else {
guard let isoString, !isoString.isEmpty,
let date = isoFormatter.date(
from: isoString)
else {
return nil
}

View file

@ -4,7 +4,7 @@ import SwiftCrossUI
@main
struct AmtrakStatusApp: App {
var body: some Scene {
WindowGroup("Amtrak Status") {
WindowGroup("AMTK Status") {
ContentView()
}
}

View file

@ -1,6 +1,5 @@
import SwiftCrossUI
import Foundation
import SwiftCrossUI
struct ContentView: View {
@State var client = AmtrakClient()
@ -18,18 +17,6 @@ struct ContentView: View {
var body: some View {
VStack {
HStack {
Text("Station Code:")
TextField("e.g. ASD", text: $stationCode)
.frame(width: 100)
Button("Submit") {
Task {
await loadTrainStatus(forStationCode: normalizedStationCode)
}
}.disabled(normalizedStationCode.isEmpty)
}.padding(10)
// Most popular Virtual Railfan stations
HStack {
Button("Ashland, VA") {
@ -49,7 +36,7 @@ struct ContentView: View {
await loadTrainStatus(forStationCode: "LAP")
}
}
}
}.padding(10)
Table(rows) {
TableColumn("Time", value: \TrainStatusRow.time)
@ -82,7 +69,7 @@ struct ContentView: View {
HStack {
if let errorMessage {
Text(errorMessage)
} else if !currentStation.isEmpty {
} else if !currentStation.isEmpty && !isLoading {
Text(
"It is \(amtrakDate.localTime(timeZone: localTimeZone)) at \(currentStation) station"
)
@ -95,7 +82,23 @@ struct ContentView: View {
await loadTrainStatus(forStationCode: normalizedStationCode)
}
}.overlay(alignment: .topTrailing) {
HStack {
Text("Station Code:")
TextField("e.g. ASD", text: $stationCode)
.frame(width: 100)
Button("Submit") {
Task {
await loadTrainStatus(forStationCode: normalizedStationCode)
}
}.disabled(normalizedStationCode.isEmpty)
}.frame(alignment: .topTrailing)
.padding(10)
}
.overlay(alignment: .topLeading) {
Text("Information by Amtraker")
.frame(alignment: .topLeading)
.padding(10)
}
}
@ -148,7 +151,8 @@ struct ContentView: View {
let trains = try await client.fetchAllTrainStatus(num: number)
for train in trains {
let platform = train.stations?.first(
where: { $0.code == code
where: {
$0.code == code
},
)?.platform
let row = await trainStatus(
@ -201,11 +205,13 @@ struct ContentView: View {
)
}
let time = amtrakDate.time(
let time =
amtrakDate.time(
from: leg?.arr,
in: timeZone,
comparedTo: originZone
) ?? amtrakDate.time(
)
?? amtrakDate.time(
from: leg?.schArr,
in: timeZone,
comparedTo: originZone
@ -245,7 +251,8 @@ struct ContentView: View {
var calendar = Calendar.current
calendar.timeZone = timeZone ?? .current
return calendar
return
calendar
.compare(
deparatureDate,
to: Date(),
@ -266,12 +273,14 @@ struct ContentView: View {
return [:]
}
return Dictionary(
staleData.lastUpdatedArr.map { ( $0.trainID, $0.timeSince) },
staleData.lastUpdatedArr.map { ($0.trainID, $0.timeSince) },
uniquingKeysWith: { first, _ in first }
)
}
func fetchTimeZone(forStationCode code: String, cache: inout [String: TimeZone?]) async -> TimeZone? {
func fetchTimeZone(forStationCode code: String, cache: inout [String: TimeZone?]) async
-> TimeZone?
{
if let cached = cache[code] {
return cached
}

View file

@ -11,9 +11,9 @@ enum LateTrain {
switch self {
case .onTime:
return "On Time"
case .early(minutes: let minutes):
case .early(let minutes):
return "\(Self.formatted(minutes)) early"
case .late(minutes: let minutes):
case .late(let minutes):
return "\(Self.formatted(minutes)) late"
case .unknown:
return ""

17
format.sh Executable file
View file

@ -0,0 +1,17 @@
#!/bin/bash
##===----------------------------------------------------------------------===##
##
## This source file is from the Swift Argument Parser open source project
##
## Copyright (c) 2025 Apple Inc. and the Swift project authors
## Licensed under Apache License v2.0 with Runtime Library Exception
##
## See https://swift.org/LICENSE.txt for license information
##
##===----------------------------------------------------------------------===##
echo "Formatting Swift sources in $(pwd)"
# Run the format / lint commands
git ls-files -z '*.swift' | xargs -0 swift format format --parallel --in-place
git ls-files -z '*.swift' | xargs -0 swift format lint --strict --parallel