1
1
mirror of https://github.com/walles/moar.git synced 2024-10-06 00:20:58 +03:00
moar/twin/cell.go
Johan Walles cc6819efd0 Display Private Use Unicode characters
Before this change, we rendered those as highlighted question marks.

With this change in place, we just send them to the screen for
displaying.

The reason is that Font Awesome uses these for symbols, this one for
example: https://fontawesome.com/v4/icon/battery-empty

More reading: https://en.wikipedia.org/wiki/Private_Use_Areas
2023-02-28 19:28:37 +01:00

73 lines
1.4 KiB
Go

package twin
import (
"fmt"
"unicode"
)
// Cell is a rune with a style to be written to a cell on screen
type Cell struct {
Rune rune
Style Style
}
func NewCell(rune rune, style Style) Cell {
return Cell{
Rune: rune,
Style: style,
}
}
func (cell Cell) String() string {
return fmt.Sprint("rune='", string(cell.Rune), "' ", cell.Style)
}
// Returns a slice of cells with trailing whitespace cells removed
func TrimSpaceRight(cells []Cell) []Cell {
for i := len(cells) - 1; i >= 0; i-- {
cell := cells[i]
if !unicode.IsSpace(cell.Rune) {
return cells[0 : i+1]
}
// That was a space, keep looking
}
// All whitespace, return empty
return []Cell{}
}
// Returns a slice of cells with leading whitespace cells removed
func TrimSpaceLeft(cells []Cell) []Cell {
for i := 0; i < len(cells); i++ {
cell := cells[i]
if !unicode.IsSpace(cell.Rune) {
return cells[i:]
}
// That was a space, keep looking
}
// All whitespace, return empty
return []Cell{}
}
func Printable(char rune) bool {
if unicode.IsPrint(char) {
return true
}
if unicode.Is(unicode.Co, char) {
// Co == "Private Use": https://www.compart.com/en/unicode/category
//
// This space is used by Font Awesome, for "fa-battery-empty" for
// example: https://fontawesome.com/v4/icon/battery-empty
//
// So we want to print these and let the rendering engine deal with
// outputting them in a way that's helpful to the user.
return true
}
return false
}