feat: center key when draw

main
sudo pacman -Syu 2023-07-23 06:07:23 +07:00
parent 3add32406c
commit a9bd6bcd8a
2 changed files with 40 additions and 56 deletions

View File

@ -9,32 +9,22 @@ const (
scaleY = 3 scaleY = 3
) )
// https://github.com/qmk/qmk_firmware/blob/master/docs/keycodes.md
var mapSpecialKey = map[string]string{ var mapSpecialKey = map[string]string{
"KC_TRNS": " ", "KC_TRNS": " ",
"KC_NO": " ", "KC_NO": " ",
"KC_LGUI": "⌘", "KC_ENT": "ENTER",
"KC_RGUI": "⌘", "KC_BSPC": "BACKSPACE",
"KC_LALT": "⌥", "KC_SPC": "SPACE",
"KC_RALT": "⌥", "KC_CAPS": "CAPSLOCK",
"KC_LSFT": "⇧", "KC_LALT": "ALT",
"KC_RSFT": "⇧", "KC_RALT": "ALT",
"KC_TAB": "↹ ", "KC_LGUI": "CMD",
"KC_CAPS": "⇪", "KC_RGUI": "CMD",
"QK_GESC": "⎋", "KC_LSFT": "SHIFT",
"CTL_T(KC_ESC)": "⌃", "KC_RSFT": "SHIFT",
} // Custom
"CTL_T(KC_ESC)": "CTRL ESC",
var mapSpecialKeyWidthLimit = map[string]int{
"KC_LGUI": 1,
"KC_RGUI": 1,
"KC_LALT": 1,
"KC_RALT": 1,
"KC_LSFT": 1,
"KC_RSFT": 1,
"KC_TAB": 1,
"KC_CAPS": 1,
"QK_GESC": 1,
"CTL_T(KC_ESC)": 1,
} }
func Draw( func Draw(
@ -90,7 +80,6 @@ func Draw(
// Fill layout // Fill layout
count := 0 count := 0
for _, key := range keys { for _, key := range keys {
originalKeyStr := layer[count]
keyStr := layer[count] keyStr := layer[count]
// Process keyStr // Process keyStr
@ -102,15 +91,20 @@ func Draw(
keyStr = strings.TrimPrefix(keyStr, "QK_") keyStr = strings.TrimPrefix(keyStr, "QK_")
} }
keyWidthLimit, existKeyWidthLimit := mapSpecialKeyWidthLimit[originalKeyStr] // Always have 1 padding left and 1 padding right
if !existKeyWidthLimit { if len(keyStr) > key.NewW-2 {
keyWidthLimit = key.NewW - 3 keyStr = keyStr[:key.NewW-2]
}
if len(keyStr) > keyWidthLimit { // New padding to center key
keyStr = keyStr[:keyWidthLimit] padding := (key.NewW - len(keyStr)) / 2
} else {
keyWidthLimit = len(keyStr) keyWidthLimit := key.NewW - 2*padding
}
if len(keyStr) > keyWidthLimit {
keyStr = keyStr[:keyWidthLimit]
} else {
keyWidthLimit = len(keyStr)
} }
for i := key.NewY; i < key.NewY+key.NewH; i++ { for i := key.NewY; i < key.NewY+key.NewH; i++ {
@ -125,18 +119,8 @@ func Draw(
// Write key in the middle // Write key in the middle
if j == key.NewX { if j == key.NewX {
table[i][j] = "|" table[i][j] = "|"
} else if j > key.NewX+1 && j < key.NewX+keyWidthLimit+2 { } else if j >= key.NewX+padding && j < key.NewX+keyWidthLimit+padding {
if existKeyWidthLimit { table[i][j] = string(keyStr[j-key.NewX-padding])
// Special key can be Unicode
// So ignore get single character of keyStr
if j == key.NewX+2 {
table[i][j] = keyStr
} else {
table[i][j] = ""
}
} else {
table[i][j] = string(keyStr[j-key.NewX-2])
}
} else { } else {
table[i][j] = " " table[i][j] = " "
} }

View File

@ -1,25 +1,25 @@
+-------+-------+-------+-------+-------+-------+-------+-------+-------+-------+-------+-------+-------+-------+-------+ +-------+-------+-------+-------+-------+-------+-------+-------+-------+-------+-------+-------+-------+-------+-------+
| ⎋ | 1 | 2 | 3 | 4 | 5 | 6 | 7 | 8 | 9 | 0 | MINS | EQL | BSLS | GRV | | GESC | 1 | 2 | 3 | 4 | 5 | 6 | 7 | 8 | 9 | 0 | MINS | EQL | BSLS | GRV |
+-----------+-------+-------+-------+-------+-------+-------+-------+-------+-------+-------+-------+-------+-----------+ +-----------+-------+-------+-------+-------+-------+-------+-------+-------+-------+-------+-------+-------+-----------+
| ↹ | Q | W | E | R | T | Y | U | I | O | P | LBRC | RBRC | BSPC | | TAB | Q | W | E | R | T | Y | U | I | O | P | LBRC | RBRC |BACKSPACE |
+-------------+-------+-------+-------+-------+-------+-------+-------+-------+-------+-------+-------+-----------------+ +-------------+-------+-------+-------+-------+-------+-------+-------+-------+-------+-------+-------+-----------------+
| | A | S | D | F | G | H | J | K | L | SCLN | QUOT | ENT | | CTRL ESC | A | S | D | F | G | H | J | K | L | SCLN | QUOT | ENTER |
+-----------------+-------+-------+-------+-------+-------+-------+-------+-------+-------+-------+-------------+-------+ +-----------------+-------+-------+-------+-------+-------+-------+-------+-------+-------+-------+-------------+-------+
| ⇧ | Z | X | C | V | B | N | M | COMM | DOT | SLSH | ⇧ | MO(1) | | SHIFT | Z | X | C | V | B | N | M | COMM | DOT | SLSH | SHIFT |MO(1) |
+-----------+-------+-----------+-------------------------------------------------------+-----------+-------+-----------+ +-----------+-------+-----------+-------------------------------------------------------+-----------+-------+-----------+
| | ⌥ | ⌘ | SPC | ⌘ | ⌥ | | | | ALT | CMD | SPACE | CMD | ALT | |
+-----------+-------+-----------+-------------------------------------------------------+-----------+-------+-----------+ +-----------+-------+-----------+-------------------------------------------------------+-----------+-------+-----------+
+-------+-------+-------+-------+-------+-------+-------+-------+-------+-------+-------+-------+-------+-------+-------+ +-------+-------+-------+-------+-------+-------+-------+-------+-------+-------+-------+-------+-------+-------+-------+
| GRV | F1 | F2 | F3 | F4 | F5 | F6 | F7 | F8 | F9 | F10 | F11 | F12 | | | | GRV | F1 | F2 | F3 | F4 | F5 | F6 | F7 | F8 | F9 | F10 | F11 | F12 | | |
+-----------+-------+-------+-------+-------+-------+-------+-------+-------+-------+-------+-------+-------+-----------+ +-----------+-------+-------+-------+-------+-------+-------+-------+-------+-------+-------+-------+-------+-----------+
| | | | | | | | | | | | UP | | | | | | | | | | | | | | | UP | | |
+-------------+-------+-------+-------+-------+-------+-------+-------+-------+-------+-------+-------+-----------------+ +-------------+-------+-------+-------+-------+-------+-------+-------+-------+-------+-------+-------+-----------------+
| | VOLD | VOLU | MUTE | | | | | | | LEFT | RGHT | | | | VOLD | VOLU | MUTE | | | | | | | LEFT | RGHT | |
+-----------------+-------+-------+-------+-------+-------+-------+-------+-------+-------+-------+-------------+-------+ +-----------------+-------+-------+-------+-------+-------+-------+-------+-------+-------+-------+-------------+-------+
| | | | | | | | | | | DOWN | | | | | | | | | | | | | | DOWN | | |
+-----------+-------+-----------+-------------------------------------------------------+-----------+-------+-----------+ +-----------+-------+-----------+-------------------------------------------------------+-----------+-------+-----------+
| | TG(2) | | | | | | | |TG(2) | | | | | |
+-----------+-------+-----------+-------------------------------------------------------+-----------+-------+-----------+ +-----------+-------+-----------+-------------------------------------------------------+-----------+-------+-----------+
+-------+-------+-------+-------+-------+-------+-------+-------+-------+-------+-------+-------+-------+-------+-------+ +-------+-------+-------+-------+-------+-------+-------+-------+-------+-------+-------+-------+-------+-------+-------+
@ -27,10 +27,10 @@
+-----------+-------+-------+-------+-------+-------+-------+-------+-------+-------+-------+-------+-------+-----------+ +-----------+-------+-------+-------+-------+-------+-------+-------+-------+-------+-------+-------+-------+-----------+
| | | | | | | | | | | | | | | | | | | | | | | | | | | | | |
+-------------+-------+-------+-------+-------+-------+-------+-------+-------+-------+-------+-------+-----------------+ +-------------+-------+-------+-------+-------+-------+-------+-------+-------+-------+-------+-------+-----------------+
| ⇪ | RGB_H | RGB_S | RGB_V | | | | | HOME | PGUP | | | | | CAPSLOCK |RGB_HU |RGB_SA |RGB_VA | | | | | HOME | PGUP | | | |
+-----------------+-------+-------+-------+-------+-------+-------+-------+-------+-------+-------+-------------+-------+ +-----------------+-------+-------+-------+-------+-------+-------+-------+-------+-------+-------+-------------+-------+
| | | | | | | | | END | PGDN | | | | | | | | | | | | | END | PGDN | | | |
+-----------+-------+-----------+-------------------------------------------------------+-----------+-------+-----------+ +-----------+-------+-----------+-------------------------------------------------------+-----------+-------+-----------+
| | | | | RGB_TOG | RGB_M | | | | | | | RGB_TOG |RGB_MO | |
+-----------+-------+-----------+-------------------------------------------------------+-----------+-------+-----------+ +-----------+-------+-----------+-------------------------------------------------------+-----------+-------+-----------+