File: sstext.go

package info (click to toggle)
golang-github-gdamore-tcell 1.3.0-3
  • links: PTS, VCS
  • area: main
  • in suites: bookworm, sid, trixie
  • size: 892 kB
  • sloc: sh: 16; makefile: 2
file content (126 lines) | stat: -rw-r--r-- 3,779 bytes parent folder | download | duplicates (3)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
// Copyright 2016 The Tcell Authors
//
// Licensed under the Apache License, Version 2.0 (the "License");
// you may not use file except in compliance with the License.
// You may obtain a copy of the license at
//
//    http://www.apache.org/licenses/LICENSE-2.0
//
// Unless required by applicable law or agreed to in writing, software
// distributed under the License is distributed on an "AS IS" BASIS,
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
// See the License for the specific language governing permissions and
// limitations under the License.

package views

import (
	"github.com/gdamore/tcell"
	"unicode"
)

// SimpleStyledText is a form of Text that offers highlighting of the text
// using simple in-line markup.  Its intention is to make it easier to mark
// up hot // keys for menubars, etc.
type SimpleStyledText struct {
	styles map[rune]tcell.Style
	markup []rune
	Text
}

// SetMarkup sets the text used for the string.  It applies markup as follows
// (modeled on tcsh style prompt markup):
//
// * %% - emit a single % in current style
// * %N - normal style
// * %A - alternate style
// * %S - start standout (reverse) style
// * %B - start bold style
// * %U - start underline style
//
// Other styles can be set using %<rune>, if styles are registered.
// Upper case characters and punctuation are reserved for use by the system.
// Lower case are available for use by the user.  (Users may define mappings
// for upper case letters to override system defined styles.)
//
// Note that for simplicity, combining styles is not supported.  By default
// the alternate style is the same as standout (reverse) mode.
//
// Arguably we could have used Markdown syntax instead, but properly doing all
// of Markdown is not trivial, and these escape sequences make it clearer that
// we are not even attempting to do that.
func (t *SimpleStyledText) SetMarkup(s string) {

	markup := []rune(s)
	styl := make([]tcell.Style, 0, len(markup))
	text := make([]rune, 0, len(markup))

	style := t.styles['N']

	esc := false
	for _, r := range markup {
		if esc {
			esc = false
			switch r {
			case '%':
				text = append(text, '%')
				styl = append(styl, style)
			default:
				style = t.styles[r]
			}
			continue
		}
		switch r {
		case '%':
			esc = true
			continue
		default:
			text = append(text, r)
			styl = append(styl, style)
		}
	}

	t.Text.SetText(string(text))
	for i, s := range styl {
		t.SetStyleAt(i, s)
	}
	t.markup = markup
}

// Registers a style for the given rune.  This style will be used for
// text marked with %<r>. See SetMarkup() for more detail.  Note that
// this must be done before using any of the styles with SetMarkup().
// Only letters may be used when registering styles, and be advised that
// the system may have predefined uses for upper case letters.
func (t *SimpleStyledText) RegisterStyle(r rune, style tcell.Style) {
	if r == 'N' {
		t.Text.SetStyle(style)
	}
	if unicode.IsLetter(r) {
		t.styles[r] = style
	}
}

// LookupStyle returns the style registered for the given rune.
// Returns tcell.StyleDefault if no style was previously registered
// for the rune.
func (t *SimpleStyledText) LookupStyle(r rune) tcell.Style {
	return t.styles[r]
}

// Markup returns the text that was set, including markup.
func (t *SimpleStyledText) Markup() string {
	return string(t.markup)
}

// NewSimpleStyledText creates an empty Text.
func NewSimpleStyledText() *SimpleStyledText {
	ss := &SimpleStyledText{}
	// Create map and establish default styles.
	ss.styles = make(map[rune]tcell.Style)
	ss.styles['N'] = tcell.StyleDefault
	ss.styles['S'] = tcell.StyleDefault.Reverse(true)
	ss.styles['U'] = tcell.StyleDefault.Underline(true)
	ss.styles['B'] = tcell.StyleDefault.Bold(true)
	return ss
}