@@ -2,74 +2,111 @@ package main
2
2
3
3
import (
4
4
"fmt"
5
+ "log"
5
6
"os"
7
+ "os/exec"
8
+ "strings"
6
9
7
- "github.com/charmbracelet/bubbles/key"
8
- "github.com/charmbracelet/bubbles/spinner"
10
+ "github.com/charmbracelet/bubbles/textinput"
9
11
tea "github.com/charmbracelet/bubbletea"
10
- "github.com/charmbracelet/lipgloss"
11
12
)
12
13
13
- type errMsg error
14
+ type (
15
+ errMsg error
16
+ )
14
17
15
18
type model struct {
16
- spinner spinner .Model
17
- quitting bool
18
- err error
19
+ textInput textinput .Model
20
+ branch string
21
+ err error
19
22
}
20
23
21
- var quitKeys = key .NewBinding (
22
- key .WithKeys ("q" , "esc" , "ctrl+c" ),
23
- key .WithHelp ("" , "press q to quit" ),
24
- )
24
+ func main () {
25
+ p := tea .NewProgram (initialModel ())
26
+ if _ , err := p .Run (); err != nil {
27
+ log .Fatal (err )
28
+ }
29
+ }
25
30
26
31
func initialModel () model {
27
- s := spinner .New ()
28
- s .Spinner = spinner .Dot
29
- s .Style = lipgloss .NewStyle ().Foreground (lipgloss .Color ("205" ))
30
- return model {spinner : s }
32
+ branch , err := getCurrentGitBranch ()
33
+ if err != nil {
34
+ log .Fatal (err )
35
+ }
36
+
37
+ result := strings .Split (branch , "/" )
38
+
39
+ // get first and second result into separate variables
40
+ // check if result has more than 2 items
41
+ if len (result ) <= 2 {
42
+ log .Fatal ("Branch name should be in the format of <type>/<task-id>/short-message" )
43
+ }
44
+
45
+ branchType := strings .ToUpper (result [0 ])
46
+ ticketId := strings .ToUpper (result [1 ])
47
+
48
+ ti := textinput .New ()
49
+ //ti.Width = 90
50
+ //ti.ShowSuggestions = true
51
+ ti .SetValue (fmt .Sprintf ("[%s] [%s] " , branchType , ticketId ))
52
+ //ti.SetSuggestions([]string{fmt.Sprintf("[FIX] [%s] ", ticketId), fmt.Sprintf("[IMPR] [%s] ", ticketId)})
53
+ ti .Focus ()
54
+ ti .CharLimit = 156
55
+ //ti.Width = 20
56
+
57
+ return model {
58
+ textInput : ti ,
59
+ branch : branch ,
60
+ err : nil ,
61
+ }
62
+ }
63
+
64
+ func getCurrentGitBranch () (string , error ) {
65
+ cmd := exec .Command ("git" , "branch" , "--show-current" )
66
+ output , err := cmd .CombinedOutput ()
67
+ if err != nil {
68
+ return "" , fmt .Errorf ("error getting branch name: %v" , err )
69
+ }
70
+
71
+ return strings .TrimSpace (string (output )), nil
31
72
}
32
73
33
74
func (m model ) Init () tea.Cmd {
34
- return m . spinner . Tick
75
+ return textinput . Blink
35
76
}
36
77
37
78
func (m model ) Update (msg tea.Msg ) (tea.Model , tea.Cmd ) {
38
- switch msg := msg .( type ) {
79
+ var cmd tea. Cmd
39
80
81
+ switch msg := msg .(type ) {
40
82
case tea.KeyMsg :
41
- if key .Matches (msg , quitKeys ) {
42
- m .quitting = true
43
- return m , tea .Quit
83
+ switch msg .Type {
84
+ case tea .KeyEnter :
85
+ // Execute git commit command
86
+ commitCmd := exec .Command ("git" , "commit" , "-m" , m .textInput .Value ())
87
+ commitCmd .Stdout = os .Stdout
88
+ commitCmd .Stderr = os .Stderr
44
89
90
+ err := commitCmd .Run ()
91
+ if err != nil {
92
+ return m , tea .Quit
93
+ }
94
+
95
+ return m , tea .Quit
96
+ case tea .KeyCtrlC , tea .KeyEsc :
97
+ return m , tea .Quit
45
98
}
46
- return m , nil
47
- case errMsg :
48
- m .err = msg
49
- return m , nil
50
-
51
- default :
52
- var cmd tea.Cmd
53
- m .spinner , cmd = m .spinner .Update (msg )
54
- return m , cmd
55
99
}
56
- }
57
100
58
- func (m model ) View () string {
59
- if m .err != nil {
60
- return m .err .Error ()
61
- }
62
- str := fmt .Sprintf ("\n \n %s Loading forever... %s\n \n " , m .spinner .View (), quitKeys .Help ().Desc )
63
- if m .quitting {
64
- return str + "\n "
65
- }
66
- return str
101
+ m .textInput , cmd = m .textInput .Update (msg )
102
+ return m , cmd
67
103
}
68
104
69
- func main () {
70
- p := tea .NewProgram (initialModel ())
71
- if _ , err := p .Run (); err != nil {
72
- fmt .Println (err )
73
- os .Exit (1 )
74
- }
105
+ func (m model ) View () string {
106
+ return fmt .Sprintf (
107
+ "Current branch: %s\n \n Enter commit message\n \n %s\n \n %s" ,
108
+ m .branch ,
109
+ m .textInput .View (),
110
+ "(esc to quit, enter to commit)" ,
111
+ ) + "\n "
75
112
}
0 commit comments