Skip to content

Commit fa0ae3b

Browse files
committed
Add bold and underline
1 parent b5864dd commit fa0ae3b

File tree

9 files changed

+79
-11
lines changed

9 files changed

+79
-11
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
@@ -46,6 +46,7 @@ func (t *Terminal) handleColorEscape(message string) {
4646
t.currentBG = nil
4747
t.currentFG = nil
4848
t.bold = false
49+
t.underlined = false
4950
t.blinking = false
5051
return
5152
}
@@ -82,10 +83,14 @@ func (t *Terminal) handleColorMode(modeStr string) {
8283
case 0:
8384
t.currentBG, t.currentFG = nil, nil
8485
t.bold = false
86+
t.underlined = false
8587
t.blinking = false
8688
case 1:
8789
t.bold = true
88-
case 4, 24: //italic
90+
case 4:
91+
t.underlined = true
92+
case 24:
93+
t.underlined = false
8994
case 5:
9095
t.blinking = true
9196
case 7: // reverse

color_test.go

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

4141
func TestHandleOutput_Text(t *testing.T) {
4242
tests := map[string]struct {
43-
inputSeq string
44-
expectBold bool
43+
inputSeq string
44+
expectBold bool
45+
expectUnderline bool
4546
}{
4647
"bold": {
4748
inputSeq: esc("[1m"),
4849
expectBold: true,
4950
},
51+
"underline": {
52+
inputSeq: esc("[4m"),
53+
expectUnderline: true,
54+
},
55+
"bold and underline": {
56+
inputSeq: esc("[1m") + esc("[4m"),
57+
expectBold: true,
58+
expectUnderline: true,
59+
},
5060
}
5161

5262
// Iterate through the test cases
@@ -55,6 +65,11 @@ func TestHandleOutput_Text(t *testing.T) {
5565
terminal := New()
5666
terminal.handleOutput([]byte(test.inputSeq))
5767

68+
// Verify the actual results match the expected results
69+
if terminal.underlined != test.expectUnderline {
70+
t.Errorf("Bold flag mismatch. Got %v, expected %v", terminal.underlined, test.expectUnderline)
71+
}
72+
5873
if terminal.bold != test.expectBold {
5974
t.Errorf("Bold flag mismatch. Got %v, expected %v", terminal.bold, test.expectBold)
6075
}

internal/widget/textgrid.go

Lines changed: 17 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -14,9 +14,9 @@ func HighlightRange(t *widget.TextGrid, blockMode bool, startRow, startCol, endR
1414
// Check if already highlighted
1515
if h, ok := cell.Style.(*TermTextGridStyle); !ok {
1616
if cell.Style != nil {
17-
cell.Style = NewTermTextGridStyle(cell.Style.TextColor(), cell.Style.BackgroundColor(), bitmask, false)
17+
cell.Style = NewTermTextGridStyle(cell.Style.TextColor(), cell.Style.BackgroundColor(), bitmask, false, false, false)
1818
} else {
19-
cell.Style = NewTermTextGridStyle(nil, nil, bitmask, false)
19+
cell.Style = NewTermTextGridStyle(nil, nil, bitmask, false, false, false)
2020
}
2121
cell.Style.(*TermTextGridStyle).Highlighted = true
2222

@@ -173,6 +173,8 @@ type TermTextGridStyle struct {
173173
Highlighted bool
174174
BlinkEnabled bool
175175
Blink bool
176+
IsBold bool
177+
IsUnderlined bool
176178
}
177179

178180
// TextColor returns the color of the text, depending on whether it is highlighted.
@@ -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 {
@@ -236,6 +248,8 @@ func NewTermTextGridStyle(fg, bg color.Color, bitmask byte, blinkEnabled bool) w
236248
Highlighted: false,
237249
BlinkEnabled: blinkEnabled,
238250
Blink: false,
251+
IsBold: bold,
252+
IsUnderlined: underlined,
239253
}
240254
}
241255

output.go

Lines changed: 2 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -190,18 +190,15 @@ func (t *Terminal) handleOutputChar(r rune) {
190190
t.content.Rows = append(t.content.Rows, widget.TextGridRow{})
191191
}
192192

193-
var cellStyle widget.TextGridStyle
194-
cellStyle = &widget.CustomTextGridStyle{FGColor: t.currentFG, BGColor: t.currentBG}
193+
cellStyle := widget2.NewTermTextGridStyle(t.currentFG, t.currentBG, t.highlightBitMask, t.blinking, t.bold, t.underlined)
195194
for len(t.content.Rows[t.cursorRow].Cells)-1 < t.cursorCol {
196195
newCell := widget.TextGridCell{
197196
Rune: ' ',
198197
Style: cellStyle,
199198
}
200199
t.content.Rows[t.cursorRow].Cells = append(t.content.Rows[t.cursorRow].Cells, newCell)
201200
}
202-
if t.blinking {
203-
cellStyle = widget2.NewTermTextGridStyle(t.currentFG, t.currentBG, t.highlightBitMask, t.blinking)
204-
}
201+
205202
t.content.SetCell(t.cursorRow, t.cursorCol, widget.TextGridCell{Rune: r, Style: cellStyle})
206203
t.cursorCol++
207204
}

term.go

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -79,6 +79,7 @@ type Terminal struct {
7979
newLineMode bool // new line mode or line feed mode
8080
bracketedPasteMode bool
8181
blinking bool
82+
underlined bool
8283
}
8384

8485
// Cursor is used for displaying a specific cursor.

0 commit comments

Comments
 (0)