-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Allow "-list" without a filter argument: this lists all the zones we know of, and makes fuzzy searching with programs like fzf possible. When -list *is* followed by another argument, tz will try to match on the zone name, or abbreviated zone name in a case insensitive manner. So far, -list results are sorted alphabetically. I think it could make more sense to use the offset, but YMMV.
- Loading branch information
Showing
2 changed files
with
74 additions
and
29 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,66 @@ | ||
package main | ||
|
||
import ( | ||
"fmt" | ||
"io" | ||
"sort" | ||
"strings" | ||
|
||
"github.com/tkuchiki/go-timezone" | ||
) | ||
|
||
// Zone search results | ||
type ZoneSearchResults map[string]*timezone.TzInfo | ||
|
||
// List of zones sorted alphabetically. | ||
func (zsr ZoneSearchResults) SortedNames() []string { | ||
sorted := make([]string, 0, len(zsr)) | ||
for name := range zsr { | ||
sorted = append(sorted, name) | ||
} | ||
sort.Strings(sorted) | ||
|
||
return sorted | ||
} | ||
|
||
// Print formatted ZoneSearchResults to the chosen Writer ; typically | ||
// os.Stdout. | ||
func (zsr ZoneSearchResults) Print(w io.Writer) { | ||
sorted := zsr.SortedNames() | ||
for i := range sorted { | ||
name := sorted[i] | ||
ti := zsr[name] | ||
fmt.Fprintf(w, "%5s (%s) :: %s\n", | ||
ti.ShortStandard(), | ||
ti.StandardOffsetHHMM(), | ||
name) | ||
} | ||
} | ||
|
||
// Find zones matching a query. An empty query string returns all zones. | ||
func SearchZones(q string) ZoneSearchResults { | ||
// TODO Each call to timezone.New() allocs a fresh list of timezones: | ||
// for now, avoid calling SearchZones too much. | ||
t := timezone.New() | ||
filter := q != "" | ||
matches := map[string]*timezone.TzInfo{} | ||
|
||
for abbr, zones := range t.Timezones() { | ||
for _, name := range zones { | ||
if filter && | ||
!strings.Contains(strings.ToLower(name), q) && | ||
!strings.Contains(strings.ToLower(abbr), q) { | ||
continue | ||
} | ||
|
||
ti, err := t.GetTzInfo(name) | ||
// That should not happen too often. | ||
if err != nil { | ||
panic(err) | ||
} | ||
|
||
matches[name] = ti | ||
} | ||
} | ||
return matches | ||
} |