Skip to content

Commit

Permalink
feat: implement AddEntry
Browse files Browse the repository at this point in the history
  • Loading branch information
kencx committed Aug 2, 2022
1 parent 8c90826 commit b916747
Show file tree
Hide file tree
Showing 3 changed files with 120 additions and 3 deletions.
61 changes: 60 additions & 1 deletion config/model.go
Original file line number Diff line number Diff line change
Expand Up @@ -15,7 +15,11 @@ type App struct {
Keybinds []KeyBind `yaml:"keybinds"`
}

type Apps []App
type Apps []*App

func (a App) String() string {
return fmt.Sprintf("App{name=%s,prefix=%s,keybinds=%v}", a.Name, a.Prefix, a.Keybinds)
}

type KeyBind struct {
Name string `yaml:"name"`
Expand Down Expand Up @@ -82,3 +86,58 @@ func generateDefaultKeyb(path string) Apps {
}},
}}
}

func AddEntry(path, appName, kbName, kbKey string, kbIgnorePrefix bool) error {

// load existing struct from filepath
apps, err := ParseApps(path)
if err != nil {
return err
}
apps.addOrUpdate(appName, kbName, kbKey, kbIgnorePrefix)

// rewrite file
data, err := yaml.Marshal(apps)
if err != nil {
return fmt.Errorf("failed to marshal entry: %w", err)
}

if err := os.WriteFile(path, data, 0644); err != nil {
return fmt.Errorf("failed to write keyb file: %w", err)
}

return nil
}

func (apps Apps) addOrUpdate(appName string, name, key string, ignorePrefix bool) Apps {
newKeyBind := KeyBind{
Name: name,
Key: key,
IgnorePrefix: ignorePrefix,
}

if !apps.exist(appName) {
a := App{
Name: appName,
Keybinds: []KeyBind{newKeyBind},
}
apps = append(apps, &a)

} else {
for _, app := range apps {
if appName == app.Name {
app.Keybinds = append(app.Keybinds, newKeyBind)
}
}
}
return apps
}

func (apps Apps) exist(appName string) bool {
for _, app := range apps {
if appName == app.Name {
return true
}
}
return false
}
58 changes: 58 additions & 0 deletions config/model_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -51,3 +51,61 @@ func TestParseApps(t *testing.T) {
}
})
}

func TestAddOrUpdate(t *testing.T) {

t.Run("update existing", func(t *testing.T) {
apps := Apps{{
Name: "test",
Keybinds: []KeyBind{{
Name: "foo",
Key: "bar",
}},
}}
want := Apps{{
Name: "test",
Keybinds: []KeyBind{{
Name: "foo",
Key: "bar",
}, {
Name: "addFoo",
Key: "addBar",
}},
}}
got := apps.addOrUpdate("test", "addFoo", "addBar", false)

if !reflect.DeepEqual(got, want) {
t.Errorf("got %v, want %v", got, want)
}
})

t.Run("add new", func(t *testing.T) {
apps := Apps{{
Name: "test",
Keybinds: []KeyBind{{
Name: "foo",
Key: "bar",
}},
}}
want := Apps{
{
Name: "test",
Keybinds: []KeyBind{{
Name: "foo",
Key: "bar",
}},
}, {
Name: "new",
Keybinds: []KeyBind{{
Name: "addFoo",
Key: "addBar",
}},
},
}
got := apps.addOrUpdate("new", "addFoo", "addBar", false)

if !reflect.DeepEqual(got, want) {
t.Errorf("got %v, want %v", got, want)
}
})
}
4 changes: 2 additions & 2 deletions ui/ui.go
Original file line number Diff line number Diff line change
Expand Up @@ -34,11 +34,11 @@ func createParentTable(a config.Apps, sortKeys bool) *table.Model {
return a[i].Name < a[j].Name
})

parent := appToTable(a[0].Name, a[0], sortKeys)
parent := appToTable(a[0].Name, *a[0], sortKeys)

if len(a) > 1 {
for _, k := range a[1:] {
child := appToTable(k.Name, k, sortKeys)
child := appToTable(k.Name, *k, sortKeys)
parent.Join(child)
}
}
Expand Down

0 comments on commit b916747

Please sign in to comment.