8000 Adding icon customization for database, schema, table and view by rkgarcia · Pull Request #383 · danvergara/dblab · GitHub
[go: up one dir, main page]

Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
7 changes: 6 additions & 1 deletion .dblab.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -70,7 +70,12 @@ database:
ssh-key-file: "/path/to/ssh/key.pem"
ssh-key-pass: "hiuwiewnc092"
limit: 50
theme: 'nord'
theme: "nord"
icons:
database: "\ueace"
schema: "\ued75"
table: "\uebb7"
view: "\uea70"
keybindings:
help: '?'
quit: 'ctrl+c'
Expand Down
29 changes: 29 additions & 0 deletions pkg/bubbletui/icons.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,29 @@
package bubbletui

import (
"github.com/danvergara/dblab/pkg/config"
)

var icons map[string]string

func init() {
icons = config.GetIcons()
}

func getIcon(icon string) string {
if v, ok := icons[icon]; ok {
return v
} else {
switch icon {
case "database":
return "⛃"
case "schema":
return "📁"
case "table":
return "📋"
case "view":
return "📑"
}
return ""
}
}
16 changes: 8 additions & 8 deletions pkg/bubbletui/sidebar.go
Original file line number Diff line number Diff line change
Expand Up @@ -62,7 +62,7 @@ func (d DBGraphTreeBuilderProvider) ID(do *client.DBNode) string {
func (d *DBGraphTreeBuilderProvider) Name(do *client.DBNode) string {
return do.Name
}
func (p *DBGraphTreeBuilderProvider) Children(do *client.DBNode) []*client.DBNode {
func (d *DBGraphTreeBuilderProvider) Children(do *client.DBNode) []*client.DBNode {
return do.Children
}

Expand Down Expand Up @@ -91,7 +91,7 @@ func NewSidebarViewport(ctx context.Context, c *client.Client, km keys.SidebarKe
return SidebarViewport{}, err
}

tree, err := treeview.NewTreeFromNestedData[*client.DBNode](
tree, err := treeview.NewTreeFromNestedData(
ctx,
[]*client.DBNode{root},
&DBGraphTreeBuilderProvider{},
Expand Down Expand Up @@ -279,7 +279,7 @@ func (s *SidebarViewport) updateGraph() tea.Cmd {
return updateGraphErrMsg{err}
}

tree, err := treeview.NewTreeFromNestedData[*client.DBNode](
tree, err := treeview.NewTreeFromNestedData(
ctx,
[]*client.DBNode{root},
&DBGraphTreeBuilderProvider{},
Expand All @@ -298,12 +298,12 @@ func (s *SidebarViewport) updateGraph() tea.Cmd {

func createCyberpunkProvider(textForeground, focusedTitleForeground, focusedTitleBackground, focusedTextBorderForeground color.Color) *treeview.DefaultNodeProvider[*client.DBNode] {
// Icons for database objects.
databaseIconRule := treeview.WithIconRule(dbObjectHasType("database"), "⛃")
schemaIconRule := treeview.WithIconRule(dbObjectHasType("schema"), "📁")
tableIconRule := treeview.WithIconRule(dbObjectHasType("table"), "📋")
viewIconRule := treeview.WithIconRule(dbObjectHasType("view"), "📑")
databaseIconRule := treeview.WithIconRule(dbObjectHasType("database"), getIcon("database"))
schemaIconRule := treeview.WithIconRule(dbObjectHasType("schema"), getIcon("schema"))
tableIconRule := treeview.WithIconRule(dbObjectHasType("table"), getIcon("table"))
viewIconRule := treeview.WithIconRule(dbObjectHasType("view"), getIcon("view"))

return treeview.NewDefaultNodeProvider[*client.DBNode](
return treeview.NewDefaultNodeProvider(
databaseIconRule,
schemaIconRule,
tableIconRule,
Expand Down
5 changes: 5 additions & 0 deletions pkg/config/.dblab.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -64,6 +64,11 @@ database:
ssh-key-file: "/path/to/ssh/key.pem"
ssh-key-pass: "hiuwiewnc092"
limit: 50
icons:
database: "D"
schema: "S"
table: "T"
view: "V"
keybindings:
help: '?'
quit: 'ctrl+c'
Expand Down
28 changes: 27 additions & 1 deletion pkg/config/config.go
Original file line number Diff line number Diff line change
Expand Up @@ -25,13 +25,18 @@ type Config struct {
Driver string
Limit uint `fig:"limit" default:"100"`
// Application theme.
Theme string `fig:"theme"`
Theme string `fig:"theme"`
Icons map[string]string `fig:"icons"`
}

type KeyMapConfig struct {
KeyBindings KeyBindings
}

type iconsConfig struct {
Icons map[string]string `fig:"icons"`
}

type Profile struct {
Name string `fig:"name"`

Expand Down Expand Up @@ -224,6 +229,27 @@ func Init(configName string) (command.Options, error) {
return opts, nil
}

// GetIcons reads the icons field from the config file.
func GetIcons() map[string]string {
Copy link
Copy Markdown
Owner

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Can we make this more optional and put it behind a cli flag? I don't want partial config reads enabled by default, see how we manage the keybindings.

var icons iconsConfig

configDir, err := os.UserConfigDir()
if err != nil {
return icons.Icons
}

home, err := os.UserHomeDir()
if err != nil {
return icons.Icons
}

if err := fig.Load(&icons, fig.File(".dblab.yaml"), fig.Dirs(".", home, configDir)); err != nil {
return icons.Icons
}

return icons.Icons
}

// GetKeyMap reads the keybindings field from the config file and populates
// the KeyMapConf 8BD0 ig struct and returns the result.
func GetKeyMap() (KeyMapConfig, error) {
Expand Down
8 changes: 8 additions & 0 deletions pkg/config/config_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -177,6 +177,14 @@ func TestInit(t *testing.T) {
}
}

func TestIcons(t *testing.T) {
icons := config.GetIcons()
assert.Equal(t, icons["database"], "D")
assert.Equal(t, icons["schema"], "S")
assert.Equal(t, icons["table"], "T")
assert.Equal(t, icons["view"], "V")
}

func TestSetupKeybindings(t *testing.T) {
kb, err := config.GetKeyMap()
assert.NoError(t, err)
Expand Down
Loading
0