Skip to content

Commit

Permalink
✨ Show default user for footer and header
Browse files Browse the repository at this point in the history
Multiple default users can be set in the config file. These users are
prioritised when selecting users. The first default user found in the
configuration file becomes default in the info and footer component.
  • Loading branch information
mikelorant committed Jan 29, 2023
1 parent 683d354 commit 9bb15f6
Show file tree
Hide file tree
Showing 9 changed files with 109 additions and 37 deletions.
2 changes: 1 addition & 1 deletion internal/ui/info/info.go
Original file line number Diff line number Diff line change
Expand Up @@ -40,7 +40,7 @@ const (
)

func New(state *commit.State) Model {
authors := concatSlice(state.Repository.Users, state.Config.Authors)
authors := commit.SortUsersByDefault(concatSlice(state.Repository.Users, state.Config.Authors)...)

if len(authors) == 0 {
authors = []repository.User{{}}
Expand Down
98 changes: 70 additions & 28 deletions internal/ui/info/info_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -121,7 +121,7 @@ func TestModel(t *testing.T) {
name: "multiple_users",
args: args{
state: func(c *commit.State) {
c.Repository.Users = testStateUsers(2)
c.Repository.Users = testRepositoryUsers(2)
},
model: func(m info.Model) info.Model {
m.Focus()
Expand All @@ -135,7 +135,7 @@ func TestModel(t *testing.T) {
name: "multiple_users_selected",
args: args{
state: func(c *commit.State) {
c.Repository.Users = testStateUsers(2)
c.Repository.Users = testRepositoryUsers(2)
},
model: func(m info.Model) info.Model {
m.Focus()
Expand All @@ -147,24 +147,21 @@ func TestModel(t *testing.T) {
},
want: want{
model: func(m info.Model) {
assert.Equal(t, m.Author, testStateUsers(2)[1])
assert.Equal(t, m.Author, testRepositoryUsers(2)[1])
},
},
},
{
name: "multiple_users_filtered",
args: args{
state: func(c *commit.State) {
c.Repository.Users = []repository.User{
testStateUsers(3)[0],
testStateUsers(3)[2],
}
c.Repository.Users = testRepositoryUsers(2)
},
model: func(m info.Model) info.Model {
m.Focus()
m.Expand = true
m, _ = info.ToModel(m.Update(nil))
m, _ = info.ToModel(uitest.SendString(m, "test"), nil)
m, _ = info.ToModel(uitest.SendString(m, "example.org"), nil)
return m
},
},
Expand All @@ -173,12 +170,7 @@ func TestModel(t *testing.T) {
name: "users_mixed",
args: args{
state: func(c *commit.State) {
c.Repository.Users = []repository.User{
testStateUsers(3)[0],
}
c.Config.Authors = []repository.User{
testStateUsers(3)[1],
}
c.Config.Authors = testConfigUsers(1)
},
model: func(m info.Model) info.Model {
m.Focus()
Expand All @@ -192,13 +184,8 @@ func TestModel(t *testing.T) {
name: "users_mixed_multiple",
args: args{
state: func(c *commit.State) {
c.Repository.Users = []repository.User{
testStateUsers(3)[0],
}
c.Config.Authors = []repository.User{
testStateUsers(3)[1],
testStateUsers(3)[2],
}
c.Repository.Users = testRepositoryUsers(2)
c.Config.Authors = testConfigUsers(2)
},
model: func(m info.Model) info.Model {
m.Focus()
Expand All @@ -224,10 +211,7 @@ func TestModel(t *testing.T) {
args: args{
state: func(c *commit.State) {
c.Repository.Users = nil
c.Config.Authors = []repository.User{
testStateUsers(3)[0],
testStateUsers(3)[1],
}
c.Config.Authors = testConfigUsers(1)
},
model: func(m info.Model) info.Model {
m.Focus()
Expand Down Expand Up @@ -267,6 +251,55 @@ func TestModel(t *testing.T) {
},
},
},
{
name: "multiple_users_config_default",
args: args{
state: func(c *commit.State) {
c.Repository.Users = nil
c.Config.Authors = testConfigUsers(2)
c.Config.Authors[1].Default = true
},
model: func(m info.Model) info.Model {
m.Focus()
m.Expand = true
m, _ = info.ToModel(m.Update(nil))
return m
},
},
},
{
name: "multiple_users_repository_config_default",
args: args{
state: func(c *commit.State) {
c.Repository.Users = testRepositoryUsers(1)
c.Config.Authors = testConfigUsers(1)
c.Config.Authors[0].Default = true
},
model: func(m info.Model) info.Model {
m.Focus()
m.Expand = true
m, _ = info.ToModel(m.Update(nil))
return m
},
},
},
{
name: "multiple_users_repository_config_default_multiple",
args: args{
state: func(c *commit.State) {
c.Repository.Users = testRepositoryUsers(2)
c.Config.Authors = testConfigUsers(2)
c.Config.Authors[0].Default = true
c.Config.Authors[1].Default = true
},
model: func(m info.Model) info.Model {
m.Focus()
m.Expand = true
m, _ = info.ToModel(m.Update(nil))
return m
},
},
},
}

for _, tt := range tests {
Expand Down Expand Up @@ -301,7 +334,7 @@ func testState() commit.State {
Branch: repository.Branch{
Local: "master",
},
Users: testStateUsers(1),
Users: testRepositoryUsers(1),
Head: repository.Head{
Hash: "1",
When: time.Date(2022, time.January, 1, 1, 0, 0, 0, time.UTC),
Expand All @@ -314,7 +347,7 @@ func testState() commit.State {
}
}

func testStateUsers(n int) []repository.User {
func testRepositoryUsers(n int) []repository.User {
return []repository.User{
{
Name: "John Doe",
Expand All @@ -324,9 +357,18 @@ func testStateUsers(n int) []repository.User {
Name: "John Doe",
Email: "jdoe@example.org",
},
}[0:n]
}

func testConfigUsers(n int) []repository.User {
return []repository.User{
{
Name: "John Doe",
Email: "jd@example.net",
},
{
Name: "John Doe",
Email: "jdoe@test",
Email: "j@example.id",
},
}[0:n]
}
6 changes: 3 additions & 3 deletions internal/ui/info/testdata/config_user_only.golden
Original file line number Diff line number Diff line change
@@ -1,10 +1,10 @@
commit 1 (HEAD -> master)
author: John Doe <john.doe@example.com>
author: John Doe <jd@example.net>
date: Sat Jan 1 01:00:00 2022 +0000

┌──────────────────────────────────────────────────────────────────────────┐
│? Choose an author: ● │
│❯ John Doe <john.doe@example.com>
John Doe <jdoe@example.org>
│❯ John Doe <jd@example.net>
│ │
└──────────────────────────────────────────────────────────────────────────┘
10 changes: 10 additions & 0 deletions internal/ui/info/testdata/multiple_users_config_default.golden
Original file line number Diff line number Diff line change
@@ -0,0 +1,10 @@
commit 1 (HEAD -> master)
author: John Doe <j@example.id>
date: Sat Jan 1 01:00:00 2022 +0000

┌──────────────────────────────────────────────────────────────────────────┐
│? Choose an author: ● │
│❯ John Doe <j@example.id> │
│ John Doe <jd@example.net> │
│ │
└──────────────────────────────────────────────────────────────────────────┘
4 changes: 2 additions & 2 deletions internal/ui/info/testdata/multiple_users_filtered.golden
Original file line number Diff line number Diff line change
Expand Up @@ -3,8 +3,8 @@ author: John Doe <john.doe@example.com>
date: Sat Jan 1 01:00:00 2022 +0000

┌──────────────────────────────────────────────────────────────────────────┐
│? Choose an author: test ● │
│❯ John Doe <jdoe@test>
│? Choose an author: example.org ● │
│❯ John Doe <jdoe@example.org>
│ │
│ │
└──────────────────────────────────────────────────────────────────────────┘
Original file line number Diff line number Diff line change
@@ -0,0 +1,10 @@
commit 1 (HEAD -> master)
author: John Doe <jd@example.net>
date: Sat Jan 1 01:00:00 2022 +0000

┌──────────────────────────────────────────────────────────────────────────┐
│? Choose an author: ● │
│❯ John Doe <jd@example.net> │
│ John Doe <john.doe@example.com> │
│ │
└──────────────────────────────────────────────────────────────────────────┘
Original file line number Diff line number Diff line change
@@ -0,0 +1,10 @@
commit 1 (HEAD -> master)
author: John Doe <jd@example.net>
date: Sat Jan 1 01:00:00 2022 +0000

┌──────────────────────────────────────────────────────────────────────────┐
│? Choose an author: ● │
│❯ John Doe <jd@example.net> ○ │
│ John Doe <j@example.id> │
│ John Doe <john.doe@example.com> │
└──────────────────────────────────────────────────────────────────────────┘
2 changes: 1 addition & 1 deletion internal/ui/info/testdata/users_mixed.golden
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,6 @@ date: Sat Jan 1 01:00:00 2022 +0000
┌──────────────────────────────────────────────────────────────────────────┐
│? Choose an author: ● │
│❯ John Doe <john.doe@example.com> │
│ John Doe <jdoe@example.org>
│ John Doe <jd@example.net>
│ │
└──────────────────────────────────────────────────────────────────────────┘
4 changes: 2 additions & 2 deletions internal/ui/info/testdata/users_mixed_multiple.golden
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@ date: Sat Jan 1 01:00:00 2022 +0000

┌──────────────────────────────────────────────────────────────────────────┐
│? Choose an author: ● │
│❯ John Doe <john.doe@example.com>
│❯ John Doe <john.doe@example.com>
│ John Doe <jdoe@example.org> │
│ John Doe <jdoe@test>
│ John Doe <jd@example.net>
└──────────────────────────────────────────────────────────────────────────┘

0 comments on commit 9bb15f6

Please sign in to comment.