114 lines
3.8 KiB
Go
114 lines
3.8 KiB
Go
/*
|
|
Copyright © Brian Buller <brian@bullercodeworks.com>
|
|
|
|
Permission is hereby granted, free of charge, to any person obtaining a copy
|
|
of this software and associated documentation files (the "Software"), to deal
|
|
in the Software without restriction, including without limitation the rights
|
|
to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
|
|
copies of the Software, and to permit persons to whom the Software is
|
|
furnished to do so, subject to the following conditions:
|
|
|
|
The above copyright notice and this permission notice shall be included in
|
|
all copies or substantial portions of the Software.
|
|
|
|
THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
|
|
IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
|
|
FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
|
|
AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
|
|
LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
|
|
OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
|
|
THE SOFTWARE.
|
|
*/
|
|
package widgets
|
|
|
|
import (
|
|
"strings"
|
|
|
|
wh "git.bullercodeworks.com/brian/tcell-widgets/helpers"
|
|
"github.com/gdamore/tcell"
|
|
)
|
|
|
|
// Currently the text widget will only split up text on newlines
|
|
// TODO: Add 'wrap' mode, which will automatically split it into lines.
|
|
type Text struct {
|
|
id string
|
|
text string
|
|
message []string
|
|
style tcell.Style
|
|
x, y int
|
|
w, h int
|
|
visible bool
|
|
active bool
|
|
focusable bool
|
|
keyMap *KeyMap
|
|
}
|
|
|
|
var _ Widget = (*Text)(nil)
|
|
|
|
func NewText(id string, style tcell.Style) *Text {
|
|
ret := &Text{}
|
|
ret.Init(id, style)
|
|
return ret
|
|
}
|
|
|
|
func (w *Text) Init(id string, style tcell.Style) {
|
|
w.id = id
|
|
w.style = style
|
|
w.visible = true
|
|
w.focusable = false
|
|
w.keyMap = BlankKeyMap()
|
|
}
|
|
|
|
func (w *Text) Id() string { return w.id }
|
|
func (w *Text) HandleResize(ev *tcell.EventResize) { w.w, w.h = ev.Size() }
|
|
|
|
func (w *Text) GetKeyMap() *KeyMap { return w.keyMap }
|
|
func (w *Text) SetKeyMap(km *KeyMap) { w.keyMap = km }
|
|
|
|
func (w *Text) HandleKey(ev *tcell.EventKey) bool { return w.keyMap.Handle(ev) }
|
|
func (w *Text) HandleTime(ev *tcell.EventTime) {}
|
|
func (w *Text) Draw(screen tcell.Screen) {
|
|
if !w.visible {
|
|
return
|
|
}
|
|
y := w.y
|
|
for i := range w.message {
|
|
wh.DrawText(w.x+(w.w/2)-(len(w.message[i])/2), y, w.message[i], w.style, screen)
|
|
y++
|
|
}
|
|
}
|
|
|
|
func (w *Text) Active() bool { return w.active }
|
|
func (w *Text) SetActive(a bool) { w.active = a }
|
|
func (w *Text) Visible() bool { return w.visible }
|
|
func (w *Text) SetVisible(a bool) { w.visible = a }
|
|
func (w *Text) SetX(x int) { w.x = x }
|
|
func (w *Text) SetY(y int) { w.y = y }
|
|
func (w *Text) GetX() int { return w.x }
|
|
func (w *Text) GetY() int { return w.y }
|
|
func (w *Text) GetPos() Coord { return Coord{X: w.x, Y: w.y} }
|
|
func (w *Text) SetPos(c Coord) { w.x, w.y = c.X, c.Y }
|
|
func (w *Text) SetW(x int) { w.w = x }
|
|
func (w *Text) SetH(y int) { w.h = y }
|
|
func (w *Text) GetW() int { return w.w }
|
|
func (w *Text) GetH() int { return w.h }
|
|
func (w *Text) WantW() int { return wh.Longest(w.message) }
|
|
func (w *Text) WantH() int { return len(w.message) }
|
|
func (w *Text) SetSize(c Coord) { w.w, w.h = c.X, c.Y }
|
|
func (w *Text) Focusable() bool { return w.focusable }
|
|
func (w *Text) SetFocusable(b bool) { w.focusable = b }
|
|
func (w *Text) MinW() int { return wh.Longest(w.message) }
|
|
func (w *Text) MinH() int { return len(w.message) }
|
|
|
|
func (w *Text) SetText(txt string) {
|
|
w.text = txt
|
|
if strings.Contains(w.text, "\n") {
|
|
w.message = strings.Split(w.text, "\n")
|
|
} else {
|
|
w.message = []string{w.text}
|
|
}
|
|
}
|
|
|
|
func (w *Text) GetText() string { return w.text }
|
|
func (w *Text) GetMessage() []string { return w.message }
|