Skip to content

Commit 4447408

Browse files
committed
Add bold and underline
1 parent 4a8cbbb commit 4447408

File tree

9 files changed

+82
-15
lines changed

9 files changed

+82
-15
lines changed
393 KB
Binary file not shown.
394 KB
Binary file not shown.
Lines changed: 26 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,26 @@
1+
package notosansmono
2+
3+
import (
4+
// go embed.
5+
_ "embed"
6+
7+
"fyne.io/fyne/v2"
8+
)
9+
10+
//go:embed NotoSansMono-Regular.ttf
11+
var regular []byte
12+
13+
// Regular is the regular font resource.
14+
var Regular = &fyne.StaticResource{
15+
StaticName: "NotoSansMono-Regular.ttf",
16+
StaticContent: regular,
17+
}
18+
19+
//go:embed NotoSansMono-Bold.ttf
20+
var bold []byte
21+
22+
// Bold is the bold font resource.
23+
var Bold = &fyne.StaticResource{
24+
StaticName: "NotoSansMono-Bold.ttf",
25+
StaticContent: bold,
26+
}

cmd/fyneterm/theme.go

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -5,6 +5,7 @@ import (
55

66
"fyne.io/fyne/v2"
77
"fyne.io/fyne/v2/theme"
8+
notosansmono "github.com/fyne-io/terminal/cmd/fyneterm/font/NotoSansMono"
89
)
910

1011
type termTheme struct {
@@ -41,3 +42,12 @@ func (t *termTheme) Size(n fyne.ThemeSizeName) float32 {
4142

4243
return t.Theme.Size(n)
4344
}
45+
46+
func (t *termTheme) Font(style fyne.TextStyle) fyne.Resource {
47+
switch {
48+
case style.Bold:
49+
return notosansmono.Bold
50+
default:
51+
return notosansmono.Regular
52+
}
53+
}

color.go

Lines changed: 6 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -47,6 +47,7 @@ func (t *Terminal) handleColorEscape(message string) {
4747
t.currentFG = nil
4848
t.bold = false
4949
t.blinking = false
50+
t.underlined = false
5051
return
5152
}
5253
modes := strings.Split(message, ";")
@@ -82,11 +83,15 @@ func (t *Terminal) handleColorMode(modeStr string) {
8283
t.currentBG, t.currentFG = nil, nil
8384
t.bold = false
8485
t.blinking = false
86+
t.underlined = false
8587
case 1:
8688
t.bold = true
87-
case 4, 24: //italic
89+
case 4:
90+
t.underlined = true
8891
case 5:
8992
t.blinking = true
93+
case 24:
94+
t.underlined = false
9095
case 7: // reverse
9196
bg, fg := t.currentBG, t.currentFG
9297
if fg == nil {

color_test.go

Lines changed: 21 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -45,13 +45,23 @@ func testColor(t *testing.T, tests map[string]struct {
4545

4646
func TestHandleOutput_Text(t *testing.T) {
4747
tests := map[string]struct {
48-
inputSeq string
49-
expectBold bool
48+
inputSeq string
49+
expectBold bool
50+
expectUnderline bool
5051
}{
5152
"bold": {
5253
inputSeq: esc("[1m"),
5354
expectBold: true,
5455
},
56+
"underline": {
57+
inputSeq: esc("[4m"),
58+
expectUnderline: true,
59+
},
60+
"bold and underline": {
61+
inputSeq: esc("[1m") + esc("[4m"),
62+
expectBold: true,
63+
expectUnderline: true,
64+
},
5565
}
5666

5767
// Iterate through the test cases
@@ -60,6 +70,11 @@ func TestHandleOutput_Text(t *testing.T) {
6070
terminal := New()
6171
terminal.handleOutput([]byte(test.inputSeq))
6272

73+
// Verify the actual results match the expected results
74+
if terminal.underlined != test.expectUnderline {
75+
t.Errorf("Bold flag mismatch. Got %v, expected %v", terminal.underlined, test.expectUnderline)
76+
}
77+
6378
if terminal.bold != test.expectBold {
6479
t.Errorf("Bold flag mismatch. Got %v, expected %v", terminal.bold, test.expectBold)
6580
}
@@ -921,10 +936,10 @@ func TestHandleOutput_BufferCutoff(t *testing.T) {
921936
tg.Rows = []widget.TextGridRow{
922937
{
923938
Cells: []widget.TextGridCell{
924-
{Rune: '4', Style: &widget.CustomTextGridStyle{FGColor: c1, BGColor: nil}},
925-
{Rune: '0', Style: &widget.CustomTextGridStyle{FGColor: c1, BGColor: nil}},
926-
{Rune: '4', Style: &widget.CustomTextGridStyle{FGColor: c2, BGColor: nil}},
927-
{Rune: '1', Style: &widget.CustomTextGridStyle{FGColor: c2, BGColor: nil}},
939+
{Rune: '4', Style: widget2.NewTermTextGridStyle(c1, nil, 0x55, false, false, false)},
940+
{Rune: '0', Style: widget2.NewTermTextGridStyle(c1, nil, 0x55, false, false, false)},
941+
{Rune: '4', Style: widget2.NewTermTextGridStyle(c2, nil, 0x55, false, false, false)},
942+
{Rune: '1', Style: widget2.NewTermTextGridStyle(c2, nil, 0x55, false, false, false)},
928943
},
929944
},
930945
}

internal/widget/termgridhelper.go

Lines changed: 17 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -15,9 +15,9 @@ func HighlightRange(t *TermGrid, blockMode bool, startRow, startCol, endRow, end
1515
// Check if already highlighted
1616
if h, ok := cell.Style.(*TermTextGridStyle); !ok {
1717
if cell.Style != nil {
18-
cell.Style = NewTermTextGridStyle(cell.Style.TextColor(), cell.Style.BackgroundColor(), bitmask, false)
18+
cell.Style = NewTermTextGridStyle(cell.Style.TextColor(), cell.Style.BackgroundColor(), bitmask, false, false, false)
1919
} else {
20-
cell.Style = NewTermTextGridStyle(nil, nil, bitmask, false)
20+
cell.Style = NewTermTextGridStyle(nil, nil, bitmask, false, false, false)
2121
}
2222
cell.Style.(*TermTextGridStyle).Highlighted = true
2323

@@ -174,6 +174,8 @@ type TermTextGridStyle struct {
174174
InvertedBackgroundColor color.Color
175175
Highlighted bool
176176
BlinkEnabled bool
177+
IsBold bool
178+
IsUnderlined bool
177179
}
178180

179181
// Style is the text style a cell should use.
@@ -197,6 +199,16 @@ func (h *TermTextGridStyle) BackgroundColor() color.Color {
197199
return h.OriginalBackgroundColor
198200
}
199201

202+
// Bold is the text bold or not.
203+
func (h *TermTextGridStyle) Bold() bool {
204+
return h.IsBold
205+
}
206+
207+
// Underlined is the text underlined or not.
208+
func (h *TermTextGridStyle) Underlined() bool {
209+
return h.IsUnderlined
210+
}
211+
200212
// HighlightOption defines a function type that can modify a TermTextGridStyle.
201213
type HighlightOption func(h *TermTextGridStyle)
202214

@@ -214,7 +226,7 @@ type HighlightOption func(h *TermTextGridStyle)
214226
// Returns:
215227
//
216228
// A pointer to a TermTextGridStyle initialized with the provided colors and inversion settings.
217-
func NewTermTextGridStyle(fg, bg color.Color, bitmask byte, blinkEnabled bool) widget.TextGridStyle {
229+
func NewTermTextGridStyle(fg, bg color.Color, bitmask byte, blinkEnabled, bold, underlined bool) widget.TextGridStyle {
218230
// calculate the inverted colors
219231
var invertedFg, invertedBg color.Color
220232
if fg == nil {
@@ -235,6 +247,8 @@ func NewTermTextGridStyle(fg, bg color.Color, bitmask byte, blinkEnabled bool) w
235247
InvertedBackgroundColor: invertedBg,
236248
Highlighted: false,
237249
BlinkEnabled: blinkEnabled,
250+
IsBold: bold,
251+
IsUnderlined: underlined,
238252
}
239253
}
240254

output.go

Lines changed: 1 addition & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -248,18 +248,14 @@ func (t *Terminal) handleOutputChar(r rune) {
248248
t.content.Rows = append(t.content.Rows, widget.TextGridRow{})
249249
}
250250

251-
var cellStyle widget.TextGridStyle
252-
cellStyle = &widget.CustomTextGridStyle{FGColor: t.currentFG, BGColor: t.currentBG}
251+
cellStyle := widget2.NewTermTextGridStyle(t.currentFG, t.currentBG, t.highlightBitMask, t.blinking, t.bold, t.underlined)
253252
for len(t.content.Rows[t.cursorRow].Cells)-1 < t.cursorCol {
254253
newCell := widget.TextGridCell{
255254
Rune: ' ',
256255
Style: cellStyle,
257256
}
258257
t.content.Rows[t.cursorRow].Cells = append(t.content.Rows[t.cursorRow].Cells, newCell)
259258
}
260-
if t.blinking {
261-
cellStyle = widget2.NewTermTextGridStyle(t.currentFG, t.currentBG, t.highlightBitMask, t.blinking)
262-
}
263259
t.content.SetCell(t.cursorRow, t.cursorCol, widget.TextGridCell{Rune: r, Style: cellStyle})
264260
t.cursorCol++
265261
}

term.go

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -81,6 +81,7 @@ type Terminal struct {
8181
bracketedPasteMode bool
8282
state *parseState
8383
blinking bool
84+
underlined bool
8485
printData []byte
8586
printer Printer
8687
cmd *exec.Cmd

0 commit comments

Comments
 (0)