using a map of states to codes to shorten display out
This commit is contained in:
parent
61d7127d51
commit
6eda2df111
27
location.go
27
location.go
@ -219,8 +219,24 @@ func (l *Location) String() string {
|
|||||||
}
|
}
|
||||||
|
|
||||||
func (l *Location) Label() 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)
|
city := strings.TrimSpace(l.City)
|
||||||
state := strings.TrimSpace(l.State)
|
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 != "" {
|
if city != "" && state != "" {
|
||||||
return fmt.Sprintf("%s, %s", city, state)
|
return fmt.Sprintf("%s, %s", city, state)
|
||||||
}
|
}
|
||||||
@ -314,6 +330,17 @@ func parseJSONFloat(raw json.RawMessage) (float64, error) {
|
|||||||
return value, nil
|
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 {
|
func hasTTY(in, out *os.File) bool {
|
||||||
inStat, err := in.Stat()
|
inStat, err := in.Stat()
|
||||||
if err != nil {
|
if err != nil {
|
||||||
|
|||||||
@ -32,7 +32,7 @@ func TestLocationUnmarshalCityFallback(t *testing.T) {
|
|||||||
}
|
}
|
||||||
|
|
||||||
func TestLocationLabel(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" {
|
if got := loc.Label(); got != "Somerville, MA" {
|
||||||
t.Fatalf("unexpected label: %q", got)
|
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) {
|
func TestHasTTYFalse(t *testing.T) {
|
||||||
f, err := os.CreateTemp(t.TempDir(), "tty-check-*")
|
f, err := os.CreateTemp(t.TempDir(), "tty-check-*")
|
||||||
if err != nil {
|
if err != nil {
|
||||||
|
|||||||
13
rofi.go
13
rofi.go
@ -58,7 +58,7 @@ func interactiveSearch(ctx context.Context, cache *Cache, userAgent string) (Loc
|
|||||||
|
|
||||||
options := make([]string, 0, len(recent)+1)
|
options := make([]string, 0, len(recent)+1)
|
||||||
for _, loc := range recent {
|
for _, loc := range recent {
|
||||||
options = append(options, loc.Label())
|
options = append(options, loc.LabelFull())
|
||||||
}
|
}
|
||||||
|
|
||||||
selection, ok, err := rofiSelect("Location", options)
|
selection, ok, err := rofiSelect("Location", options)
|
||||||
@ -74,7 +74,7 @@ func interactiveSearch(ctx context.Context, cache *Cache, userAgent string) (Loc
|
|||||||
if len(matches) > 0 {
|
if len(matches) > 0 {
|
||||||
choiceOptions := make([]string, 0, len(matches)+1)
|
choiceOptions := make([]string, 0, len(matches)+1)
|
||||||
for _, loc := range matches {
|
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)
|
searchOption := fmt.Sprintf("Search for %s", query)
|
||||||
choiceOptions = append(choiceOptions, searchOption)
|
choiceOptions = append(choiceOptions, searchOption)
|
||||||
@ -88,7 +88,10 @@ func interactiveSearch(ctx context.Context, cache *Cache, userAgent string) (Loc
|
|||||||
}
|
}
|
||||||
if choice != searchOption {
|
if choice != searchOption {
|
||||||
for _, loc := range matches {
|
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
|
return loc, nil
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
@ -120,7 +123,7 @@ func SelectLocationRofi(results []Location) (Location, error) {
|
|||||||
|
|
||||||
options := make([]string, 0, len(results))
|
options := make([]string, 0, len(results))
|
||||||
for _, loc := range results {
|
for _, loc := range results {
|
||||||
label := loc.Label()
|
label := loc.LabelFull()
|
||||||
if loc.Country != "" && !strings.Contains(label, loc.Country) {
|
if loc.Country != "" && !strings.Contains(label, loc.Country) {
|
||||||
label = fmt.Sprintf("%s, %s", 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))
|
matches := make([]Location, 0, len(recent))
|
||||||
for _, loc := range recent {
|
for _, loc := range recent {
|
||||||
label := strings.ToLower(loc.Label())
|
label := strings.ToLower(loc.LabelFull())
|
||||||
if strings.Contains(label, query) {
|
if strings.Contains(label, query) {
|
||||||
matches = append(matches, loc)
|
matches = append(matches, loc)
|
||||||
}
|
}
|
||||||
|
|||||||
55
us_states.go
Normal file
55
us_states.go
Normal file
@ -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",
|
||||||
|
}
|
||||||
Loading…
x
Reference in New Issue
Block a user