Skip to content

Commit 1f05491

Browse files
committed
refactor(ui): refactor task ui code
refactor task ui code Signed-off-by: mritd <[email protected]>
1 parent fe60343 commit 1f05491

File tree

5 files changed

+263
-166
lines changed

5 files changed

+263
-166
lines changed

install.go

Lines changed: 115 additions & 119 deletions
Original file line numberDiff line numberDiff line change
@@ -7,6 +7,8 @@ import (
77
"os/exec"
88
"path/filepath"
99

10+
"github.com/mritd/gitflow-toolkit/v2/ui"
11+
1012
tea "github.com/charmbracelet/bubbletea"
1113
"github.com/mitchellh/go-homedir"
1214
)
@@ -22,102 +24,99 @@ func install(dir string) error {
2224
toolKitHooks := filepath.Join(toolKitHome, "hooks")
2325
links := linkPath(dir)
2426

25-
m := stageModel{
26-
stages: []stage{
27-
{
28-
title: "Clean install dir...",
29-
f: func() error { return os.RemoveAll(toolKitHome) },
30-
},
31-
{
32-
title: "Clean symlinks...",
33-
f: func() error {
34-
for _, link := range links {
35-
if _, err := os.Lstat(link); err == nil {
36-
err := os.RemoveAll(link)
37-
if err != nil {
38-
return fmt.Errorf("💔 failed to remove symlink: %s: %s", link, err)
39-
}
40-
} else if !os.IsNotExist(err) {
41-
return fmt.Errorf("💔 failed to get symlink info: %s: %s", link, err)
27+
m := ui.NewMultiTaskModelWithTasks([]ui.Task{
28+
{
29+
Title: "Clean install dir...",
30+
Func: func() error { return os.RemoveAll(toolKitHome) },
31+
},
32+
{
33+
Title: "Clean symlinks...",
34+
Func: func() error {
35+
for _, link := range links {
36+
if _, err := os.Lstat(link); err == nil {
37+
err := os.RemoveAll(link)
38+
if err != nil {
39+
return fmt.Errorf("💔 failed to remove symlink: %s: %s", link, err)
4240
}
41+
} else if !os.IsNotExist(err) {
42+
return fmt.Errorf("💔 failed to get symlink info: %s: %s", link, err)
4343
}
44-
return nil
45-
},
44+
}
45+
return nil
4646
},
47-
{
48-
title: "Unset commit hooks...",
49-
f: func() error {
50-
_, _ = git("config", "--global", "--unset", "core.hooksPath")
51-
return nil
52-
},
47+
},
48+
{
49+
Title: "Unset commit hooks...",
50+
Func: func() error {
51+
_, _ = git("config", "--global", "--unset", "core.hooksPath")
52+
return nil
5353
},
54-
{
55-
title: "Create toolkit home...",
56-
f: func() error {
57-
return os.MkdirAll(toolKitHome, 0755)
58-
},
54+
},
55+
{
56+
Title: "Create toolkit home...",
57+
Func: func() error {
58+
return os.MkdirAll(toolKitHome, 0755)
5959
},
60-
{
61-
title: "Install executable file...",
62-
f: func() error {
63-
binPath, err := exec.LookPath(os.Args[0])
64-
if err != nil {
65-
return fmt.Errorf("💔 failed to get bin file info: %s: %s", os.Args[0], err)
66-
}
60+
},
61+
{
62+
Title: "Install executable file...",
63+
Func: func() error {
64+
binPath, err := exec.LookPath(os.Args[0])
65+
if err != nil {
66+
return fmt.Errorf("💔 failed to get bin file info: %s: %s", os.Args[0], err)
67+
}
6768

68-
currentFile, err := os.Open(binPath)
69-
if err != nil {
70-
return fmt.Errorf("💔 failed to get bin file info: %s: %s", binPath, err)
71-
}
72-
defer func() { _ = currentFile.Close() }()
69+
currentFile, err := os.Open(binPath)
70+
if err != nil {
71+
return fmt.Errorf("💔 failed to get bin file info: %s: %s", binPath, err)
72+
}
73+
defer func() { _ = currentFile.Close() }()
7374

74-
installFile, err := os.OpenFile(filepath.Join(dir, "gitflow-toolkit"), os.O_CREATE|os.O_RDWR|os.O_TRUNC, 0755)
75-
if err != nil {
76-
return fmt.Errorf("💔 failed to create bin file: %s: %s", filepath.Join(toolKitHome, "gitflow-toolkit"), err)
77-
}
78-
defer func() { _ = installFile.Close() }()
75+
installFile, err := os.OpenFile(filepath.Join(dir, "gitflow-toolkit"), os.O_CREATE|os.O_RDWR|os.O_TRUNC, 0755)
76+
if err != nil {
77+
return fmt.Errorf("💔 failed to create bin file: %s: %s", filepath.Join(toolKitHome, "gitflow-toolkit"), err)
78+
}
79+
defer func() { _ = installFile.Close() }()
7980

80-
_, err = io.Copy(installFile, currentFile)
81-
if err != nil {
82-
return fmt.Errorf("💔 failed to copy file: %s: %s", filepath.Join(toolKitHome, "gitflow-toolkit"), err)
83-
}
84-
return nil
85-
},
86-
},
87-
{
88-
title: "Create symlink...",
89-
f: func() error {
90-
for _, link := range links {
91-
err := os.Symlink(toolKitPath, link)
92-
if err != nil {
93-
return fmt.Errorf("💔 failed to create symlink: %s: %s", link, err)
94-
}
95-
}
96-
return nil
97-
},
81+
_, err = io.Copy(installFile, currentFile)
82+
if err != nil {
83+
return fmt.Errorf("💔 failed to copy file: %s: %s", filepath.Join(toolKitHome, "gitflow-toolkit"), err)
84+
}
85+
return nil
9886
},
99-
{
100-
title: "Set commit hooks...",
101-
f: func() error {
102-
err := os.MkdirAll(toolKitHooks, 0755)
103-
if err != nil {
104-
return fmt.Errorf("💔 failed to create hooks dir: %s: %s", toolKitHooks, err)
105-
}
106-
err = os.Symlink(toolKitPath, filepath.Join(toolKitHooks, "commit-msg"))
87+
},
88+
{
89+
Title: "Create symlink...",
90+
Func: func() error {
91+
for _, link := range links {
92+
err := os.Symlink(toolKitPath, link)
10793
if err != nil {
108-
return fmt.Errorf("💔 failed to create commit hook synlink: %s: %s", filepath.Join(toolKitHooks, "commit-msg"), err)
94+
return fmt.Errorf("💔 failed to create symlink: %s: %s", link, err)
10995
}
110-
_, _ = git("config", "--global", "core.hooksPath", toolKitHooks)
111-
return nil
112-
},
96+
}
97+
return nil
11398
},
114-
{
115-
title: "Install success...",
116-
f: func() error { return nil },
99+
},
100+
{
101+
Title: "Set commit hooks...",
102+
Func: func() error {
103+
err := os.MkdirAll(toolKitHooks, 0755)
104+
if err != nil {
105+
return fmt.Errorf("💔 failed to create hooks dir: %s: %s", toolKitHooks, err)
106+
}
107+
err = os.Symlink(toolKitPath, filepath.Join(toolKitHooks, "commit-msg"))
108+
if err != nil {
109+
return fmt.Errorf("💔 failed to create commit hook synlink: %s: %s", filepath.Join(toolKitHooks, "commit-msg"), err)
110+
}
111+
_, _ = git("config", "--global", "core.hooksPath", toolKitHooks)
112+
return nil
117113
},
118114
},
119-
spinner: stageSpinner,
120-
}
115+
{
116+
Title: "Install success...",
117+
Func: func() error { return nil },
118+
},
119+
})
121120

122121
return tea.NewProgram(m).Start()
123122
}
@@ -132,50 +131,47 @@ func uninstall(dir string) error {
132131
toolKitPath := filepath.Join(dir, "gitflow-toolkit")
133132
links := linkPath(dir)
134133

135-
m := stageModel{
136-
spinner: stageSpinner,
137-
stages: []stage{
138-
{
139-
title: "Clean install dir...",
140-
f: func() error { return os.RemoveAll(toolKitHome) },
141-
},
142-
{
143-
title: "Clean symlinks...",
144-
f: func() error {
145-
for _, link := range links {
146-
if _, err := os.Lstat(link); err == nil {
147-
err := os.RemoveAll(link)
148-
if err != nil {
149-
return fmt.Errorf("💔 failed to remove symlink: %s: %s", link, err)
150-
}
151-
} else if !os.IsNotExist(err) {
152-
return fmt.Errorf("💔 failed to get symlink info: %s: %s", link, err)
134+
m := ui.NewMultiTaskModelWithTasks([]ui.Task{
135+
{
136+
Title: "Clean install dir...",
137+
Func: func() error { return os.RemoveAll(toolKitHome) },
138+
},
139+
{
140+
Title: "Clean symlinks...",
141+
Func: func() error {
142+
for _, link := range links {
143+
if _, err := os.Lstat(link); err == nil {
144+
err := os.RemoveAll(link)
145+
if err != nil {
146+
return fmt.Errorf("💔 failed to remove symlink: %s: %s", link, err)
153147
}
148+
} else if !os.IsNotExist(err) {
149+
return fmt.Errorf("💔 failed to get symlink info: %s: %s", link, err)
154150
}
155-
return nil
156-
},
151+
}
152+
return nil
157153
},
158-
{
159-
title: "Clean bin file...",
160-
f: func() error {
161-
return os.Remove(toolKitPath)
162-
},
154+
},
155+
{
156+
Title: "Clean bin file...",
157+
Func: func() error {
158+
return os.Remove(toolKitPath)
163159
},
164-
{
165-
title: "Unset commit hooks...",
166-
f: func() error {
167-
_, _ = git("config", "--global", "--unset", "core.hooksPath")
168-
return nil
169-
},
160+
},
161+
{
162+
Title: "Unset commit hooks...",
163+
Func: func() error {
164+
_, _ = git("config", "--global", "--unset", "core.hooksPath")
165+
return nil
170166
},
171-
{
172-
title: "UnInstall success...",
173-
f: func() error {
174-
return nil
175-
},
167+
},
168+
{
169+
Title: "UnInstall success...",
170+
Func: func() error {
171+
return nil
176172
},
177173
},
178-
}
174+
})
179175
return tea.NewProgram(m).Start()
180176
}
181177

ui/multi_task.go

Lines changed: 7 additions & 45 deletions
Original file line numberDiff line numberDiff line change
@@ -8,6 +8,8 @@ import (
88
"github.com/charmbracelet/lipgloss"
99
)
1010

11+
var NothingFunc = func() error { return nil }
12+
1113
var (
1214
MultiTaskLayoutStyle = lipgloss.NewStyle().
1315
Padding(0, 0, 1, 2)
@@ -29,7 +31,7 @@ var (
2931
Foreground(lipgloss.Color("#37B9FF"))
3032

3133
MultiTaskSpinner = spinner.Model{
32-
Style: lipgloss.NewStyle().Foreground(lipgloss.Color("#f8ca61")),
34+
Style: lipgloss.NewStyle().Foreground(lipgloss.Color("#F8CA61")),
3335
Spinner: spinner.Spinner{
3436
Frames: []string{
3537
"[ ]",
@@ -140,54 +142,14 @@ func (m MultiTaskModel) View() string {
140142
for i, task := range m.Tasks {
141143
if task.err == nil {
142144
if i < m.index {
143-
view = lipgloss.JoinVertical(lipgloss.Left, view, MultiTaskMsgSuccessStyle.Render("[ ✔ ] "+task.Title))
145+
view = lipgloss.JoinVertical(lipgloss.Left, view, m.MsgSuccessStyle.Render("[ ✔ ] "+task.Title))
144146
} else {
145-
view = lipgloss.JoinVertical(lipgloss.Left, view, m.Spinner.View()+" "+MultiTaskMsgWaitingStyle.Render(task.Title))
147+
view = lipgloss.JoinVertical(lipgloss.Left, view, m.Spinner.View()+" "+m.MsgWaitingStyle.Render(task.Title))
146148
}
147149
} else {
148-
view = lipgloss.JoinVertical(lipgloss.Left, view, MultiTaskMsgFailedStyle.Render("[ ✗ ] "+task.err.Error()))
150+
view = lipgloss.JoinVertical(lipgloss.Left, view, m.MsgFailedStyle.Render("[ ✗ ] "+task.err.Error()))
149151
}
150152

151153
}
152-
return MultiTaskLayoutStyle.Render(MultiTaskBorderStyle.Render(view))
154+
return m.LayoutStyle.Render(m.BorderStyle.Render(view))
153155
}
154-
155-
//func main() {
156-
// m := MultiTaskModel{
157-
// Tasks: []Task{
158-
// {
159-
// Title: "Clean install dir...",
160-
// Func: func() error { return nil },
161-
// },
162-
// {
163-
// Title: "Clean symlinks...",
164-
// Func: func() error { return nil },
165-
// },
166-
// {
167-
// Title: "Unset commit hooks...",
168-
// Func: func() error { return nil },
169-
// },
170-
// {
171-
// Title: "Create toolkit home...",
172-
// Func: func() error { return nil },
173-
// },
174-
// {
175-
// Title: "Install executable file...",
176-
// Func: func() error { return nil },
177-
// },
178-
// {
179-
// Title: "Create symlink...",
180-
// Func: func() error { return errors.New("This is a test message.") },
181-
// },
182-
// {
183-
// Title: "Install success...",
184-
// Func: func() error { return nil },
185-
// },
186-
// },
187-
// Spinner: MultiTaskSpinner,
188-
// }
189-
// if err := tea.NewProgram(&m).Start(); err != nil {
190-
// fmt.Printf("could not start program: %s\n", err)
191-
// os.Exit(1)
192-
// }
193-
//}

ui/multi_task_test.go

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -15,7 +15,7 @@ func init() {
1515
runewidth.DefaultCondition.EastAsianWidth = false
1616
}
1717

18-
func TestMultiTaskModel(t *testing.T) {
18+
func TestMultiTask(t *testing.T) {
1919
m := NewMultiTaskModelWithTasks([]Task{
2020
{
2121
Title: "Clean install dir...",

0 commit comments

Comments
 (0)