Skip to content

Commit 3797d8c

Browse files
author
Stanislav (Stas) Katkov
committed
feat: Adding a Cron Job Parser to the list:
1 parent 5b11885 commit 3797d8c

File tree

2 files changed

+79
-23
lines changed

2 files changed

+79
-23
lines changed

tui/cron/main.go

Lines changed: 74 additions & 23 deletions
Original file line numberDiff line numberDiff line change
@@ -1,26 +1,37 @@
1-
package main
1+
package cron
22

33
import (
44
"fmt"
5+
"os"
56
"regexp"
7+
"strconv"
68

79
"github.com/charmbracelet/huh"
10+
"github.com/charmbracelet/lipgloss"
811
"github.com/lnquy/cron"
12+
13+
tea "github.com/charmbracelet/bubbletea"
914
)
1015

11-
func main() {
12-
var cronExpression string
16+
type CronModel struct {
17+
form *huh.Form
18+
cronExpression string
19+
}
20+
21+
func NewCronModel() *CronModel {
22+
m := &CronModel{}
23+
accessible, _ := strconv.ParseBool(os.Getenv("ACCESSIBLE"))
1324

1425
// @see https://gist.github.com/Aterfax/401875eb3d45c9c114bbef69364dd045
1526
// @see https://regexr.com/4jp54
1627
cronRegex := `^((((\d+,)+\d+|(\d+(\/|-|#)\d+)|\d+L?|\*(\/\d+)?|L(-\d+)?|\?|[A-Z]{3}(-[A-Z]{3})?) ?){5,7})|(@(annually|yearly|monthly|weekly|daily|hourly|reboot))|(@every (\d+(ns|us|µs|ms|s|m|h))+)$`
1728

18-
form := huh.NewForm(
29+
m.form = huh.NewForm(
1930
huh.NewGroup(
2031
huh.NewInput().
2132
Title("Cron Expression").
2233
Placeholder("* * * * *").
23-
Value(&cronExpression).
34+
Value(&m.cronExpression).
2435
Validate(func(str string) error {
2536
// First validate with regexp
2637
matched, err := regexp.MatchString(cronRegex, str)
@@ -43,27 +54,67 @@ func main() {
4354
return nil
4455
}),
4556
),
46-
)
57+
).WithTheme(huh.ThemeCharm()).WithAccessible(accessible)
4758

48-
err := form.Run()
49-
if err != nil {
50-
fmt.Printf("Error running form: %v\n", err)
51-
return
52-
}
59+
return m
60+
}
61+
62+
func (m *CronModel) Init() tea.Cmd {
63+
return m.form.Init()
64+
}
5365

54-
expr, err := cron.NewDescriptor(
55-
cron.Use24HourTimeFormat(true),
56-
cron.DayOfWeekStartsAtOne(true),
57-
)
58-
if err != nil {
59-
fmt.Printf("Error parsing cron expression: %v\n", err)
60-
return
66+
func (m *CronModel) Update(msg tea.Msg) (tea.Model, tea.Cmd) {
67+
switch msg := msg.(type) {
68+
case tea.KeyMsg:
69+
switch msg.String() {
70+
case "ctrl+c":
71+
return m, tea.Interrupt
72+
case "esc", "q":
73+
return m, tea.Quit
74+
}
6175
}
62-
desc, err := expr.ToDescription(cronExpression, cron.Locale_en)
63-
if err != nil {
64-
fmt.Printf("Error parsing cron expression: %v\n", err)
65-
return
76+
77+
form, cmd := m.form.Update(msg)
78+
if f, ok := form.(*huh.Form); ok {
79+
m.form = f
6680
}
81+
return m, cmd
82+
}
83+
84+
func (m *CronModel) View() string {
85+
switch m.form.State {
86+
case huh.StateCompleted:
87+
expr, err := cron.NewDescriptor(
88+
cron.Use24HourTimeFormat(true),
89+
cron.DayOfWeekStartsAtOne(true),
90+
)
91+
if err != nil {
92+
return lipgloss.NewStyle().Padding(2).
93+
Render(fmt.Sprintf("Error parsing cron expression: %v", err))
94+
}
95+
96+
desc, err := expr.ToDescription(m.cronExpression, cron.Locale_en)
97+
if err != nil {
98+
return lipgloss.NewStyle().Padding(2).
99+
Render(fmt.Sprintf("Error parsing cron expression: %v", err))
100+
}
101+
titleStyle := lipgloss.NewStyle().
102+
Foreground(lipgloss.Color("#FF69B4")).
103+
Bold(true)
67104

68-
fmt.Printf("%s => %s\n", cronExpression, desc)
105+
valueStyle := lipgloss.NewStyle().
106+
Foreground(lipgloss.Color("#87CEEB"))
107+
108+
output := fmt.Sprintf("%s \n\n",
109+
titleStyle.Render(m.cronExpression)) +
110+
fmt.Sprintf("%s",
111+
valueStyle.Render(desc))
112+
113+
return lipgloss.NewStyle().
114+
Padding(2).
115+
PaddingTop(1).
116+
Render(output)
117+
default:
118+
return lipgloss.NewStyle().Padding(2).Render(m.form.View())
119+
}
69120
}

tui/root/list.go

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -8,6 +8,7 @@ import (
88
"github.com/charmbracelet/bubbles/list"
99
tea "github.com/charmbracelet/bubbletea"
1010
"github.com/charmbracelet/lipgloss"
11+
cron "github.com/skatkov/devtui/tui/cron"
1112
"github.com/skatkov/devtui/tui/numbers"
1213
uuiddecode "github.com/skatkov/devtui/tui/uuid-decode"
1314
uuidgenerate "github.com/skatkov/devtui/tui/uuid-generate"
@@ -68,6 +69,10 @@ func newListModel() *listModel {
6869
title: "UUID Generate",
6970
model: func() tea.Model { return uuidgenerate.NewUUIDGenerateModel() },
7071
},
72+
MenuOption{
73+
title: "Cron Job Parser",
74+
model: func() tea.Model { return cron.NewCronModel() },
75+
},
7176
}
7277

7378
delegate := itemDelegate{}

0 commit comments

Comments
 (0)