Skip to content

Commit

Permalink
✨ Add toggling between new and amend commits
Browse files Browse the repository at this point in the history
Add feature to toggle between new and amend commits. The state is
preserved between the two different types which helps when deciding
which type of commit to apply.

The existing flag can still be used to set the initial state.

Switching state resets the cursor to the beginning of each input.

Further code changes are required to apply the commit based on the
current state.
  • Loading branch information
mikelorant committed Jan 31, 2023
1 parent 75f985b commit 898ceeb
Show file tree
Hide file tree
Showing 45 changed files with 138 additions and 47 deletions.
15 changes: 15 additions & 0 deletions internal/ui/body/body.go
Original file line number Diff line number Diff line change
Expand Up @@ -119,10 +119,25 @@ func (m Model) Value() string {
return res
}

func (m Model) RawValue() string {
return m.textArea.Value()
}

func (m *Model) SetValue(str string) {
m.textArea.SetValue(str)
}

func (m *Model) Reset() {
m.textArea.Reset()
}

func (m *Model) CursorStart() {
m.textArea.CursorStart()
for i := 1; i < m.textArea.LineCount(); i++ {
m.textArea.CursorUp()
}
}

func ToModel(m tea.Model, c tea.Cmd) (Model, tea.Cmd) {
return m.(Model), c
}
Expand Down
14 changes: 14 additions & 0 deletions internal/ui/defaults.go
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
package ui

import (
"github.com/mikelorant/committed/internal/commit"
"github.com/mikelorant/committed/internal/config"
"github.com/mikelorant/committed/internal/ui/theme"
)
Expand Down Expand Up @@ -39,3 +40,16 @@ func (m *Model) defaultTheme(th string, clr config.Colour) {

m.state.Theme = t
}

func defaultAmendSave(st *commit.State) savedState {
var save savedState

save.amend = true
if e := commit.MessageToEmoji(st.Emojis, st.Repository.Head.Message); e.Valid {
save.emoji = e.Emoji
}
save.summary = commit.MessageToSummary(st.Repository.Head.Message)
save.body = commit.MessageToBody(st.Repository.Head.Message)

return save
}
8 changes: 8 additions & 0 deletions internal/ui/header/header.go
Original file line number Diff line number Diff line change
Expand Up @@ -178,6 +178,14 @@ func (m *Model) SetSummary(str string) {
m.summaryInput.SetValue(str)
}

func (m *Model) ResetSummary() {
m.summaryInput.Reset()
}

func (m *Model) CursorStartSummary() {
m.summaryInput.CursorStart()
}

func (m *Model) ToggleAmend() {
m.Amend = !m.Amend
}
Expand Down
1 change: 1 addition & 0 deletions internal/ui/status/status.go
Original file line number Diff line number Diff line change
Expand Up @@ -138,6 +138,7 @@ func defaultKeyBindings() []shortcut.KeyBinding {
return []shortcut.KeyBinding{
{Modifier: shortcut.ControlModifier, Key: "c", Label: "Cancel"},
{Modifier: shortcut.AltModifier, Key: "enter", Label: "Commit"},
{Modifier: shortcut.AltModifier, Key: "a", Label: "Amend"},
{Modifier: shortcut.AltModifier, Key: "s", Label: "Sign-off"},
{Modifier: shortcut.AltModifier, Key: "/", Label: "Help"},
}
Expand Down
2 changes: 1 addition & 1 deletion internal/ui/status/testdata/default.golden
Original file line number Diff line number Diff line change
@@ -1,2 +1,2 @@
Alt + <enter> Commit <s> Sign-off </> Help
Alt + <enter> Commit <a> Amend <s> Sign-off </> Help
Ctrl + <c> Cancel
2 changes: 1 addition & 1 deletion internal/ui/status/testdata/help.golden
Original file line number Diff line number Diff line change
@@ -1,2 +1,2 @@
Alt + <enter> Commit <s> Sign-off </> Help Exit <esc>
Alt + <enter> Commit <a> Amend <s> Sign-off </> Help Exit <esc>
Ctrl + <c> Cancel
2 changes: 1 addition & 1 deletion internal/ui/status/testdata/next.golden
Original file line number Diff line number Diff line change
@@ -1,2 +1,2 @@
Alt + <enter> Commit <s> Sign-off </> Help next <tab>
Alt + <enter> Commit <a> Amend <s> Sign-off </> Help next <tab>
Ctrl + <c> Cancel
2 changes: 1 addition & 1 deletion internal/ui/status/testdata/next_previous.golden
Original file line number Diff line number Diff line change
@@ -1,2 +1,2 @@
Alt + <enter> Commit <s> Sign-off </> Help next <tab>
Alt + <enter> Commit <a> Amend <s> Sign-off </> Help next <tab>
Ctrl + <c> Cancel previous <tab> + Shift
2 changes: 1 addition & 1 deletion internal/ui/status/testdata/previous.golden
Original file line number Diff line number Diff line change
@@ -1,2 +1,2 @@
Alt + <enter> Commit <s> Sign-off </> Help
Alt + <enter> Commit <a> Amend <s> Sign-off </> Help
Ctrl + <c> Cancel previous <tab> + Shift
2 changes: 1 addition & 1 deletion internal/ui/testdata/alt+1.golden
Original file line number Diff line number Diff line change
Expand Up @@ -28,5 +28,5 @@ date: Sat Jan 1 01:00:00 2022 +0000
│ ~ │
└──────────────────────────────────────────────────────────────────────────┘

Alt + <enter> Commit <s> Sign-off </> Help Emoji <tab>
Alt + <enter> Commit <a> Amend <s> Sign-off </> Help Emoji <tab>
Ctrl + <c> Cancel
2 changes: 1 addition & 1 deletion internal/ui/testdata/alt+1_twice.golden
Original file line number Diff line number Diff line change
Expand Up @@ -28,5 +28,5 @@ date: Sat Jan 1 01:00:00 2022 +0000
│ ~ │
└──────────────────────────────────────────────────────────────────────────┘

Alt + <enter> Commit <s> Sign-off </> Help Emoji <tab>
Alt + <enter> Commit <a> Amend <s> Sign-off </> Help Emoji <tab>
Ctrl + <c> Cancel
2 changes: 1 addition & 1 deletion internal/ui/testdata/alt+2.golden
Original file line number Diff line number Diff line change
Expand Up @@ -28,5 +28,5 @@ date: Sat Jan 1 01:00:00 2022 +0000
│ ~ │
└──────────────────────────────────────────────────────────────────────────┘

Alt + <enter> Commit <s> Sign-off </> Help Summary <tab>
Alt + <enter> Commit <a> Amend <s> Sign-off </> Help Summary <tab>
Ctrl + <c> Cancel Author <tab> + Shift
2 changes: 1 addition & 1 deletion internal/ui/testdata/alt+2_twice.golden
Original file line number Diff line number Diff line change
Expand Up @@ -28,5 +28,5 @@ date: Sat Jan 1 01:00:00 2022 +0000
│ ~ │
└──────────────────────────────────────────────────────────────────────────┘

Alt + <enter> Commit <s> Sign-off </> Help Summary <tab>
Alt + <enter> Commit <a> Amend <s> Sign-off </> Help Summary <tab>
Ctrl + <c> Cancel Author <tab> + Shift
2 changes: 1 addition & 1 deletion internal/ui/testdata/alt+3.golden
Original file line number Diff line number Diff line change
Expand Up @@ -28,5 +28,5 @@ date: Sat Jan 1 01:00:00 2022 +0000
│ ~ │
└──────────────────────────────────────────────────────────────────────────┘

Alt + <enter> Commit <s> Sign-off </> Help Body <tab>
Alt + <enter> Commit <a> Amend <s> Sign-off </> Help Body <tab>
Ctrl + <c> Cancel Emoji <tab> + Shift
2 changes: 1 addition & 1 deletion internal/ui/testdata/alt+3_twice.golden
Original file line number Diff line number Diff line change
Expand Up @@ -28,5 +28,5 @@ date: Sat Jan 1 01:00:00 2022 +0000
│ ~ │
└──────────────────────────────────────────────────────────────────────────┘

Alt + <enter> Commit <s> Sign-off </> Help Body <tab>
Alt + <enter> Commit <a> Amend <s> Sign-off </> Help Body <tab>
Ctrl + <c> Cancel Emoji <tab> + Shift
2 changes: 1 addition & 1 deletion internal/ui/testdata/alt+4.golden
Original file line number Diff line number Diff line change
Expand Up @@ -28,5 +28,5 @@ date: Sat Jan 1 01:00:00 2022 +0000
│ │
└──────────────────────────────────────────────────────────────────────────┘

Alt + <enter> Commit <s> Sign-off </> Help
Alt + <enter> Commit <a> Amend <s> Sign-off </> Help
Ctrl + <c> Cancel Summary <tab> + Shift
2 changes: 1 addition & 1 deletion internal/ui/testdata/alt+4_twice.golden
Original file line number Diff line number Diff line change
Expand Up @@ -28,5 +28,5 @@ date: Sat Jan 1 01:00:00 2022 +0000
│ │
└──────────────────────────────────────────────────────────────────────────┘

Alt + <enter> Commit <s> Sign-off </> Help
Alt + <enter> Commit <a> Amend <s> Sign-off </> Help
Ctrl + <c> Cancel Summary <tab> + Shift
2 changes: 1 addition & 1 deletion internal/ui/testdata/alt+enter_invalid.golden
Original file line number Diff line number Diff line change
Expand Up @@ -28,5 +28,5 @@ date: Sat Jan 1 01:00:00 2022 +0000
│ │
└──────────────────────────────────────────────────────────────────────────┘

Alt + <enter> Commit <s> Sign-off </> Help
Alt + <enter> Commit <a> Amend <s> Sign-off </> Help
Ctrl + <c> Cancel Summary <tab> + Shift
2 changes: 1 addition & 1 deletion internal/ui/testdata/alt+s.golden
Original file line number Diff line number Diff line change
Expand Up @@ -28,5 +28,5 @@ date: Sat Jan 1 01:00:00 2022 +0000

Signed-off-by: John Doe <[email protected]>

Alt + <enter> Commit <s> Sign-off </> Help Summary <tab>
Alt + <enter> Commit <a> Amend <s> Sign-off </> Help Summary <tab>
Ctrl + <c> Cancel Author <tab> + Shift
2 changes: 1 addition & 1 deletion internal/ui/testdata/alt+s_change_author.golden
Original file line number Diff line number Diff line change
Expand Up @@ -28,5 +28,5 @@ date: Sat Jan 1 01:00:00 2022 +0000

Signed-off-by: John Doe <[email protected]>

Alt + <enter> Commit <s> Sign-off </> Help Summary <tab>
Alt + <enter> Commit <a> Amend <s> Sign-off </> Help Summary <tab>
Ctrl + <c> Cancel Author <tab> + Shift
2 changes: 1 addition & 1 deletion internal/ui/testdata/alt+s_twice.golden
Original file line number Diff line number Diff line change
Expand Up @@ -28,5 +28,5 @@ date: Sat Jan 1 01:00:00 2022 +0000
│ ~ │
└──────────────────────────────────────────────────────────────────────────┘

Alt + <enter> Commit <s> Sign-off </> Help Summary <tab>
Alt + <enter> Commit <a> Amend <s> Sign-off </> Help Summary <tab>
Ctrl + <c> Cancel Author <tab> + Shift
2 changes: 1 addition & 1 deletion internal/ui/testdata/alt+slash.golden
Original file line number Diff line number Diff line change
Expand Up @@ -28,5 +28,5 @@ date: Sat Jan 1 01:00:00 2022 +0000
│ │
└──────────────────────────────────────────────────────────────────────────┘

Alt + <enter> Commit <s> Sign-off </> Help Exit <esc>
Alt + <enter> Commit <a> Amend <s> Sign-off </> Help Exit <esc>
Ctrl + <c> Cancel
2 changes: 1 addition & 1 deletion internal/ui/testdata/alt+slash_twice.golden
Original file line number Diff line number Diff line change
Expand Up @@ -28,5 +28,5 @@ date: Sat Jan 1 01:00:00 2022 +0000
│ ~ │
└──────────────────────────────────────────────────────────────────────────┘

Alt + <enter> Commit <s> Sign-off </> Help Summary <tab>
Alt + <enter> Commit <a> Amend <s> Sign-off </> Help Summary <tab>
Ctrl + <c> Cancel Author <tab> + Shift
2 changes: 1 addition & 1 deletion internal/ui/testdata/alt+slash_twice_body.golden
Original file line number Diff line number Diff line change
Expand Up @@ -28,5 +28,5 @@ date: Sat Jan 1 01:00:00 2022 +0000
│ │
└──────────────────────────────────────────────────────────────────────────┘

Alt + <enter> Commit <s> Sign-off </> Help
Alt + <enter> Commit <a> Amend <s> Sign-off </> Help
Ctrl + <c> Cancel Summary <tab> + Shift
2 changes: 1 addition & 1 deletion internal/ui/testdata/alt+t.golden
Original file line number Diff line number Diff line change
Expand Up @@ -28,5 +28,5 @@ date: Sat Jan 1 01:00:00 2022 +0000
│ ~ │
└──────────────────────────────────────────────────────────────────────────┘

Alt + <enter> Commit <s> Sign-off </> Help Summary <tab>
Alt + <enter> Commit <a> Amend <s> Sign-off </> Help Summary <tab>
Ctrl + <c> Cancel Author <tab> + Shift
2 changes: 1 addition & 1 deletion internal/ui/testdata/config_author.golden
Original file line number Diff line number Diff line change
Expand Up @@ -28,5 +28,5 @@ date: Sat Jan 1 01:00:00 2022 +0000
│ ~ │
└──────────────────────────────────────────────────────────────────────────┘

Alt + <enter> Commit <s> Sign-off </> Help Emoji <tab>
Alt + <enter> Commit <a> Amend <s> Sign-off </> Help Emoji <tab>
Ctrl + <c> Cancel
2 changes: 1 addition & 1 deletion internal/ui/testdata/config_emoji.golden
Original file line number Diff line number Diff line change
Expand Up @@ -28,5 +28,5 @@ date: Sat Jan 1 01:00:00 2022 +0000
│ ~ │
└──────────────────────────────────────────────────────────────────────────┘

Alt + <enter> Commit <s> Sign-off </> Help Summary <tab>
Alt + <enter> Commit <a> Amend <s> Sign-off </> Help Summary <tab>
Ctrl + <c> Cancel Author <tab> + Shift
2 changes: 1 addition & 1 deletion internal/ui/testdata/config_signoff_off.golden
Original file line number Diff line number Diff line change
Expand Up @@ -28,5 +28,5 @@ date: Sat Jan 1 01:00:00 2022 +0000
│ ~ │
└──────────────────────────────────────────────────────────────────────────┘

Alt + <enter> Commit <s> Sign-off </> Help Summary <tab>
Alt + <enter> Commit <a> Amend <s> Sign-off </> Help Summary <tab>
Ctrl + <c> Cancel Author <tab> + Shift
2 changes: 1 addition & 1 deletion internal/ui/testdata/config_signoff_on.golden
Original file line number Diff line number Diff line change
Expand Up @@ -28,5 +28,5 @@ date: Sat Jan 1 01:00:00 2022 +0000

Signed-off-by: John Doe <[email protected]>

Alt + <enter> Commit <s> Sign-off </> Help Summary <tab>
Alt + <enter> Commit <a> Amend <s> Sign-off </> Help Summary <tab>
Ctrl + <c> Cancel Author <tab> + Shift
2 changes: 1 addition & 1 deletion internal/ui/testdata/config_summary.golden
Original file line number Diff line number Diff line change
Expand Up @@ -28,5 +28,5 @@ date: Sat Jan 1 01:00:00 2022 +0000
│ ~ │
└──────────────────────────────────────────────────────────────────────────┘

Alt + <enter> Commit <s> Sign-off </> Help Body <tab>
Alt + <enter> Commit <a> Amend <s> Sign-off </> Help Body <tab>
Ctrl + <c> Cancel Emoji <tab> + Shift
2 changes: 1 addition & 1 deletion internal/ui/testdata/ctrl+c.golden
Original file line number Diff line number Diff line change
Expand Up @@ -28,5 +28,5 @@ date: Sat Jan 1 01:00:00 2022 +0000
│ ~ │
└──────────────────────────────────────────────────────────────────────────┘

Alt + <enter> Commit <s> Sign-off </> Help Summary <tab>
Alt + <enter> Commit <a> Amend <s> Sign-off </> Help Summary <tab>
Ctrl + <c> Cancel Author <tab> + Shift
2 changes: 1 addition & 1 deletion internal/ui/testdata/enter_author.golden
Original file line number Diff line number Diff line change
Expand Up @@ -28,5 +28,5 @@ date: Sat Jan 1 01:00:00 2022 +0000
│ ~ │
└──────────────────────────────────────────────────────────────────────────┘

Alt + <enter> Commit <s> Sign-off </> Help Summary <tab>
Alt + <enter> Commit <a> Amend <s> Sign-off </> Help Summary <tab>
Ctrl + <c> Cancel Author <tab> + Shift
2 changes: 1 addition & 1 deletion internal/ui/testdata/enter_body.golden
Original file line number Diff line number Diff line change
Expand Up @@ -28,5 +28,5 @@ date: Sat Jan 1 01:00:00 2022 +0000
│ │
└──────────────────────────────────────────────────────────────────────────┘

Alt + <enter> Commit <s> Sign-off </> Help
Alt + <enter> Commit <a> Amend <s> Sign-off </> Help
Ctrl + <c> Cancel Summary <tab> + Shift
2 changes: 1 addition & 1 deletion internal/ui/testdata/enter_emoji.golden
Original file line number Diff line number Diff line change
Expand Up @@ -28,5 +28,5 @@ date: Sat Jan 1 01:00:00 2022 +0000
│ ~ │
└──────────────────────────────────────────────────────────────────────────┘

Alt + <enter> Commit <s> Sign-off </> Help Body <tab>
Alt + <enter> Commit <a> Amend <s> Sign-off </> Help Body <tab>
Ctrl + <c> Cancel Emoji <tab> + Shift
2 changes: 1 addition & 1 deletion internal/ui/testdata/enter_summary.golden
Original file line number Diff line number Diff line change
Expand Up @@ -28,5 +28,5 @@ date: Sat Jan 1 01:00:00 2022 +0000
│ │
└──────────────────────────────────────────────────────────────────────────┘

Alt + <enter> Commit <s> Sign-off </> Help
Alt + <enter> Commit <a> Amend <s> Sign-off </> Help
Ctrl + <c> Cancel Summary <tab> + Shift
2 changes: 1 addition & 1 deletion internal/ui/testdata/escape_help.golden
Original file line number Diff line number Diff line change
Expand Up @@ -28,5 +28,5 @@ date: Sat Jan 1 01:00:00 2022 +0000
│ ~ │
└──────────────────────────────────────────────────────────────────────────┘

Alt + <enter> Commit <s> Sign-off </> Help Summary <tab>
Alt + <enter> Commit <a> Amend <s> Sign-off </> Help Summary <tab>
Ctrl + <c> Cancel Author <tab> + Shift
2 changes: 1 addition & 1 deletion internal/ui/testdata/shift_tab_author.golden
Original file line number Diff line number Diff line change
Expand Up @@ -28,5 +28,5 @@ date: Sat Jan 1 01:00:00 2022 +0000
│ ~ │
└──────────────────────────────────────────────────────────────────────────┘

Alt + <enter> Commit <s> Sign-off </> Help Emoji <tab>
Alt + <enter> Commit <a> Amend <s> Sign-off </> Help Emoji <tab>
Ctrl + <c> Cancel
2 changes: 1 addition & 1 deletion internal/ui/testdata/shift_tab_body.golden
Original file line number Diff line number Diff line change
Expand Up @@ -28,5 +28,5 @@ date: Sat Jan 1 01:00:00 2022 +0000
│ │
└──────────────────────────────────────────────────────────────────────────┘

Alt + <enter> Commit <s> Sign-off </> Help Body <tab>
Alt + <enter> Commit <a> Amend <s> Sign-off </> Help Body <tab>
Ctrl + <c> Cancel Emoji <tab> + Shift
2 changes: 1 addition & 1 deletion internal/ui/testdata/shift_tab_emoji.golden
Original file line number Diff line number Diff line change
Expand Up @@ -28,5 +28,5 @@ date: Sat Jan 1 01:00:00 2022 +0000
│ ~ │
└──────────────────────────────────────────────────────────────────────────┘

Alt + <enter> Commit <s> Sign-off </> Help Emoji <tab>
Alt + <enter> Commit <a> Amend <s> Sign-off </> Help Emoji <tab>
Ctrl + <c> Cancel
2 changes: 1 addition & 1 deletion internal/ui/testdata/shift_tab_summary.golden
Original file line number Diff line number Diff line change
Expand Up @@ -28,5 +28,5 @@ date: Sat Jan 1 01:00:00 2022 +0000
│ ~ │
└──────────────────────────────────────────────────────────────────────────┘

Alt + <enter> Commit <s> Sign-off </> Help Summary <tab>
Alt + <enter> Commit <a> Amend <s> Sign-off </> Help Summary <tab>
Ctrl + <c> Cancel Author <tab> + Shift
Loading

0 comments on commit 898ceeb

Please sign in to comment.