Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Introducing two new config parameters #13

Closed
wants to merge 3 commits into from
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
15 changes: 15 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -47,6 +47,21 @@ tz name followed by `;` and your alias:

`TZ_LIST="Europe/Paris;EMEA office,US/Central;US office"`

## Name of local zone

tz, by default, displays your local timezone as "Local". If you rather like
to display a different name there instead, you can use the `TZ_LOCAL_NAME`
environment variable to override it.

`TZ_LOCAL_NAME="Cologe office`

## Display time format

In the header line, tz will display the current time. By default the time format
for the time is in 12H format. If you prefer to display the time in 24H format,
you can do so by setting `TZ_24H`

`TZ_24H=1`

# Building

Expand Down
22 changes: 19 additions & 3 deletions config.go
Original file line number Diff line number Diff line change
Expand Up @@ -25,17 +25,33 @@ import (

// Config stores app configuration
type Config struct {
Zones []*Zone
Zones []*Zone
TimeFmt int
}

// LoadConfig from environment
func LoadConfig() (*Config, error) {
conf := Config{
Zones: DefaultZones,
Zones: DefaultZones,
TimeFmt: 12,
}

// Allow 24h time format
if tzTimeFmt := os.Getenv("TZ_24H"); tzTimeFmt != "" {
conf.TimeFmt = 24
}

// "Local" can be overwritten by TZ_LOCAL_NAME
var localIdentifier string
if localName := os.Getenv("TZ_LOCAL_NAME"); localName == "" {
localIdentifier = "Local"
} else {
localIdentifier = localName
}

tzList := os.Getenv("TZ_LIST")
if tzList == "" {
conf.Zones[0].Name = localIdentifier
return &conf, nil
}
tzConfigs := strings.Split(tzList, ",")
Expand All @@ -48,7 +64,7 @@ func LoadConfig() (*Config, error) {
now := time.Now()
localZoneName, offset := now.Zone()
zones[0] = &Zone{
Name: fmt.Sprintf("(%s) Local", localZoneName),
Name: fmt.Sprintf("(%s) %s", localZoneName, localIdentifier),
DbName: localZoneName,
Offset: offset / 3600,
}
Expand Down
2 changes: 2 additions & 0 deletions main.go
Original file line number Diff line number Diff line change
Expand Up @@ -44,6 +44,7 @@ type model struct {
now time.Time
hour int
showDates bool
timeFmt int
}

func (m model) Init() tea.Cmd {
Expand Down Expand Up @@ -96,6 +97,7 @@ func main() {
now: now,
hour: now.Hour(),
showDates: false,
timeFmt: config.TimeFmt,
}
p := tea.NewProgram(initialModel)
if err := p.Start(); err != nil {
Expand Down
2 changes: 1 addition & 1 deletion view.go
Original file line number Diff line number Diff line change
Expand Up @@ -70,7 +70,7 @@ func (m model) View() string {
}
}

zoneHeader := fmt.Sprintf("%s %s %s", zone.ClockEmoji(), normalTextStyle(zone.String()), dateTimeStyle(zone.ShortDT()))
zoneHeader := fmt.Sprintf("%s %s %s", zone.ClockEmoji(), normalTextStyle(zone.String()), dateTimeStyle(zone.ShortDT(m.timeFmt)))

s += fmt.Sprintf(" %s\n %s\n %s\n", zoneHeader, hours.String(), dates.String())
}
Expand Down
8 changes: 6 additions & 2 deletions zone.go
Original file line number Diff line number Diff line change
Expand Up @@ -66,8 +66,12 @@ func (z Zone) ClockEmoji() (clock string) {
}

// ShortDT returns the current time in short format.
func (z Zone) ShortDT() string {
return z.currentTime().Format("3:04PM, Mon 02")
func (z Zone) ShortDT(f int) string {
if f == 24 {
return z.currentTime().Format("15:04, Mon 02")
} else {
return z.currentTime().Format("3:04PM, Mon 02")
}
}

func (z Zone) currentTime() time.Time {
Expand Down