Skip to content
Merged
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
4 changes: 4 additions & 0 deletions desktop/internal/i18n/en.go
Original file line number Diff line number Diff line change
Expand Up @@ -304,12 +304,16 @@ var english = Messages{
FindPrevious: Shared(English, "desktop.terminal.findPrevious"),
FindNext: Shared(English, "desktop.terminal.findNext"),
FindClose: Shared(English, "desktop.terminal.findClose"),
PasteImageTooLarge: Shared(English, "desktop.terminal.pasteImageTooLarge"),
FindCount: func(current, total string) string {
return SharedFormat(English, "desktop.terminal.findCount", map[string]string{"current": current, "total": total})
},
OpenLinkFailed: func(reason string) string {
return SharedFormat(English, "desktop.terminal.openLinkFailed", map[string]string{"reason": reason})
},
PasteImageFailed: func(reason string) string {
return SharedFormat(English, "desktop.terminal.pasteImageFailed", map[string]string{"reason": reason})
},
ShellMenu: Shared(English, "native.newTerminalWithShell"),
DetectingShells: Shared(English, "terminal.detectingShells"),
NoShells: Shared(English, "native.shellNotFound"),
Expand Down
4 changes: 4 additions & 0 deletions desktop/internal/i18n/messages.go
Original file line number Diff line number Diff line change
Expand Up @@ -498,6 +498,10 @@ type TerminalMessages struct {
FindCount func(current, total string) string
// OpenLinkFailed tells that a clicked link could not be opened.
OpenLinkFailed func(reason string) string
// PasteImageFailed tells that the picture on the clipboard could not be saved for a
// paste, and PasteImageTooLarge that it was too large to read.
PasteImageFailed func(reason string) string
PasteImageTooLarge string
// ShellMenu is the accessible name of the button beside NewTab that lists the
// installed shells. DetectingShells and NoShells stand in that list while it is being
// made and when it found none. ShellWithPath names an installation whose name another
Expand Down
24 changes: 24 additions & 0 deletions desktop/internal/i18n/shared.generated.go

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

4 changes: 4 additions & 0 deletions desktop/internal/i18n/zh.go
Original file line number Diff line number Diff line change
Expand Up @@ -304,12 +304,16 @@ var chinese = Messages{
FindPrevious: Shared(Chinese, "desktop.terminal.findPrevious"),
FindNext: Shared(Chinese, "desktop.terminal.findNext"),
FindClose: Shared(Chinese, "desktop.terminal.findClose"),
PasteImageTooLarge: Shared(Chinese, "desktop.terminal.pasteImageTooLarge"),
FindCount: func(current, total string) string {
return SharedFormat(Chinese, "desktop.terminal.findCount", map[string]string{"current": current, "total": total})
},
OpenLinkFailed: func(reason string) string {
return SharedFormat(Chinese, "desktop.terminal.openLinkFailed", map[string]string{"reason": reason})
},
PasteImageFailed: func(reason string) string {
return SharedFormat(Chinese, "desktop.terminal.pasteImageFailed", map[string]string{"reason": reason})
},
ShellMenu: Shared(Chinese, "native.newTerminalWithShell"),
DetectingShells: Shared(Chinese, "terminal.detectingShells"),
NoShells: Shared(Chinese, "native.shellNotFound"),
Expand Down
9 changes: 9 additions & 0 deletions desktop/internal/settingswindow/controller.go
Original file line number Diff line number Diff line change
Expand Up @@ -74,6 +74,12 @@ type Values struct {
// TerminalScrollback is the history lines a new terminal keeps, within
// MinScrollback and MaxScrollback.
TerminalScrollback int
// TerminalCursorShape is the shape the terminal cursor takes until a program asks for
// its own (CursorShapeBlock, CursorShapeBar or CursorShapeUnderline), and
// TerminalCursorThickness how thick a bar or an underline draws (CursorThicknessThin,
// CursorThicknessNormal or CursorThicknessThick).
TerminalCursorShape string
TerminalCursorThickness string
// Fonts are the fonts of the editor, the terminal and the run output, as
// fontprefs.Normalize returns them. Nil is fontprefs.Default, the system's fixed-pitch
// font for every place.
Expand Down Expand Up @@ -374,6 +380,9 @@ type Options struct {
// SetTerminalScrollback hands a change of Values.TerminalScrollback to the settings
// owner, like SetTabRows.
SetTerminalScrollback func(int)
// SetTerminalCursor hands a change of Values.TerminalCursorShape and
// Values.TerminalCursorThickness to the settings owner, like SetTabRows.
SetTerminalCursor func(shape, thickness string)
// SetFonts hands a change of Values.Fonts to the settings owner, like SetTabRows. The
// list it receives is normalized.
SetFonts func([]fontprefs.Font)
Expand Down
101 changes: 101 additions & 0 deletions desktop/internal/settingswindow/terminal_cursor_view.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,101 @@
package settingswindow

import (
"github.com/vivaui/viva/ui"

"github.com/1lck/Lithe-IDEA/desktop/internal/i18n"
)

// The terminal cursor shapes Values.TerminalCursorShape holds. The empty string is the block a
// terminal starts with.
const (
CursorShapeBlock = ""
CursorShapeBar = "bar"
CursorShapeUnderline = "underline"
)

// The terminal cursor thicknesses Values.TerminalCursorThickness holds. The empty string is
// the normal thickness.
const (
CursorThicknessThin = "thin"
CursorThicknessNormal = ""
CursorThicknessThick = "thick"
)

var (
cursorShapeChoices = []string{CursorShapeBlock, CursorShapeBar, CursorShapeUnderline}
cursorThicknessChoices = []string{CursorThicknessThin, CursorThicknessNormal, CursorThicknessThick}
)

// NormalCursorShape is shape when it is one of the shapes above and the block otherwise, so a
// value an older or newer build stored draws a cursor and shows as one choice of the page.
func NormalCursorShape(shape string) string {
if shape == CursorShapeBar || shape == CursorShapeUnderline {
return shape
}
return CursorShapeBlock
}

// NormalCursorThickness is thickness when it is one of the thicknesses above and the normal
// one otherwise.
func NormalCursorThickness(thickness string) string {
if thickness == CursorThicknessThin || thickness == CursorThicknessThick {
return thickness
}
return CursorThicknessNormal
}

// cursorCard is the Terminal page's cursor setting: the shape the cursor takes until a
// program asks for its own, and how thick a bar or an underline draws. The thickness stays
// open with the block chosen, as it is also the thickness of a bar or an underline a program
// asks for. Open terminals take a change at their next paint.
func (w *window) cursorCard(c ui.C) {
language := w.marketLanguage()
shape := NormalCursorShape(w.sess.values.TerminalCursorShape)
thickness := NormalCursorThickness(w.sess.values.TerminalCursorThickness)
shapeTitle := i18n.Shared(language, "desktop.settings.terminal.cursorShape")
thicknessTitle := i18n.Shared(language, "desktop.settings.terminal.cursorThickness")
w.card(c, i18n.Shared(language, "desktop.settings.terminal.cursor"), i18n.Shared(language, "desktop.settings.terminal.cursorHelp"), func() {
w.settingsRow(c, shapeTitle, "", func() {
if ui.Select(c.Part("cursor-shape"), &shape, cursorShapeChoices, func(choice string) string {
switch choice {
case CursorShapeBar:
return i18n.Shared(language, "desktop.settings.terminal.cursorShapeBar")
case CursorShapeUnderline:
return i18n.Shared(language, "desktop.settings.terminal.cursorShapeUnderline")
}
return i18n.Shared(language, "desktop.settings.terminal.cursorShapeBlock")
}).Width(selectWidth).Label(shapeTitle).Changed() {
w.setTerminalCursor(shape, thickness)
}
})
w.settingsRow(c, thicknessTitle, "", func() {
if ui.Select(c.Part("cursor-thickness"), &thickness, cursorThicknessChoices, func(choice string) string {
switch choice {
case CursorThicknessThin:
return i18n.Shared(language, "desktop.settings.terminal.cursorThin")
case CursorThicknessThick:
return i18n.Shared(language, "desktop.settings.terminal.cursorThick")
}
return i18n.Shared(language, "desktop.settings.terminal.cursorNormal")
}).Width(selectWidth).Label(thicknessTitle).Changed() {
w.setTerminalCursor(shape, thickness)
}
})
})
}

// setTerminalCursor stores the cursor chosen and hands it to the settings owner.
func (w *window) setTerminalCursor(shape, thickness string) {
if w.sess.finished.Load() {
return
}
shape, thickness = NormalCursorShape(shape), NormalCursorThickness(thickness)
if shape == NormalCursorShape(w.sess.values.TerminalCursorShape) && thickness == NormalCursorThickness(w.sess.values.TerminalCursorThickness) {
return
}
w.sess.values.TerminalCursorShape, w.sess.values.TerminalCursorThickness = shape, thickness
if w.ctrl.opts.SetTerminalCursor != nil {
w.ctrl.opts.SetTerminalCursor(shape, thickness)
}
}
52 changes: 52 additions & 0 deletions desktop/internal/settingswindow/terminal_cursor_view_test.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,52 @@
package settingswindow

import (
"slices"
"testing"

"github.com/vivaui/viva/gfx"

"github.com/1lck/Lithe-IDEA/desktop/internal/i18n"
"github.com/1lck/Lithe-IDEA/desktop/internal/localshell"
)

// TestTerminalCursorChoicesApplyWhenChosen covers the Terminal page's cursor card: a stored
// value the page does not offer shows as the default and is not rewritten by opening the
// page, and each choice reaches the settings owner at once with the other half kept.
func TestTerminalCursorChoicesApplyWhenChosen(t *testing.T) {
var changed [][2]string
w := newWindow(NewController(Options{
TerminalShells: func() ([]localshell.Choice, string) {
return []localshell.Choice{{Name: "zsh", Path: "/bin/zsh", Default: true}}, "/bin/zsh"
},
SetTerminalCursor: func(shape, thickness string) { changed = append(changed, [2]string{shape, thickness}) },
}), Values{TerminalCursorShape: "beam", TerminalCursorThickness: CursorThicknessThin})
w.sess.page = pageTerminal
d := openWindow(t, w)
language := w.marketLanguage()
word := func(key string) string { return i18n.Shared(language, "desktop.settings.terminal."+key) }

if hit := d.hit(word("cursorShape")); hit.Role != gfx.RoleCombobox || hit.Value != word("cursorShapeBlock") {
t.Fatalf("an unknown shape did not show the block: %+v", hit)
}
if hit := d.hit(word("cursorThickness")); hit.Value != word("cursorThin") {
t.Fatalf("the stored thickness is not chosen: %+v", hit)
}
if len(changed) != 0 {
t.Fatalf("opening the page wrote %v", changed)
}

d.click(word("cursorShape"))
d.click(word("cursorShapeBar"))
d.click(word("cursorThickness"))
d.click(word("cursorThick"))
d.click(word("cursorShape"))
d.click(word("cursorShapeBlock"))
want := [][2]string{{CursorShapeBar, CursorThicknessThin}, {CursorShapeBar, CursorThicknessThick}, {CursorShapeBlock, CursorThicknessThick}}
if !slices.Equal(changed, want) {
t.Fatalf("choices wrote %v, want %v", changed, want)
}
if hit := d.hit(word("cursorThickness")); hit.Value != word("cursorThick") || hit.Disabled {
t.Fatalf("with the block chosen the thickness is %+v, want the thick one still open", hit)
}
}
1 change: 1 addition & 0 deletions desktop/internal/settingswindow/terminal_view.go
Original file line number Diff line number Diff line change
Expand Up @@ -169,6 +169,7 @@ func (w *window) terminalPage(c ui.C) {
})
})
w.scrollbackCard(c)
w.cursorCard(c)
})
if w.shellPick != stored && !(stored != "" && sameShell(w.shellPick, stored)) {
w.setTerminalShell(w.shellPick, false)
Expand Down
7 changes: 7 additions & 0 deletions desktop/internal/term/csi.go
Original file line number Diff line number Diff line change
Expand Up @@ -469,6 +469,13 @@ func (t *Terminal) softReset() {

func (t *Terminal) setCursorStyle(n int) {
t.cursorSteady = n == 2 || n == 4 || n == 6
if n == 0 {
// 0 asks for the default cursor, which is the user's choice as in Windows Terminal,
// iTerm2 and kitty rather than xterm's blinking block: vim, and the vi mode of zsh and
// fish, send it on the way out to give back the cursor they found.
t.cursor, t.cursorStyled = t.defaultCursor, false
return
}
t.cursorStyled = true
switch n {
case 3, 4:
Expand Down
39 changes: 39 additions & 0 deletions desktop/internal/term/cursor_style_test.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,39 @@
package term

import "testing"

// The user's default shape is the cursor's until a program asks for its own, and comes back
// when the program asks for the default with DECSCUSR 0 or resets the terminal.
func TestDefaultCursorShapeYieldsToProgramsAndComesBack(t *testing.T) {
screen := New(20, 4, 0)
if got := screen.Cursor().Style; got != CursorBlock {
t.Fatalf("a new terminal's cursor = %v, want the block", got)
}
screen.SetDefaultCursor(CursorBar)
if got := screen.Cursor().Style; got != CursorBar {
t.Fatalf("after a bar default the cursor = %v, want the bar", got)
}

screen.Write([]byte("\x1b[2 q"))
if got := screen.Cursor().Style; got != CursorBlock {
t.Fatalf("after a program asked for a steady block the cursor = %v", got)
}
screen.SetDefaultCursor(CursorUnderline)
if got := screen.Cursor().Style; got != CursorBlock {
t.Fatalf("a new default replaced the shape a program asked for: %v", got)
}

screen.Write([]byte("\x1b[0 q"))
if got := screen.Cursor().Style; got != CursorUnderline {
t.Fatalf("DECSCUSR 0 gave %v, want the user's underline", got)
}
screen.SetDefaultCursor(CursorBar)
if got := screen.Cursor().Style; got != CursorBar {
t.Fatalf("after DECSCUSR 0 the default no longer applies: %v", got)
}

screen.Write([]byte("\x1b[3 q\x1bc"))
if got := screen.Cursor().Style; got != CursorBar {
t.Fatalf("after a reset the cursor = %v, want the user's bar", got)
}
}
34 changes: 34 additions & 0 deletions desktop/internal/term/selection.go
Original file line number Diff line number Diff line change
Expand Up @@ -67,6 +67,40 @@ func (t *Terminal) SelectedRange(anchor, focus Point) string {
return result.String()
}

// BlockRange returns the cells of the rectangle between two buffer points: the
// columns they span, on every line they span. Unlike SelectedRange, a
// soft-wrapped row is a line of its own here, the way the rectangle shows it on
// screen. Points beyond the retained lines clamp to them; a wide character is
// kept whole when the rectangle starts on its second cell. Blank cells at the
// right edge are dropped from each line, so a column copied out of a table keeps
// its alignment without trailing spaces.
func (t *Terminal) BlockRange(anchor, focus Point) string {
base, count := t.lineBase(), uint64(t.bufferLineCount())
if count == 0 {
return ""
}
anchor, focus = t.clampPoint(anchor, base, count), t.clampPoint(focus, base, count)
if focus.Line < anchor.Line {
anchor.Line, focus.Line = focus.Line, anchor.Line
}
from, to := min(anchor.Column, focus.Column), max(anchor.Column, focus.Column)

var result strings.Builder
result.Grow(int(min(focus.Line-anchor.Line+1, 1<<16)) * (to - from + 1))
for line := anchor.Line; line <= focus.Line; line++ {
if line > anchor.Line {
result.WriteByte('\n')
}
row := t.rowOf(t.bufferLine(int(line - base)))
start := from
if start > 0 && start < len(row.Cells) && row.Cells[start].Attr&WideTail != 0 {
start--
}
writeSelectedCells(&result, row, start, to)
}
return result.String()
}

// lineBase is the line number of retained-buffer index 0.
func (t *Terminal) lineBase() uint64 {
if t.onAlt {
Expand Down
Loading
Loading