Skip to content

Commit 43a0a79

Browse files
authored
Merge pull request #3 from mrados7/IMPR/commit-command-form-improvements
commit commands improvements
2 parents 1ff0ac7 + b8a3331 commit 43a0a79

File tree

3 files changed

+42
-61
lines changed

3 files changed

+42
-61
lines changed

.git-commands.json

Lines changed: 0 additions & 26 deletions
This file was deleted.

cmd/commit/main.go

Lines changed: 31 additions & 34 deletions
Original file line numberDiff line numberDiff line change
@@ -15,7 +15,7 @@ import (
1515
)
1616

1717
var (
18-
focusedStyle = lipgloss.NewStyle().Foreground(lipgloss.Color("#6FD0FB")).Bold(true)
18+
focusedStyle = lipgloss.NewStyle().Foreground(lipgloss.Color("#DA8BFF")).Bold(true)
1919
blurredStyle = lipgloss.NewStyle().Foreground(lipgloss.Color("240"))
2020
cursorStyle = focusedStyle.Copy()
2121
noStyle = lipgloss.NewStyle()
@@ -37,7 +37,7 @@ func main() {
3737
if !isGitRepo {
3838
log.Fatal("Not a git repository")
3939
}
40-
p := tea.NewProgram(initialModel())
40+
p := tea.NewProgram(initialModel(), tea.WithAltScreen())
4141
if _, err := p.Run(); err != nil {
4242
log.Fatal(err)
4343
}
@@ -57,12 +57,6 @@ func initialModel() model {
5757

5858
result := strings.Split(branch, "/")
5959

60-
// get first and second result into separate variables
61-
// check if result has more than 2 items
62-
//if len(result) <= 2 {
63-
// log.Fatal("Branch name should be in the format of <type>/<task-id>/short-message")
64-
//}
65-
6660
var branchType string
6761
var ticketId string
6862

@@ -87,7 +81,12 @@ func initialModel() model {
8781

8882
switch i {
8983
case 0:
90-
t.SetValue(fmt.Sprintf("[%s] [%s] ", branchType, ticketId))
84+
if branchType != "" {
85+
t.SetValue(fmt.Sprintf("[%s] ", branchType))
86+
}
87+
if ticketId != "" {
88+
t.SetValue(t.Value() + fmt.Sprintf("[%s] ", ticketId))
89+
}
9190
t.Focus()
9291
t.PromptStyle = focusedStyle
9392
t.TextStyle = focusedStyle
@@ -193,46 +192,44 @@ func (m *model) updateInputs(msg tea.Msg) tea.Cmd {
193192
}
194193

195194
func (m model) View() string {
196-
var b strings.Builder
195+
s := ""
196+
197+
stagedFilesView := ""
198+
stagedFilesView += lipgloss.JoinVertical(lipgloss.Top, "Staged files:")
199+
stagedFilesView += "\n"
200+
for index, elem := range m.stagedFiles {
201+
stagedFilesView += lipgloss.JoinVertical(lipgloss.Top, stagedFilesStyle.Render(fmt.Sprintf("• %s", elem)))
202+
if index != len(m.stagedFiles)-1 {
203+
stagedFilesView += "\n"
204+
}
205+
}
197206

198207
currentBranch := lipgloss.NewStyle().Foreground(lipgloss.Color("#fcbda1")).SetString(fmt.Sprintf("Branch: %s", m.branch))
199208

200-
b.WriteString(currentBranch.Render())
201-
b.WriteRune('\n')
202-
b.WriteRune('\n')
203-
204-
currentCommitCommand := lipgloss.NewStyle().Foreground(lipgloss.Color("#a1e0fc")).SetString(fmt.Sprintf("git commit -m \"%s\" %s", m.inputs[0].Value(), m.inputs[1].Value()))
209+
s += lipgloss.JoinVertical(lipgloss.Top, currentBranch.Render())
210+
s += "\n\n"
205211

206-
b.WriteString(currentCommitCommand.Render())
207-
b.WriteRune('\n')
208-
b.WriteRune('\n')
209-
210-
inputLabels := []string{"Commit message ", "Commit flags "}
212+
inputLabels := []string{"Commit message:", "Commit flags (optional):"}
211213

212214
for i := range m.inputs {
213-
if i == m.focusInputIndex {
214-
b.WriteString(lipgloss.NewStyle().Foreground(lipgloss.Color("#6FD0FB")).Bold(true).SetString(inputLabels[i]).Render())
215-
} else {
216-
b.WriteString(inputLabels[i])
217-
}
218-
b.WriteString(m.inputs[i].View())
219-
b.WriteRune('\n')
215+
s += lipgloss.JoinVertical(1, inputLabels[i])
216+
s += "\n"
217+
s += lipgloss.JoinVertical(0.5, m.inputs[i].View())
220218
if i < len(m.inputs)-1 {
221-
b.WriteRune('\n')
219+
s += "\n"
222220
}
223221
}
224222

225223
button := &blurredButton
226224
if m.focusInputIndex == len(m.inputs) {
227225
button = &focusedButton
228226
}
229-
fmt.Fprintf(&b, "\n\n%s\n\n", *button)
227+
s += "\n\n"
228+
s += lipgloss.JoinVertical(1, *button)
230229

231-
b.WriteString("Staged changes: ")
232-
b.WriteRune('\n')
233-
b.WriteString(stagedFilesStyle.Render(strings.Join(m.stagedFiles, "\n")))
230+
stagedFilesStyle := lipgloss.NewStyle().MarginLeft(8).Border(lipgloss.NormalBorder()).Padding(0, 1)
234231

235-
b.WriteRune('\n')
232+
f := lipgloss.JoinHorizontal(lipgloss.Left, s, stagedFilesStyle.Render(stagedFilesView))
236233

237-
return b.String()
234+
return f
238235
}

cmd/git/git.go

Lines changed: 11 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -32,7 +32,17 @@ func GetStagedFiles() []string {
3232
return []string{}
3333
}
3434

35-
return strings.Split(string(out), "\n")
35+
return filterEmpty(strings.Split(string(out), "\n"))
36+
}
37+
38+
func filterEmpty(s []string) []string {
39+
var r []string
40+
for _, str := range s {
41+
if str != "" {
42+
r = append(r, str)
43+
}
44+
}
45+
return r
3646
}
3747

3848
func GetCurrentGitBranch() (string, error) {

0 commit comments

Comments
 (0)