Skip to content

Commit 6f6fbbe

Browse files
committed
[WIP] Proportional font: Rebase to master
+ minor changes
1 parent 417790f commit 6f6fbbe

File tree

5 files changed

+315
-285
lines changed

5 files changed

+315
-285
lines changed

editor/cursor.go

Lines changed: 6 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -165,7 +165,7 @@ func (c *Cursor) drawForeground(p *gui.QPainter, sx, sy, dx, dy float64, text st
165165
charCache := *c.charCache
166166
imagev, err := charCache.get(HlTextKey{
167167
text: text,
168-
fg: c.fg,
168+
fg: *(c.fg),
169169
italic: false,
170170
bold: false,
171171
})
@@ -267,7 +267,7 @@ func (c *Cursor) setCharCache(text string, fg *RGBA, image *gui.QImage) {
267267
c.charCache.set(
268268
HlTextKey{
269269
text: text,
270-
fg: c.fg,
270+
fg: *(c.fg),
271271
italic: false,
272272
bold: false,
273273
},
@@ -505,12 +505,13 @@ func (c *Cursor) updateCursorShape(win *Window) {
505505
}
506506
case "vertical":
507507
c.isTextDraw = true
508-
if (c.font == nil || !c.font.proportional) {
508+
c.horizontalShift = 0
509+
font := win.font
510+
if font == nil || !font.proportional {
509511
width = int(math.Ceil(float64(width) * p))
510512
} else {
511-
width = int(c.font.cellwidth) / 10
513+
width = int(font.cellwidth) / 10
512514
}
513-
c.horizontalShift = 0
514515
default:
515516
c.isTextDraw = true
516517
c.horizontalShift = 0

editor/editor.go

Lines changed: 22 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -3,7 +3,7 @@ package editor
33
import (
44
"context"
55
"fmt"
6-
"io/ioutil"
6+
"io"
77
"log"
88
"os"
99
"os/exec"
@@ -867,31 +867,34 @@ func (e *Editor) setEnvironmentVariables() {
867867
}
868868

869869
// If the OS is MacOS and the application is launched from an .app
870-
if runtime.GOOS == "darwin" && e.ppid == 1 {
870+
if runtime.GOOS == "darwin" && os.Getenv("TERM") == "" {
871871
shell := os.Getenv("SHELL")
872872
if shell == "" {
873-
shell = os.Getenv("/bin/bash")
873+
shell = "/bin/zsh" // fallback
874874
}
875-
cmd := exec.Command(shell, "-l", "-c", "env", "-i")
876-
// cmd := exec.Command("printenv")
875+
876+
cmd := exec.Command(shell, "-l", "-c", "env")
877877
stdout, err := cmd.StdoutPipe()
878878
if err != nil {
879879
e.putLog(err)
880880
return
881881
}
882+
882883
if err := cmd.Start(); err != nil {
883884
e.putLog(err)
884885
return
885886
}
886-
output, err := ioutil.ReadAll(stdout)
887+
888+
output, err := io.ReadAll(stdout)
889+
stdout.Close()
887890
if err != nil {
888891
e.putLog(err)
889-
stdout.Close()
890892
return
891893
}
892-
for _, b := range strings.Split(string(output), "\n") {
893-
splits := strings.SplitN(b, "=", 2)
894-
if len(splits) > 1 {
894+
895+
for _, line := range strings.Split(string(output), "\n") {
896+
splits := strings.SplitN(line, "=", 2)
897+
if len(splits) == 2 {
895898
_ = os.Setenv(splits[0], splits[1])
896899
}
897900
}
@@ -1100,11 +1103,18 @@ func (e *Editor) setWindowSizeFromOpts() {
11001103
func (e *Editor) setWindowSize(s string) (int, int) {
11011104
var width, height int
11021105
var err error
1103-
width, err = strconv.Atoi(strings.SplitN(s, "x", 2)[0])
1106+
1107+
parsed_s := strings.SplitN(s, "x", 2)
1108+
if len(parsed_s) != 2 {
1109+
// TODO: Error message to user?
1110+
return 40, 30
1111+
}
1112+
1113+
width, err = strconv.Atoi(parsed_s[0])
11041114
if err != nil || width < 40 {
11051115
width = 40
11061116
}
1107-
height, err = strconv.Atoi(strings.SplitN(s, "x", 2)[1])
1117+
height, err = strconv.Atoi(parsed_s[1])
11081118
if err != nil || height < 30 {
11091119
height = 30
11101120
}

editor/screen.go

Lines changed: 22 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -23,6 +23,7 @@ var globalOrder int
2323
// Screen is the main editor area
2424
type Screen struct {
2525
cache Cache
26+
bgcache Cache
2627
tooltip *IMETooltip
2728
font *Font
2829
fallbackfonts []*Font
@@ -57,6 +58,7 @@ func newScreen() *Screen {
5758
cursor: [2]int{0, 0},
5859
highlightGroup: make(map[string]int),
5960
cache: newCache(),
61+
bgcache: newBrushCache(),
6062
}
6163

6264
widget.ConnectMousePressEvent(screen.mousePressEvent)
@@ -354,6 +356,13 @@ func (s *Screen) gridFontAutomaticHeight(update interface{}) {
354356
grid = s.ws.cursor.bufferGridid
355357
}
356358

359+
// Workaround for autocmds triggered with BufEnter/BufWinEnter/...
360+
// When a file is opened directly e.g. with `goneovim plainfile.txt`
361+
// would end with applying the new font to the status line.
362+
if grid == 1 {
363+
grid = 2
364+
}
365+
357366
win, ok := s.getWindow(grid)
358367
if !ok {
359368
return
@@ -369,6 +378,7 @@ func (s *Screen) gridFontAutomaticHeight(update interface{}) {
369378
}
370379

371380
font := win.getFont()
381+
oldCellWidth := font.cellwidth
372382

373383
// The font height we'll try to approximate with the new font
374384
oldFontHeight := font.fontMetrics.Height()
@@ -404,12 +414,11 @@ func (s *Screen) gridFontAutomaticHeight(update interface{}) {
404414
for newFontHeight > oldFontHeight {
405415
updateFont(-1)
406416
}
407-
for newFontHeight < oldFontHeight {
408-
updateFont(0.1)
409-
}
410-
for newFontHeight > oldFontHeight {
411-
updateFont(-0.1)
412-
}
417+
418+
// Refresh the screen while the font.cellwidth hasn't change.
419+
// It avoids old text to stay forever, between windows.
420+
// (Especially between window and statusbar.)
421+
s.refresh()
413422

414423
// Build the new font
415424
win.font = initFontNew(fontFamily, newFontSize, font.weight, font.stretch, font.lineSpace, font.letterSpace)
@@ -420,6 +429,13 @@ func (s *Screen) gridFontAutomaticHeight(update interface{}) {
420429
win.fallbackfonts = nil
421430
s.ws.cursor.fallbackfonts = win.fallbackfonts
422431

432+
// Keep the old cellwidth matters. Changing it would impact e.g.
433+
// Window.paint(). Plus, proportional font doesn't have something
434+
// such as a `cellwidth`.
435+
win.font.cellwidth = oldCellWidth
436+
437+
// TODO: Resize Grid if enough space to add a row
438+
423439
// Cache
424440
cache := win.cache
425441
if cache == (Cache{}) {

0 commit comments

Comments
 (0)