Skip to content

Commit

Permalink
feat: wish app set TZ
Browse files Browse the repository at this point in the history
Signed-off-by: Carlos A Becker <caarlos0@users.noreply.github.com>
  • Loading branch information
caarlos0 committed May 22, 2022
1 parent b687c66 commit a907c3f
Show file tree
Hide file tree
Showing 3 changed files with 35 additions and 9 deletions.
6 changes: 5 additions & 1 deletion cmd/uhr/main.go
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,11 @@ import (
)

func main() {
p := tea.NewProgram(ui.New())
m, err := ui.New("Local")
if err != nil {
log.Fatal(err)
}
p := tea.NewProgram(m)
if err := p.Start(); err != nil {
log.Fatal(err)
}
Expand Down
17 changes: 16 additions & 1 deletion cmd/wisuhr/main.go
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@ import (
"log"
"os"
"os/signal"
"strings"
"syscall"
"time"

Expand Down Expand Up @@ -33,7 +34,11 @@ func main() {
wish.WithHostKeyPath(".ssh/term_info_ed25519"),
wish.WithMiddleware(
bubbletea.Middleware(func(s ssh.Session) (tea.Model, []tea.ProgramOption) {
return ui.New(), []tea.ProgramOption{tea.WithAltScreen()}
m, err := ui.New(tz(s.Environ()))
if err != nil {
wish.Fatal(s, err)
}
return m, []tea.ProgramOption{tea.WithAltScreen()}
}),
logging.Middleware(),
),
Expand All @@ -59,3 +64,13 @@ func main() {
log.Fatalln(err)
}
}

func tz(env []string) string {
for _, e := range env {
k, v, ok := strings.Cut(e, "=")
if ok && k == "TZ" {
return v
}
}
return ""
}
21 changes: 14 additions & 7 deletions pkg/ui/model.go
Original file line number Diff line number Diff line change
Expand Up @@ -8,14 +8,20 @@ import (
"github.com/charmbracelet/lipgloss"
)

func New() tea.Model {
return model{
t: time.Now(),
func New(tz string) (tea.Model, error) {
loc, err := time.LoadLocation(tz)
if err != nil {
return nil, err
}
return model{
t: time.Now(),
tz: loc,
}, err
}

type model struct {
t time.Time
t time.Time
tz *time.Location
}

func (m model) Init() tea.Cmd {
Expand Down Expand Up @@ -49,13 +55,14 @@ var (
)

func (m model) View() string {
t := m.t.In(m.tz)
s := header.Render("Hallo!") + "\n\n"
s += "Heute ist " + italic.Render(uhr.Weekday(m.t)) + ".\n"
s += "Es ist jetzt " + italic.Render(m.t.Format("15.04")) + ", aber du kannst auch sagen:\n"
for _, l := range uhr.Uhr(m.t) {
s += "Es ist jetzt " + italic.Render(t.Format("15.04")) + ", aber du kannst auch sagen:\n"
for _, l := range uhr.Uhr(t) {
s += list.Render("- ") + italic.Render(l) + "\n"
}
s += "Es ist " + italic.Render(uhr.PartOfDay(m.t)) + ".\n"
s += "Es ist " + italic.Render(uhr.PartOfDay(t)) + ".\n"

s += footer.Render("\ndrücke 'q' zum Beenden")
return s
Expand Down

0 comments on commit a907c3f

Please sign in to comment.