Skip to content

Enhance Keybinding Display #3706

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Open
wants to merge 8 commits into
base: master
Choose a base branch
from
1 change: 1 addition & 0 deletions internal/config/settings.go
Original file line number Diff line number Diff line change
Expand Up @@ -65,6 +65,7 @@ var defaultCommonSettings = map[string]interface{}{
"fastdirty": false,
"fileformat": defaultFileFormat(),
"filetype": "unknown",
"helpactions": "Quit,Save,OpenFile,ToggleHelp,CommandMode,Cut,Find,Undo,Redo,SelectAll,Duplicate,AddTab",
"hlsearch": false,
"hltaberrors": false,
"hltrailingws": false,
Expand Down
75 changes: 65 additions & 10 deletions internal/display/infowindow.go
Original file line number Diff line number Diff line change
@@ -1,6 +1,10 @@
package display

import (
"regexp"
"slices"
"strings"

runewidth "github.com/mattn/go-runewidth"
"github.com/micro-editor/tcell/v2"
"github.com/zyedidia/micro/v2/internal/buffer"
Expand All @@ -14,7 +18,8 @@ type InfoWindow struct {
*info.InfoBuf
*View

hscroll int
hscroll int
keydisplay []string
}

func (i *InfoWindow) errStyle() tcell.Style {
Expand Down Expand Up @@ -44,6 +49,8 @@ func NewInfoWindow(b *info.InfoBuf) *InfoWindow {
iw.InfoBuf = b
iw.View = new(View)

iw.keydisplay = getKeyDisplay()

iw.Width, iw.Y = screen.Screen.Size()
iw.Y--

Expand Down Expand Up @@ -178,20 +185,68 @@ func (i *InfoWindow) displayBuffer() {
}
}

var keydisplay = []string{"^Q Quit, ^S Save, ^O Open, ^G Help, ^E Command Bar, ^K Cut Line", "^F Find, ^Z Undo, ^Y Redo, ^A Select All, ^D Duplicate Line, ^T New Tab"}

func (i *InfoWindow) displayKeyMenu() {
// TODO: maybe make this based on the actual keybindings

for y := 0; y < len(keydisplay); y++ {
for y := 0; y < len(i.keydisplay); y++ {
for x := 0; x < i.Width; x++ {
if x < len(keydisplay[y]) {
screen.SetContent(x, i.Y-len(keydisplay)+y, rune(keydisplay[y][x]), nil, i.defStyle())
if x < len(i.keydisplay[y]) {
screen.SetContent(x, i.Y-len(i.keydisplay)+y, rune(i.keydisplay[y][x]), nil, i.defStyle())
} else {
screen.SetContent(x, i.Y-len(keydisplay)+y, ' ', nil, i.defStyle())
screen.SetContent(x, i.Y-len(i.keydisplay)+y, ' ', nil, i.defStyle())
}
}
}
}

func getKeyDisplay() []string {
keybinds := strings.Split(config.GlobalSettings["helpactions"].(string), ",")
mid := len(keybinds) / 2

return []string{
getKeyBinds(keybinds[:mid]),
getKeyBinds(keybinds[mid:]),
}
}

func getKeyBinds(actions []string) string {
keys := make(map[string][]string, 0)
re := regexp.MustCompile(`[&|,]+`)

for key, binding := range config.Bindings["buffer"] {
for _, action := range actions {
if slices.Index(re.Split(binding, -1), action) != -1 {
k := key

if strings.Contains(key, "Ctrl-") {
k = "^" + key[len(key)-1:]
}

keys[action] = append(keys[action], k)
}
}
}

var sb strings.Builder

for i, action := range actions {
slices.Sort(keys[action])

// TODO Those hardcoded keys could also be editable in the settings file
sb.WriteString(action + ": ")

for j, key := range keys[action] {
sb.WriteString(key)

if len(keys[action])-1 != j {
sb.WriteString(", ")
}
}

if len(actions)-1 != i {
sb.WriteString(" | ")
}
}

return sb.String()
}

func (i *InfoWindow) totalSize() int {
Expand Down Expand Up @@ -267,7 +322,7 @@ func (i *InfoWindow) Display() {
}
keymenuOffset := 0
if config.GetGlobalOption("keymenu").(bool) {
keymenuOffset = len(keydisplay)
keymenuOffset = len(i.keydisplay)
}

draw := func(r rune, s tcell.Style) {
Expand Down