Browse Source

Add times to statuses in list command

pull/3/head
alufers 2 years ago
parent
commit
459b91e5ea
No known key found for this signature in database
GPG Key ID: 9176AE047525A423
  1. 2
      README.md
  2. 13
      list_parcels_cmd.go
  3. 8
      parcel_info_cmd.go
  4. 36
      util.go

2
README.md

@ -17,6 +17,8 @@ This project was developed solely by me and is not affiliated with InPost in any
## Installation
Prebuilt binaries are available for download [at the releases page](https://github.com/alufers/inpost-cli/releases/latest). Linux (arm, arm64, and x64), macOS and windows are supported.
If you have Go installed on your system, you can compile and install `inpost-cli` this way:
```bash

13
list_parcels_cmd.go

@ -4,7 +4,6 @@ import (
"encoding/json"
"fmt"
"os"
"regexp"
"time"
"github.com/alufers/inpost-cli/swagger"
@ -68,8 +67,14 @@ var ListParcelsCmd = &cli.Command{
}
expiryTime := ""
if !v.ExpiryDate.IsZero() {
expiryTime = v.ExpiryDate.Sub(time.Now()).String()
expiryTime = regexp.MustCompile("[0-9\\.]*s$").ReplaceAllString(expiryTime, "")
expiryTime = formatDuration(v.ExpiryDate.Sub(time.Now()))
}
timeSinceStatus := ""
for _, s := range v.StatusHistory {
if s.Status == v.Status {
timeSinceStatus = formatDuration(time.Now().Sub(s.Date)) + " ago"
break
}
}
var colors []tablewriter.Colors
if v.Status == "delivered" {
@ -98,7 +103,7 @@ var ListParcelsCmd = &cli.Command{
colors = append(colors, tablewriter.Colors{tablewriter.FgHiYellowColor})
}
}
table.Rich([]string{v.ShipmentNumber, v.SenderName, v.Status, pickupPoint, pickupCity, v.OpenCode, expiryTime}, colors)
table.Rich([]string{v.ShipmentNumber, v.SenderName, v.Status + prependSpaceIfNotEmpty(timeSinceStatus), pickupPoint, pickupCity, v.OpenCode, expiryTime}, colors)
}
table.Render()
case "json":

8
parcel_info_cmd.go

@ -4,7 +4,6 @@ import (
"bytes"
"encoding/json"
"fmt"
"regexp"
"strings"
"time"
@ -85,10 +84,7 @@ var ParcelInfoCmd = &cli.Command{
fmt.Fprintf(infoBuf, "Open code: %v\n", p.OpenCode)
}
if !p.ExpiryDate.IsZero() {
expiryTime := ""
expiryTime = p.ExpiryDate.Sub(time.Now()).String()
expiryTime = regexp.MustCompile("[0-9\\.]*s$").ReplaceAllString(expiryTime, "")
fmt.Fprintf(infoBuf, "Expiry time: %v\n", expiryTime)
fmt.Fprintf(infoBuf, "Expiry time: %v\n", formatDuration(p.ExpiryDate.Sub(time.Now())))
}
if p.PickupPoint != nil {
fmt.Fprintf(infoBuf, "Pickup point:\n")
@ -102,7 +98,7 @@ var ParcelInfoCmd = &cli.Command{
p.PickupPoint.AddressDetails.City,
p.PickupPoint.AddressDetails.Province,
)
fmt.Fprintf(infoBuf, " Location description:%v\n", p.PickupPoint.LocationDescription)
fmt.Fprintf(infoBuf, " Location description: %v\n", p.PickupPoint.LocationDescription)
}
}
if p.OpenCode != "" {

36
util.go

@ -3,8 +3,11 @@ package main
import (
"context"
"fmt"
"math"
"strings"
"time"
"github.com/alufers/inpost-cli/swagger"
"github.com/antihax/optional"
)
@ -53,3 +56,36 @@ func resolveShipmentNumber(ctx context.Context, apiClient *swagger.APIClient, pa
}
return matchingPackages[0].ShipmentNumber, nil
}
// formatDuration formats a duration without the seconds
func formatDuration(d time.Duration) string {
var format string
seconds := math.Floor(d.Seconds())
days := math.Floor(seconds / (60 * 60 * 24))
if days > 0 {
format += fmt.Sprintf("%vd ", days)
seconds -= days * 60 * 60 * 24
}
hours := math.Floor(seconds / (60 * 60))
if hours > 0 {
format += fmt.Sprintf("%vh ", hours)
seconds -= hours * 60 * 60
}
minutes := math.Floor(seconds / 60)
if minutes > 0 {
format += fmt.Sprintf("%vm ", minutes)
seconds -= minutes * 60
}
if format == "" {
format += fmt.Sprintf("%vs ", seconds)
}
return strings.TrimSpace(format)
}

Loading…
Cancel
Save