Skip to content

Commit 6055e4d

Browse files
committed
fix conflicts
1 parent 3a9f1f4 commit 6055e4d

File tree

1 file changed

+97
-33
lines changed

1 file changed

+97
-33
lines changed

main.go

Lines changed: 97 additions & 33 deletions
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,7 @@ package main
22

33
import (
44
"fmt"
5+
"github.com/charmbracelet/lipgloss"
56
"log"
67
"os"
78
"os/exec"
@@ -16,9 +17,13 @@ type (
1617
)
1718

1819
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
2227
}
2328

2429
func main() {
@@ -33,6 +38,11 @@ func initialModel() model {
3338
if err != nil {
3439
log.Fatal(err)
3540
}
41+
stagedFiles, errStaged := getStagedFiles()
42+
43+
if errStaged != nil {
44+
log.Fatal(errStaged)
45+
}
3646

3747
result := strings.Split(branch, "/")
3848

@@ -46,31 +56,19 @@ func initialModel() model {
4656
ticketId := strings.ToUpper(result[1])
4757

4858
ti := textinput.New()
49-
//ti.Width = 90
50-
//ti.ShowSuggestions = true
5159
ti.SetValue(fmt.Sprintf("[%s] [%s] ", branchType, ticketId))
52-
//ti.SetSuggestions([]string{fmt.Sprintf("[FIX] [%s] ", ticketId), fmt.Sprintf("[IMPR] [%s] ", ticketId)})
5360
ti.Focus()
5461
ti.CharLimit = 156
55-
//ti.Width = 20
5662

5763
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,
6169
}
6270
}
6371

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-
7472
func (m model) Init() tea.Cmd {
7573
return textinput.Blink
7674
}
@@ -82,31 +80,97 @@ func (m model) Update(msg tea.Msg) (tea.Model, tea.Cmd) {
8280
case tea.KeyMsg:
8381
switch msg.Type {
8482
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+
}
8993

90-
err := commitCmd.Run()
91-
if err != nil {
9294
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
93116
}
94-
95-
return m, tea.Quit
96117
case tea.KeyCtrlC, tea.KeyEsc:
97118
return m, tea.Quit
98119
}
99120
}
100121

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+
102128
return m, cmd
103129
}
104130

105131
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+
106147
return fmt.Sprintf(
107-
"Current branch: %s\n\nEnter 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,
111154
) + "\n"
112155
}
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

Comments
 (0)