Skip to content

Commit

Permalink
✨ Add configuration for terminal compatibility
Browse files Browse the repository at this point in the history
Allow setting the terminal compatibility to handle emoji width
problems.

Further changes are necessary to support additional terminals as well
as resolve layout issues with borders.
  • Loading branch information
mikelorant committed Jan 27, 2023
1 parent 53026e3 commit f4d66b6
Show file tree
Hide file tree
Showing 5 changed files with 25 additions and 20 deletions.
5 changes: 5 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -148,6 +148,11 @@ view:
# Default: adaptive
colour: adaptive

# Terminal compatibility.
# Values: default, ttyd
# Default: default
compatibility: default

commit:
# Emoji format in commit.
# Values: shortcode, character
Expand Down
24 changes: 15 additions & 9 deletions internal/ui/header/emoji.go
Original file line number Diff line number Diff line change
Expand Up @@ -2,20 +2,17 @@ package header

import (
"fmt"
"os"

"github.com/charmbracelet/bubbles/list"
"github.com/mattn/go-runewidth"
"github.com/mikelorant/committed/internal/config"
"github.com/mikelorant/committed/internal/emoji"
"github.com/mikelorant/committed/internal/fuzzy"
)

const (
ttydTerminal = "ttyd"
)

type listItem struct {
emoji emoji.Emoji
emoji emoji.Emoji
compatibility config.Compatibility
}

type fuzzyItem struct {
Expand All @@ -25,8 +22,8 @@ type fuzzyItem struct {
func (i listItem) Title() string {
var space string

switch os.Getenv("COMMITTED_TERMINAL") {
case ttydTerminal:
switch i.compatibility {
case config.CompatibilityTtyd:
space = " "
default:
if runewidth.StringWidth(i.emoji.Character) == 1 {
Expand All @@ -51,11 +48,20 @@ func (i fuzzyItem) Terms() []string {
}
}

func castToListItems(emojis []emoji.Emoji) []list.Item {
func WithCompatibility(c config.Compatibility) func(*listItem) {
return func(i *listItem) {
i.compatibility = c
}
}

func castToListItems(emojis []emoji.Emoji, opts ...func(*listItem)) []list.Item {
res := make([]list.Item, len(emojis))
for i, e := range emojis {
var item listItem
item.emoji = e
for _, o := range opts {
o(&item)
}
res[i] = item
}

Expand Down
5 changes: 3 additions & 2 deletions internal/ui/header/header.go
Original file line number Diff line number Diff line change
Expand Up @@ -58,7 +58,7 @@ func New(state *commit.State) Model {
styles: defaultStyles(state.Theme),
summaryInput: summaryInput(state),
filterList: filterlist.New(
castToListItems(state.Emojis.Emojis),
castToListItems(state.Emojis.Emojis, WithCompatibility(state.Config.View.Compatibility)),
filterPromptText,
filterHeight,
state,
Expand Down Expand Up @@ -136,7 +136,8 @@ func (m Model) Update(msg tea.Msg) (tea.Model, tea.Cmd) {

items := make([]list.Item, len(ranks))
for i, rank := range ranks {
items[i] = castToListItems(m.Emojis)[rank]
compat := WithCompatibility(m.state.Config.View.Compatibility)
items[i] = castToListItems(m.Emojis, compat)[rank]
}
m.filterList.SetItems(items)
}
Expand Down
11 changes: 2 additions & 9 deletions internal/ui/header/header_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -17,7 +17,6 @@ import (

func TestModel(t *testing.T) {
type args struct {
env map[string]string
state func(c *commit.State)
model func(m header.Model) header.Model
}
Expand Down Expand Up @@ -174,14 +173,12 @@ func TestModel(t *testing.T) {
},
},
{
name: "expand_emojis_env",
name: "expand_emojis_ttyd",
args: args{
env: map[string]string{
"COMMITTED_TERMINAL": "ttyd",
},
state: func(c *commit.State) {
c.Emojis = emoji.New()
c.Repository.Head.Message = "summary\n\nbody"
c.Config.View.Compatibility = config.CompatibilityTtyd
},
model: func(m header.Model) header.Model {
m.Focus()
Expand Down Expand Up @@ -762,10 +759,6 @@ func TestModel(t *testing.T) {

for _, tt := range tests {
t.Run(tt.name, func(t *testing.T) {
for k, v := range tt.args.env {
t.Setenv(k, v)
}

c := testState()
if tt.args.state != nil {
tt.args.state(&c)
Expand Down

0 comments on commit f4d66b6

Please sign in to comment.