Skip to content

Commit

Permalink
Add bold and underline
Browse files Browse the repository at this point in the history
  • Loading branch information
mgazza committed Feb 2, 2024
1 parent b2e22b7 commit 43baa67
Show file tree
Hide file tree
Showing 9 changed files with 79 additions and 11 deletions.
Binary file not shown.
Binary file not shown.
26 changes: 26 additions & 0 deletions cmd/fyneterm/font/NotoSansMono/notosansmono.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,26 @@
package notosansmono

import (
// go embed.
_ "embed"

"fyne.io/fyne/v2"
)

//go:embed NotoSansMono-Regular.ttf
var regular []byte

// Regular is the regular font resource.
var Regular = &fyne.StaticResource{
StaticName: "NotoSansMono-Regular.ttf",
StaticContent: regular,
}

//go:embed NotoSansMono-Bold.ttf
var bold []byte

// Bold is the bold font resource.
var Bold = &fyne.StaticResource{
StaticName: "NotoSansMono-Bold.ttf",
StaticContent: bold,
}
10 changes: 10 additions & 0 deletions cmd/fyneterm/theme.go
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@ import (

"fyne.io/fyne/v2"
"fyne.io/fyne/v2/theme"
notosansmono "github.com/fyne-io/terminal/cmd/fyneterm/font/NotoSansMono"
)

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

return t.Theme.Size(n)
}

func (t *termTheme) Font(style fyne.TextStyle) fyne.Resource {
switch {
case style.Bold:
return notosansmono.Bold
default:
return notosansmono.Regular
}
}
7 changes: 6 additions & 1 deletion color.go
Original file line number Diff line number Diff line change
Expand Up @@ -46,6 +46,7 @@ func (t *Terminal) handleColorEscape(message string) {
t.currentBG = nil
t.currentFG = nil
t.bold = false
t.underlined = false
t.blinking = false
return
}
Expand Down Expand Up @@ -81,10 +82,14 @@ func (t *Terminal) handleColorMode(modeStr string) {
case 0:
t.currentBG, t.currentFG = nil, nil
t.bold = false
t.underlined = false
t.blinking = false
case 1:
t.bold = true
case 4, 24: //italic
case 4:
t.underlined = true
case 24:
t.underlined = false
case 5:
t.blinking = true
case 7: // reverse
Expand Down
19 changes: 17 additions & 2 deletions color_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -40,13 +40,23 @@ func testColor(t *testing.T, tests map[string]struct {

func TestHandleOutput_Text(t *testing.T) {
tests := map[string]struct {
inputSeq string
expectBold bool
inputSeq string
expectBold bool
expectUnderline bool
}{
"bold": {
inputSeq: esc("[1m"),
expectBold: true,
},
"underline": {
inputSeq: esc("[4m"),
expectUnderline: true,
},
"bold and underline": {
inputSeq: esc("[1m") + esc("[4m"),
expectBold: true,
expectUnderline: true,
},
}

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

// Verify the actual results match the expected results
if terminal.underlined != test.expectUnderline {
t.Errorf("Bold flag mismatch. Got %v, expected %v", terminal.underlined, test.expectUnderline)
}

if terminal.bold != test.expectBold {
t.Errorf("Bold flag mismatch. Got %v, expected %v", terminal.bold, test.expectBold)
}
Expand Down
20 changes: 17 additions & 3 deletions internal/widget/textgrid.go
Original file line number Diff line number Diff line change
Expand Up @@ -14,9 +14,9 @@ func HighlightRange(t *widget.TextGrid, blockMode bool, startRow, startCol, endR
// Check if already highlighted
if h, ok := cell.Style.(*TermTextGridStyle); !ok {
if cell.Style != nil {
cell.Style = NewTermTextGridStyle(cell.Style.TextColor(), cell.Style.BackgroundColor(), bitmask, false)
cell.Style = NewTermTextGridStyle(cell.Style.TextColor(), cell.Style.BackgroundColor(), bitmask, false, false, false)
} else {
cell.Style = NewTermTextGridStyle(nil, nil, bitmask, false)
cell.Style = NewTermTextGridStyle(nil, nil, bitmask, false, false, false)
}
cell.Style.(*TermTextGridStyle).Highlighted = true

Expand Down Expand Up @@ -173,6 +173,8 @@ type TermTextGridStyle struct {
Highlighted bool
BlinkEnabled bool
Blink bool
IsBold bool
IsUnderlined bool
}

// TextColor returns the color of the text, depending on whether it is highlighted.
Expand All @@ -197,6 +199,16 @@ func (h *TermTextGridStyle) BackgroundColor() color.Color {
return h.OriginalBackgroundColor
}

// Bold is the text bold or not.
func (h *TermTextGridStyle) Bold() bool {
return h.IsBold
}

// Underlined is the text underlined or not.
func (h *TermTextGridStyle) Underlined() bool {
return h.IsUnderlined
}

// HighlightOption defines a function type that can modify a TermTextGridStyle.
type HighlightOption func(h *TermTextGridStyle)

Expand All @@ -214,7 +226,7 @@ type HighlightOption func(h *TermTextGridStyle)
// Returns:
//
// A pointer to a TermTextGridStyle initialized with the provided colors and inversion settings.
func NewTermTextGridStyle(fg, bg color.Color, bitmask byte, blinkEnabled bool) widget.TextGridStyle {
func NewTermTextGridStyle(fg, bg color.Color, bitmask byte, blinkEnabled, bold, underlined bool) widget.TextGridStyle {
// calculate the inverted colors
var invertedFg, invertedBg color.Color
if fg == nil {
Expand All @@ -236,6 +248,8 @@ func NewTermTextGridStyle(fg, bg color.Color, bitmask byte, blinkEnabled bool) w
Highlighted: false,
BlinkEnabled: blinkEnabled,
Blink: false,
IsBold: bold,
IsUnderlined: underlined,
}
}

Expand Down
7 changes: 2 additions & 5 deletions output.go
Original file line number Diff line number Diff line change
Expand Up @@ -217,18 +217,15 @@ func (t *Terminal) handleOutputChar(r rune) {
t.content.Rows = append(t.content.Rows, widget.TextGridRow{})
}

var cellStyle widget.TextGridStyle
cellStyle = &widget.CustomTextGridStyle{FGColor: t.currentFG, BGColor: t.currentBG}
cellStyle := widget2.NewTermTextGridStyle(t.currentFG, t.currentBG, t.highlightBitMask, t.blinking, t.bold, t.underlined)
for len(t.content.Rows[t.cursorRow].Cells)-1 < t.cursorCol {
newCell := widget.TextGridCell{
Rune: ' ',
Style: cellStyle,
}
t.content.Rows[t.cursorRow].Cells = append(t.content.Rows[t.cursorRow].Cells, newCell)
}
if t.blinking {
cellStyle = widget2.NewTermTextGridStyle(t.currentFG, t.currentBG, t.highlightBitMask, t.blinking)
}

t.content.SetCell(t.cursorRow, t.cursorCol, widget.TextGridCell{Rune: r, Style: cellStyle})
t.cursorCol++
}
Expand Down
1 change: 1 addition & 0 deletions term.go
Original file line number Diff line number Diff line change
Expand Up @@ -80,6 +80,7 @@ type Terminal struct {
bracketedPasteMode bool
state *parseState
blinking bool
underlined bool
}

// Cursor is used for displaying a specific cursor.
Expand Down

0 comments on commit 43baa67

Please sign in to comment.