From f765d561749495525e9ee1d1dcea7e5226af9a88 Mon Sep 17 00:00:00 2001 From: Michael Lorant Date: Wed, 25 Jan 2023 11:00:11 +1100 Subject: [PATCH] :sparkles: Add configuration to set sign-off Allow setting the default state for sign-off of commits. Identified a problem with reseting the footer model which was not changing the signoff to the default value. By fixing this issue, there is no need to use the toggle signoff method within the footer model. --- README.md | 6 ++++ internal/ui/footer/footer.go | 13 ++++++-- .../ui/testdata/config_signoff_off.golden | 32 +++++++++++++++++++ internal/ui/testdata/config_signoff_on.golden | 32 +++++++++++++++++++ internal/ui/ui.go | 3 +- internal/ui/ui_test.go | 24 ++++++++++++++ 6 files changed, 106 insertions(+), 4 deletions(-) create mode 100644 internal/ui/testdata/config_signoff_off.golden create mode 100644 internal/ui/testdata/config_signoff_on.golden diff --git a/README.md b/README.md index fcf1e9c..c8f297a 100644 --- a/README.md +++ b/README.md @@ -124,6 +124,12 @@ view: # Default: below emojiSelector: below +commit: + # Enable author sign-off for commits. + # Values: true, false + # Default: false + signoff: false + authors: # List of extra authors. - name: John Doe diff --git a/internal/ui/footer/footer.go b/internal/ui/footer/footer.go index b22f3d9..af322c6 100644 --- a/internal/ui/footer/footer.go +++ b/internal/ui/footer/footer.go @@ -16,12 +16,14 @@ type Model struct { } func New(state *commit.State) Model { - if len(state.Repository.Users) == 0 { - state.Repository.Users = []repository.User{{}} + authors := concatSlice(state.Repository.Users, state.Config.Authors) + + if len(authors) == 0 { + authors = []repository.User{{}} } return Model{ - Author: state.Repository.Users[0], + Author: authors[0], } } @@ -68,3 +70,8 @@ func (m Model) signoff() string { func ToModel(m tea.Model, c tea.Cmd) (Model, tea.Cmd) { return m.(Model), c } + +func concatSlice[T any](first []T, second []T) []T { + n := len(first) + return append(first[:n:n], second...) +} diff --git a/internal/ui/testdata/config_signoff_off.golden b/internal/ui/testdata/config_signoff_off.golden new file mode 100644 index 0000000..510ca08 --- /dev/null +++ b/internal/ui/testdata/config_signoff_off.golden @@ -0,0 +1,32 @@ +commit 1 (HEAD -> master) +author: John Doe +date: Sat Jan 1 01:00:00 2022 +0000 + + ┌────┐ ┌─────────────────────────────────────────────────────────────┐ + │ │ │ placeholder │ 0/50 + └────┘ └─────────────────────────────────────────────────────────────┘ + + ┌──────────────────────────────────────────────────────────────────────────┐ + │? Choose an emoji: ● │ + │❯ 🎨 - Improve structure / format of the code. │ + │ │ + │ │ + │ │ + │ │ + │ │ + │ │ + │ │ + │ │ + └──────────────────────────────────────────────────────────────────────────┘ + + ┌──────────────────────────────────────────────────────────────────────────┐ + │ placeholder │ + │ ~ │ + │ ~ │ + │ ~ │ + │ ~ │ + │ ~ │ + └──────────────────────────────────────────────────────────────────────────┘ + + Alt + Commit Sign-off Help Summary +Ctrl + Cancel Author + Shift diff --git a/internal/ui/testdata/config_signoff_on.golden b/internal/ui/testdata/config_signoff_on.golden new file mode 100644 index 0000000..df7f791 --- /dev/null +++ b/internal/ui/testdata/config_signoff_on.golden @@ -0,0 +1,32 @@ +commit 1 (HEAD -> master) +author: John Doe +date: Sat Jan 1 01:00:00 2022 +0000 + + ┌────┐ ┌─────────────────────────────────────────────────────────────┐ + │ │ │ placeholder │ 0/50 + └────┘ └─────────────────────────────────────────────────────────────┘ + + ┌──────────────────────────────────────────────────────────────────────────┐ + │? Choose an emoji: ● │ + │❯ 🎨 - Improve structure / format of the code. │ + │ │ + │ │ + │ │ + │ │ + │ │ + │ │ + │ │ + │ │ + └──────────────────────────────────────────────────────────────────────────┘ + + ┌──────────────────────────────────────────────────────────────────────────┐ + │ placeholder │ + │ ~ │ + │ ~ │ + │ ~ │ + └──────────────────────────────────────────────────────────────────────────┘ + + Signed-off-by: John Doe + + Alt + Commit Sign-off Help Summary +Ctrl + Cancel Author + Shift diff --git a/internal/ui/ui.go b/internal/ui/ui.go index b7f5698..5946734 100644 --- a/internal/ui/ui.go +++ b/internal/ui/ui.go @@ -88,6 +88,7 @@ func (m *Model) Configure(state *commit.State) { } m.defaultFocus(state.Config) + m.signoff = state.Config.Commit.Signoff } func (m Model) Start() (*commit.Request, error) { @@ -227,7 +228,6 @@ func (m Model) onKeyPress(msg tea.KeyMsg) keyResponse { return keyResponse{model: m, cmd: tea.Quit, end: true} case "alt+s": m.signoff = !m.signoff - m.models.footer.ToggleSignoff() return keyResponse{model: m, end: false, nilMsg: true} case "alt+t": @@ -276,6 +276,7 @@ func (m Model) resetModels() Model { m.models.body.Blur() m.models.body.Height = bodyDefaultHeight m.models.footer.Author = m.models.info.Author + m.models.footer.Signoff = m.signoff m.models.help.Blur() return m diff --git a/internal/ui/ui_test.go b/internal/ui/ui_test.go index ad82e09..702b79c 100644 --- a/internal/ui/ui_test.go +++ b/internal/ui/ui_test.go @@ -529,6 +529,30 @@ func TestModel(t *testing.T) { }, }, }, + { + name: "config_signoff_off", + args: args{ + state: func(s *commit.State) { + s.Config.Commit.Signoff = false + }, + model: func(m ui.Model) ui.Model { + m, _ = ToModel(m.Update(nil)) + return m + }, + }, + }, + { + name: "config_signoff_on", + args: args{ + state: func(s *commit.State) { + s.Config.Commit.Signoff = true + }, + model: func(m ui.Model) ui.Model { + m, _ = ToModel(m.Update(nil)) + return m + }, + }, + }, } for _, tt := range tests {