Initial Commit
This commit is contained in:
118
widgets/json_content.go
Normal file
118
widgets/json_content.go
Normal file
@@ -0,0 +1,118 @@
|
||||
/*
|
||||
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"
|
||||
|
||||
wd "git.bullercodeworks.com/brian/tcell-widgets"
|
||||
wh "git.bullercodeworks.com/brian/tcell-widgets/helpers"
|
||||
"github.com/gdamore/tcell"
|
||||
)
|
||||
|
||||
// TODO: Format contents as json. For now this is a duplice of the Text widget
|
||||
type JsonContent struct {
|
||||
id string
|
||||
text string
|
||||
contents []string
|
||||
style tcell.Style
|
||||
x, y int
|
||||
w, h int
|
||||
visible bool
|
||||
active bool
|
||||
focusable bool
|
||||
keyMap *wd.KeyMap
|
||||
|
||||
flags wd.LayoutFlag
|
||||
}
|
||||
|
||||
var _ wd.Widget = (*JsonContent)(nil)
|
||||
|
||||
func NewJsonContent(id string, style tcell.Style) *JsonContent {
|
||||
ret := &JsonContent{}
|
||||
ret.Init(id, style)
|
||||
return ret
|
||||
}
|
||||
|
||||
func (w *JsonContent) Init(id string, style tcell.Style) {
|
||||
w.id = id
|
||||
w.style = style
|
||||
w.visible = true
|
||||
w.focusable = false
|
||||
w.keyMap = wd.BlankKeyMap()
|
||||
}
|
||||
|
||||
func (w *JsonContent) Id() string { return w.id }
|
||||
func (w *JsonContent) HandleResize(ev *tcell.EventResize) { w.w, w.h = ev.Size() }
|
||||
|
||||
func (w *JsonContent) GetKeyMap() *wd.KeyMap { return w.keyMap }
|
||||
func (w *JsonContent) SetKeyMap(km *wd.KeyMap) { w.keyMap = km }
|
||||
|
||||
func (w *JsonContent) HandleKey(ev *tcell.EventKey) bool { return w.keyMap.Handle(ev) }
|
||||
func (w *JsonContent) HandleTime(ev *tcell.EventTime) {}
|
||||
func (w *JsonContent) Draw(screen tcell.Screen) {
|
||||
if !w.visible {
|
||||
return
|
||||
}
|
||||
y := w.y
|
||||
for i := range w.contents {
|
||||
wh.DrawText(w.x, y, w.contents[i], w.style, screen)
|
||||
y++
|
||||
}
|
||||
}
|
||||
|
||||
func (w *JsonContent) Active() bool { return w.active }
|
||||
func (w *JsonContent) SetActive(a bool) { w.active = a }
|
||||
func (w *JsonContent) Visible() bool { return w.visible }
|
||||
func (w *JsonContent) SetVisible(a bool) { w.visible = a }
|
||||
func (w *JsonContent) SetX(x int) { w.x = x }
|
||||
func (w *JsonContent) SetY(y int) { w.y = y }
|
||||
func (w *JsonContent) GetX() int { return w.x }
|
||||
func (w *JsonContent) GetY() int { return w.y }
|
||||
func (w *JsonContent) GetPos() wd.Coord { return wd.Coord{X: w.x, Y: w.y} }
|
||||
func (w *JsonContent) SetPos(c wd.Coord) { w.x, w.y = c.X, c.Y }
|
||||
func (w *JsonContent) SetW(x int) { w.w = x }
|
||||
func (w *JsonContent) SetH(y int) { w.h = y }
|
||||
func (w *JsonContent) GetW() int { return w.w }
|
||||
func (w *JsonContent) GetH() int { return w.h }
|
||||
func (w *JsonContent) WantW() int { return wh.Longest(w.contents) }
|
||||
func (w *JsonContent) WantH() int { return len(w.contents) }
|
||||
func (w *JsonContent) SetSize(c wd.Coord) { w.w, w.h = c.X, c.Y }
|
||||
func (w *JsonContent) Focusable() bool { return w.focusable }
|
||||
func (w *JsonContent) SetFocusable(b bool) { w.focusable = b }
|
||||
func (w *JsonContent) MinW() int { return wh.Longest(w.contents) }
|
||||
func (w *JsonContent) MinH() int { return len(w.contents) }
|
||||
|
||||
func (w *JsonContent) SetJsonContent(txt string) {
|
||||
w.text = txt
|
||||
if strings.Contains(w.text, "\n") {
|
||||
w.contents = strings.Split(w.text, "\n")
|
||||
} else {
|
||||
w.contents = []string{w.text}
|
||||
}
|
||||
}
|
||||
|
||||
func (w *JsonContent) GetJsonContent() string { return w.text }
|
||||
func (w *JsonContent) GetContents() []string { return w.contents }
|
||||
|
||||
func (w *JsonContent) AddFlag(f wd.LayoutFlag) { w.flags.Add(f) }
|
||||
func (w *JsonContent) RemoveFlag(f wd.LayoutFlag) { w.flags.Remove(f) }
|
||||
Reference in New Issue
Block a user