Skip to content

Commit

Permalink
feat: export to json and yaml
Browse files Browse the repository at this point in the history
  • Loading branch information
kencx committed May 20, 2024
1 parent d996d79 commit 05083b7
Show file tree
Hide file tree
Showing 6 changed files with 69 additions and 11 deletions.
2 changes: 1 addition & 1 deletion README.md
Original file line number Diff line number Diff line change
Expand Up @@ -168,7 +168,7 @@ See [config](examples/config/README.md) for all configuration options.

## Contributing

keyb requires Go 1.21. Bug reports, feature requests and PRs are very welcome.
keyb requires Go 1.22. Bug reports, feature requests and PRs are very welcome.

## Similar Tools

Expand Down
1 change: 0 additions & 1 deletion config/config.go
Original file line number Diff line number Diff line change
Expand Up @@ -132,7 +132,6 @@ var DefaultConfig = &Config{
}

// Read configuration and keyb file from flags, default path.
// If config directory and/or files do not exist, create them.
func Parse(flagCPath, flagKPath string) (Apps, *Config, error) {
xdgConfigDir, err := getXDGConfigDir()
if err != nil {
Expand Down
12 changes: 6 additions & 6 deletions config/model.go
Original file line number Diff line number Diff line change
Expand Up @@ -9,9 +9,9 @@ import (
)

type App struct {
Name string `yaml:"name"`
Prefix string `yaml:"prefix,omitempty"`
Keybinds []KeyBind `yaml:"keybinds"`
Prefix string `yaml:"prefix,omitempty" json:"prefix,omitempty"`
Name string `yaml:"name" json:"name"`
Keybinds []KeyBind `yaml:"keybinds" json:"keybinds"`
}

type Apps []*App
Expand All @@ -21,12 +21,12 @@ func (a App) String() string {
}

type KeyBind struct {
Name string `yaml:"name"`
Key string `yaml:"key"`
Name string `yaml:"name" json:"name"`
Key string `yaml:"key" json:"key"`

// ignore prefix defaults to false
// so user can choose to ignore prefix for a specific kb
IgnorePrefix bool `yaml:"ignore_prefix,omitempty"`
IgnorePrefix bool `yaml:"ignore_prefix,omitempty" json:"ignore_prefix,omitempty"`
}

func AddEntry(path, binding string, kbIgnorePrefix bool) error {
Expand Down
27 changes: 25 additions & 2 deletions output/output.go
Original file line number Diff line number Diff line change
@@ -1,19 +1,42 @@
package output

import (
"encoding/json"
"fmt"
"os"
"path/filepath"

"github.com/kencx/keyb/ui"
"gopkg.in/yaml.v2"
)

func ToFile(m *ui.Model, path string) error {
output := m.List.UnstyledString()
var (
output []byte
err error
)

path = os.ExpandEnv(path)
if err := os.WriteFile(path, []byte(output), 0664); err != nil {
ext := filepath.Ext(path)

switch ext {
case ".json":
output, err = json.Marshal(m.Apps)
if err != nil {
return fmt.Errorf("failed to marshal to json: %w", err)
}
case ".yml", ".yaml":
output, err = yaml.Marshal(m.Apps)
if err != nil {
return fmt.Errorf("failed to marshal to yaml: %w", err)
}
default:
output = []byte(m.List.UnstyledString())
}
if err := os.WriteFile(path, output, 0664); err != nil {
return fmt.Errorf("failed to write to file: %w", err)
}

return nil
}

Expand Down
36 changes: 35 additions & 1 deletion output/output_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,8 @@ package output
import (
"io"
"os"
"path/filepath"
"reflect"
"testing"

"github.com/kencx/keyb/config"
Expand All @@ -14,9 +16,41 @@ import (
var (
testTable = table.New([]*table.Row{table.NewHeading("foo"), {Text: "bar"}})
testConfig = &config.Config{}
m = &ui.Model{List: list.New(testTable, testConfig)}
testApps = &config.Apps{
&config.App{
Name: "foo",
Prefix: "bar",
Keybinds: []config.KeyBind{
{
Name: "key foo",
Key: "key bar",
},
},
},
}
m = &ui.Model{List: list.New(testTable, testConfig), Apps: testApps}
)

func TestToJson(t *testing.T) {
tempDir := t.TempDir()
path := filepath.Join(tempDir, "test.json")

err := ToFile(m, path)
if err != nil {
t.Fatal(err)
}

got, err := os.ReadFile(path)
if err != nil {
t.Fatal(err)
}

want := []byte(`[{"prefix":"bar","name":"foo","keybinds":[{"name":"key foo","key":"key bar"}]}]`)
if !reflect.DeepEqual(got, want) {
t.Fatalf("got %s, want %s", got, want)
}
}

func TestToStdout(t *testing.T) {
rescueStdout := os.Stdout
r, w, _ := os.Pipe()
Expand Down
2 changes: 2 additions & 0 deletions ui/ui.go
Original file line number Diff line number Diff line change
Expand Up @@ -13,13 +13,15 @@ import (

type Model struct {
List list.Model
Apps *config.Apps
}

func NewModel(a config.Apps, config *config.Config) *Model {

table := createParentTable(a, config.SortKeys)
return &Model{
List: list.New(table, config),
Apps: &a,
}
}

Expand Down

0 comments on commit 05083b7

Please sign in to comment.