@@ -2,6 +2,7 @@ package main
2
2
3
3
import (
4
4
"fmt"
5
+ "github.com/charmbracelet/lipgloss"
5
6
"log"
6
7
"os"
7
8
"os/exec"
@@ -16,9 +17,13 @@ type (
16
17
)
17
18
18
19
type model struct {
19
- textInput textinput.Model
20
- branch string
21
- err error
20
+ inputLabel string
21
+ commitMessageInput textinput.Model
22
+ flagsInput textinput.Model
23
+ branch string
24
+ err error
25
+ askingForFlags bool
26
+ stagedFiles []string
22
27
}
23
28
24
29
func main () {
@@ -33,6 +38,11 @@ func initialModel() model {
33
38
if err != nil {
34
39
log .Fatal (err )
35
40
}
41
+ stagedFiles , errStaged := getStagedFiles ()
42
+
43
+ if errStaged != nil {
44
+ log .Fatal (errStaged )
45
+ }
36
46
37
47
result := strings .Split (branch , "/" )
38
48
@@ -46,31 +56,19 @@ func initialModel() model {
46
56
ticketId := strings .ToUpper (result [1 ])
47
57
48
58
ti := textinput .New ()
49
- //ti.Width = 90
50
- //ti.ShowSuggestions = true
51
59
ti .SetValue (fmt .Sprintf ("[%s] [%s] " , branchType , ticketId ))
52
- //ti.SetSuggestions([]string{fmt.Sprintf("[FIX] [%s] ", ticketId), fmt.Sprintf("[IMPR] [%s] ", ticketId)})
53
60
ti .Focus ()
54
61
ti .CharLimit = 156
55
- //ti.Width = 20
56
62
57
63
return model {
58
- textInput : ti ,
59
- branch : branch ,
60
- err : nil ,
64
+ inputLabel : "Enter commit message:" ,
65
+ commitMessageInput : ti ,
66
+ branch : branch ,
67
+ err : nil ,
68
+ stagedFiles : stagedFiles ,
61
69
}
62
70
}
63
71
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
72
- }
73
-
74
72
func (m model ) Init () tea.Cmd {
75
73
return textinput .Blink
76
74
}
@@ -82,31 +80,97 @@ func (m model) Update(msg tea.Msg) (tea.Model, tea.Cmd) {
82
80
case tea.KeyMsg :
83
81
switch msg .Type {
84
82
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
83
+ if m .askingForFlags {
84
+ // Execute git commit command with flags
85
+ commitCmd := exec .Command ("git" , "commit" , m .flagsInput .Value (), "-m" , m .commitMessageInput .Value ())
86
+ commitCmd .Stdout = os .Stdout
87
+ commitCmd .Stderr = os .Stderr
88
+
89
+ err := commitCmd .Run ()
90
+ if err != nil {
91
+ return m , tea .Quit
92
+ }
89
93
90
- err := commitCmd .Run ()
91
- if err != nil {
92
94
return m , tea .Quit
95
+ } else {
96
+ m .inputLabel = "Commit flags"
97
+ // Ask for commit flags
98
+ flagsInput := textinput .New ()
99
+ commitFlags := []string {
100
+ "--message" ,
101
+ "--all" ,
102
+ "--patch" ,
103
+ "--reuse-message" ,
104
+ "--amend" ,
105
+ "--signoff" ,
106
+ "--no-verify" ,
107
+ "--allow-empty" ,
108
+ "--no-edit" ,
109
+ }
110
+ flagsInput .SetSuggestions (commitFlags )
111
+ flagsInput .ShowSuggestions = true
112
+ flagsInput .Focus ()
113
+ m .flagsInput = flagsInput
114
+ m .askingForFlags = true
115
+ return m , nil
93
116
}
94
-
95
- return m , tea .Quit
96
117
case tea .KeyCtrlC , tea .KeyEsc :
97
118
return m , tea .Quit
98
119
}
99
120
}
100
121
101
- m .textInput , cmd = m .textInput .Update (msg )
122
+ if m .askingForFlags {
123
+ m .flagsInput , cmd = m .flagsInput .Update (msg )
124
+ } else {
125
+ m .commitMessageInput , cmd = m .commitMessageInput .Update (msg )
126
+ }
127
+
102
128
return m , cmd
103
129
}
104
130
105
131
func (m model ) View () string {
132
+ var inputView string
133
+ if m .askingForFlags {
134
+ inputView = m .flagsInput .View ()
135
+ } else {
136
+ inputView = m .commitMessageInput .View ()
137
+ }
138
+
139
+ currentCommitMessage := "Current commit message: " + m .commitMessageInput .Value () + " " + m .flagsInput .Value ()
140
+
141
+ stagedFiles := strings .Join (m .stagedFiles , "\n " )
142
+
143
+ currentBranch := lipgloss .NewStyle ().Foreground (lipgloss .Color ("#fbd87f" )).SetString (fmt .Sprintf ("Current branch: %s" , m .branch ))
144
+ inputLabel := lipgloss .NewStyle ().Foreground (lipgloss .Color ("#b5f8fe" )).SetString (m .inputLabel ).Bold (true )
145
+ inputStyle := lipgloss .NewStyle ().SetString (inputView )
146
+
106
147
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)" ,
148
+ "%s\n \n %s\n \n %s\n %s\n \n %s" ,
149
+ currentBranch .Render (),
150
+ currentCommitMessage ,
151
+ inputLabel .Render (),
152
+ inputStyle .Render (),
153
+ stagedFiles ,
111
154
) + "\n "
112
155
}
156
+
157
+ // cmds
158
+ func getCurrentGitBranch () (string , error ) {
159
+ cmd := exec .Command ("git" , "branch" , "--show-current" )
160
+ output , err := cmd .CombinedOutput ()
161
+ if err != nil {
162
+ return "" , fmt .Errorf ("error getting branch name: %v" , err )
163
+ }
164
+
165
+ return strings .TrimSpace (string (output )), nil
166
+ }
167
+
168
+ func getStagedFiles () ([]string , error ) {
169
+ cmd := exec .Command ("git" , "diff" , "--cached" , "--name-only" )
170
+ output , err := cmd .CombinedOutput ()
171
+ if err != nil {
172
+ return nil , fmt .Errorf ("error getting staged files: %v" , err )
173
+ }
174
+
175
+ return strings .Split (strings .TrimSpace (string (output )), "\n " ), nil
176
+ }
0 commit comments