From 6eda2df111e7da958ebabb26495fd1f2f4bb2e88 Mon Sep 17 00:00:00 2001 From: spinach Date: Wed, 28 Jan 2026 13:26:22 -0500 Subject: [PATCH] using a map of states to codes to shorten display out --- location.go | 27 ++++++++++++++++++++++++ location_test.go | 9 +++++++- rofi.go | 13 +++++++----- us_states.go | 55 ++++++++++++++++++++++++++++++++++++++++++++++++ 4 files changed, 98 insertions(+), 6 deletions(-) create mode 100644 us_states.go diff --git a/location.go b/location.go index 4c648fe..16dc004 100644 --- a/location.go +++ b/location.go @@ -219,8 +219,24 @@ func (l *Location) String() string { } func (l *Location) Label() string { + return l.label(true) +} + +func (l *Location) LabelFull() string { + return l.label(false) +} + +func (l *Location) label(abbrevUS bool) string { city := strings.TrimSpace(l.City) state := strings.TrimSpace(l.State) + country := strings.TrimSpace(l.Country) + + if abbrevUS && (country == "" || strings.EqualFold(country, "United States") || strings.EqualFold(country, "United States of America")) { + if abbr := usStateAbbreviation(state); abbr != "" { + state = abbr + } + } + if city != "" && state != "" { return fmt.Sprintf("%s, %s", city, state) } @@ -314,6 +330,17 @@ func parseJSONFloat(raw json.RawMessage) (float64, error) { return value, nil } +func usStateAbbreviation(state string) string { + state = strings.TrimSpace(state) + if state == "" { + return "" + } + if abbr, ok := usStateMap[state]; ok { + return abbr + } + return "" +} + func hasTTY(in, out *os.File) bool { inStat, err := in.Stat() if err != nil { diff --git a/location_test.go b/location_test.go index 9caf6ae..2d80b99 100644 --- a/location_test.go +++ b/location_test.go @@ -32,7 +32,7 @@ func TestLocationUnmarshalCityFallback(t *testing.T) { } func TestLocationLabel(t *testing.T) { - loc := Location{City: "Somerville", State: "MA"} + loc := Location{City: "Somerville", State: "Massachusetts", Country: "United States"} if got := loc.Label(); got != "Somerville, MA" { t.Fatalf("unexpected label: %q", got) } @@ -43,6 +43,13 @@ func TestLocationLabel(t *testing.T) { } } +func TestUSStateAbbreviationFallback(t *testing.T) { + loc := Location{City: "Toronto", State: "Ontario", Country: "Canada"} + if got := loc.Label(); got != "Toronto, Ontario" { + t.Fatalf("unexpected non-us label: %q", got) + } +} + func TestHasTTYFalse(t *testing.T) { f, err := os.CreateTemp(t.TempDir(), "tty-check-*") if err != nil { diff --git a/rofi.go b/rofi.go index d3604da..3773ebd 100644 --- a/rofi.go +++ b/rofi.go @@ -58,7 +58,7 @@ func interactiveSearch(ctx context.Context, cache *Cache, userAgent string) (Loc options := make([]string, 0, len(recent)+1) for _, loc := range recent { - options = append(options, loc.Label()) + options = append(options, loc.LabelFull()) } selection, ok, err := rofiSelect("Location", options) @@ -74,7 +74,7 @@ func interactiveSearch(ctx context.Context, cache *Cache, userAgent string) (Loc if len(matches) > 0 { choiceOptions := make([]string, 0, len(matches)+1) for _, loc := range matches { - choiceOptions = append(choiceOptions, fmt.Sprintf("Use saved %s", loc.Label())) + choiceOptions = append(choiceOptions, fmt.Sprintf("Use saved %s", loc.LabelFull())) } searchOption := fmt.Sprintf("Search for %s", query) choiceOptions = append(choiceOptions, searchOption) @@ -88,7 +88,10 @@ func interactiveSearch(ctx context.Context, cache *Cache, userAgent string) (Loc } if choice != searchOption { for _, loc := range matches { - if choice == fmt.Sprintf("Use saved %s", loc.Label()) { + if choice == fmt.Sprintf("Use saved %s", loc.LabelFull()) { + if err := cache.SaveLocation(loc.LabelFull(), loc); err != nil { + return Location{}, err + } return loc, nil } } @@ -120,7 +123,7 @@ func SelectLocationRofi(results []Location) (Location, error) { options := make([]string, 0, len(results)) for _, loc := range results { - label := loc.Label() + label := loc.LabelFull() if loc.Country != "" && !strings.Contains(label, loc.Country) { label = fmt.Sprintf("%s, %s", label, loc.Country) } @@ -151,7 +154,7 @@ func matchRecentLocations(recent []Location, query string) []Location { } matches := make([]Location, 0, len(recent)) for _, loc := range recent { - label := strings.ToLower(loc.Label()) + label := strings.ToLower(loc.LabelFull()) if strings.Contains(label, query) { matches = append(matches, loc) } diff --git a/us_states.go b/us_states.go new file mode 100644 index 0000000..bd2b025 --- /dev/null +++ b/us_states.go @@ -0,0 +1,55 @@ +package main + +var usStateMap = map[string]string{ + "Alabama": "AL", + "Alaska": "AK", + "Arizona": "AZ", + "Arkansas": "AR", + "California": "CA", + "Colorado": "CO", + "Connecticut": "CT", + "Delaware": "DE", + "Florida": "FL", + "Georgia": "GA", + "Hawaii": "HI", + "Idaho": "ID", + "Illinois": "IL", + "Indiana": "IN", + "Iowa": "IA", + "Kansas": "KS", + "Kentucky": "KY", + "Louisiana": "LA", + "Maine": "ME", + "Maryland": "MD", + "Massachusetts": "MA", + "Michigan": "MI", + "Minnesota": "MN", + "Mississippi": "MS", + "Missouri": "MO", + "Montana": "MT", + "Nebraska": "NE", + "Nevada": "NV", + "New Hampshire": "NH", + "New Jersey": "NJ", + "New Mexico": "NM", + "New York": "NY", + "North Carolina": "NC", + "North Dakota": "ND", + "Ohio": "OH", + "Oklahoma": "OK", + "Oregon": "OR", + "Pennsylvania": "PA", + "Rhode Island": "RI", + "South Carolina": "SC", + "South Dakota": "SD", + "Tennessee": "TN", + "Texas": "TX", + "Utah": "UT", + "Vermont": "VT", + "Virginia": "VA", + "Washington": "WA", + "West Virginia": "WV", + "Wisconsin": "WI", + "Wyoming": "WY", + "District of Columbia": "DC", +}