Skip to content

Commit

Permalink
Merge pull request #7 from navidys/arrival_tableview
Browse files Browse the repository at this point in the history
 cmd - table view for arrivals by airport update
  • Loading branch information
navidys authored Nov 9, 2023
2 parents 6fc58b0 + 41ff679 commit aaf32d8
Show file tree
Hide file tree
Showing 2 changed files with 46 additions and 28 deletions.
62 changes: 38 additions & 24 deletions cmd/gopensky/arrivals.go
Original file line number Diff line number Diff line change
Expand Up @@ -47,33 +47,41 @@ func runArrivals(cmd *cobra.Command, args []string) {

fmt.Printf("%s\n", jsonResult) //nolint:forbidigo
} else {
printArrivalsTemplate(flights)
printArrivalsTable(flights)
}
}

func printArrivalsTemplate(flights []gopensky.FlighData) {
func printArrivalsTable(flights []gopensky.FlighData) { //nolint:funlen
writer := tabwriter.NewWriter(os.Stdout, 0, 0, 1, ' ', 0)

header := fmt.Sprintf("\n%s\t%s\t%s\t%s\t%s\t%s\t%s\t%s\t%s\t%s\t%s\t%s",
"Icao24",
"FirstSeen",
"LastSeen",
"EstDepartureAirp",
"EstArrivalAirp",
"Callsign",
"EstDepAirpHorizDist",
"EstDepAirpVertDist",
"EstArrAirpHorizDist",
"EstArrAirpVertDist",
"DepAirportCandCount",
"ArrAirportCandCount",
)

fmt.Fprintln(writer, header)
info := []string{
"", // just to print a new line
"EDA = EstDepartureAirport",
"EAA = EstArrivalAirport",
"EDAHD = EstDepartureAirportHorizDistance",
"EDAVD = EstDepartureAirportVertDistance",
"EAAHD = EstArrivalAirportHorizDistance",
"EAAVD = EstArrivalAirportVertDistance",
"DACC = DepartureAirportCandidatesCount",
"AACC = ArrivalAirportCandidatesCount",
"", // just to print a new line
}

if _, err := fmt.Fprintln(os.Stdout, strings.Join(info, "\n")); err != nil {
log.Error().Msgf("%v", err)
}

header := fmt.Sprintf("%s\t%s\t%s\t%8s\t%s\t%s\t%s\t%s\t%s\t%s\t%s\t%s",
"Icao24", "EDA", "EAA", "Callsign", "EDAHD", "EDAVD",
"EAAHD", "EAAVD", "DACC", "AACC", "FirstSeen", "LastSeen")

if _, err := fmt.Fprintln(writer, header); err != nil {
log.Error().Msgf("%v", err)
}

for _, flightData := range flights {
firstSeen := time.Unix(flightData.FirstSeen, 0).Format("2023-10-09 09:52:38")
lastSeen := time.Unix(flightData.LastSeen, 0).Format("2023-10-09 09:52:38")
firstSeen := time.Unix(flightData.FirstSeen, 0).UTC()
lastSeen := time.Unix(flightData.LastSeen, 0).UTC()
estDepartureAirport := ""
estArrivalAirport := ""
callsign := ""
Expand All @@ -90,10 +98,8 @@ func printArrivalsTemplate(flights []gopensky.FlighData) {
estDepartureAirport = *flightData.Callsign
}

data := fmt.Sprintf("\n%s\t%s\t%s\t%s\t%s\t%s\t%d\t%d\t%d\t%d\t%d\t%d",
data := fmt.Sprintf("%s\t%s\t%s\t%s\t%d\t%d\t%d\t%d\t%d\t%d\t%s\t%s",
flightData.Icao24,
firstSeen,
lastSeen,
estDepartureAirport,
estArrivalAirport,
callsign,
Expand All @@ -103,9 +109,17 @@ func printArrivalsTemplate(flights []gopensky.FlighData) {
flightData.EstArrivalAirportVertDistance,
flightData.DepartureAirportCandidatesCount,
flightData.ArrivalAirportCandidatesCount,
firstSeen,
lastSeen,
)

fmt.Fprintln(writer, data)
if _, err := fmt.Fprintln(writer, data); err != nil {
log.Error().Msgf("%v", err)
}
}

if err := writer.Flush(); err != nil {
log.Error().Msgf("failed to flush template: %v", err)
}
}

Expand Down
12 changes: 8 additions & 4 deletions cmd/gopensky/states.go
Original file line number Diff line number Diff line change
Expand Up @@ -64,11 +64,11 @@ func runStates(cmd *cobra.Command, args []string) {

fmt.Printf("%s\n", jsonResult) //nolint:forbidigo
} else {
printStatesTemplate(states)
printStatesTable(states)
}
}

func printStatesTemplate(states *gopensky.States) { //nolint:funlen
func printStatesTable(states *gopensky.States) { //nolint:funlen
writer := tabwriter.NewWriter(os.Stdout, 0, 0, 1, ' ', 0)

header := fmt.Sprintf("\n%s\t%s\t%s\t%s\t%s\t%s\t%s\t%s\t%s\t%s\t%s\t%s\t%s\t%s\t%s\t%s\t%s\t%s",
Expand All @@ -92,7 +92,9 @@ func printStatesTemplate(states *gopensky.States) { //nolint:funlen
"Category",
)

fmt.Fprintln(writer, header)
if _, err := fmt.Fprintln(writer, header); err != nil {
log.Error().Msgf("%v", err)
}

floatToString := func(data *float64) string {
if data != nil {
Expand Down Expand Up @@ -155,7 +157,9 @@ func printStatesTemplate(states *gopensky.States) { //nolint:funlen
state.Category,
)

fmt.Fprintln(writer, data)
if _, err := fmt.Fprintln(writer, data); err != nil {
log.Error().Msgf("%v", err)
}
}

if err := writer.Flush(); err != nil {
Expand Down

0 comments on commit aaf32d8

Please sign in to comment.