Chat Widget, Cli improvements
This commit is contained in:
264
chat.go
Normal file
264
chat.go
Normal file
@@ -0,0 +1,264 @@
|
||||
/*
|
||||
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 (
|
||||
"fmt"
|
||||
"time"
|
||||
|
||||
h "git.bullercodeworks.com/brian/tcell-widgets/helpers"
|
||||
"github.com/gdamore/tcell"
|
||||
)
|
||||
|
||||
type Chat struct {
|
||||
id string
|
||||
style tcell.Style
|
||||
|
||||
x, y int
|
||||
w, h int
|
||||
active bool
|
||||
visible bool
|
||||
|
||||
title string
|
||||
rawLog []string
|
||||
log []*ChatMsg
|
||||
logPosition int
|
||||
|
||||
value string
|
||||
cursor int
|
||||
|
||||
history []ChatMsg
|
||||
historyPosition int
|
||||
|
||||
keyMap KeyMap
|
||||
}
|
||||
|
||||
func NewChat(id string, s tcell.Style) *Chat {
|
||||
ret := &Chat{}
|
||||
ret.Init(id, s)
|
||||
return ret
|
||||
}
|
||||
|
||||
func (w *Chat) Init(id string, s tcell.Style) {
|
||||
w.id, w.style = id, s
|
||||
w.visible = true
|
||||
w.initKeyMap()
|
||||
}
|
||||
|
||||
func (w *Chat) Id() string { return w.id }
|
||||
func (w *Chat) HandleResize(ev *tcell.EventResize) {}
|
||||
func (w *Chat) HandleKey(ev *tcell.EventKey) bool {
|
||||
if !w.active {
|
||||
return false
|
||||
}
|
||||
if h.IsKey(*ev, tcell.KeyEsc) {
|
||||
w.SetActive(false)
|
||||
return true
|
||||
}
|
||||
if h.IsBS(*ev) {
|
||||
if w.cursor > 0 {
|
||||
w.cursor--
|
||||
}
|
||||
if w.cursor < len(w.value) {
|
||||
w.value = w.value[:w.cursor] + w.value[w.cursor+1:]
|
||||
}
|
||||
}
|
||||
if ok := w.keyMap.Handle(ev); ok {
|
||||
return true
|
||||
}
|
||||
|
||||
var ch string
|
||||
if h.KeyIsDisplayable(*ev) {
|
||||
ch = string(ev.Rune())
|
||||
if w.cursor < len(w.value) {
|
||||
strPt1 := w.value[:w.cursor]
|
||||
strPt2 := w.value[w.cursor:]
|
||||
w.value = fmt.Sprintf("%s%s%s", strPt1, ch, strPt2)
|
||||
} else {
|
||||
w.value = fmt.Sprintf("%s%s", w.value, ch)
|
||||
}
|
||||
w.cursor++
|
||||
return true
|
||||
}
|
||||
return false
|
||||
}
|
||||
func (w *Chat) HandleTime(ev *tcell.EventTime) {}
|
||||
|
||||
func (w *Chat) Draw(screen tcell.Screen) {
|
||||
if !w.visible {
|
||||
return
|
||||
}
|
||||
dStyle := w.style
|
||||
if !w.active {
|
||||
dStyle = dStyle.Dim(true)
|
||||
}
|
||||
if w.title != "" {
|
||||
h.TitledBorderFilled(w.x, w.y, w.x+w.w-1, w.y+w.h, w.title, h.BRD_SIMPLE, dStyle, screen)
|
||||
} else {
|
||||
h.BorderFilled(w.x, w.y, w.x+w.w-1, w.y+w.h, h.BRD_SIMPLE, dStyle, screen)
|
||||
}
|
||||
|
||||
x, y := w.x+1, w.y+1+w.h-3
|
||||
for i := 0; i < w.h-2; i++ {
|
||||
if len(w.log) > i {
|
||||
line := w.log[len(w.log)-i-1].String()
|
||||
if len(line) > w.w-2 {
|
||||
line = line[:w.w-2]
|
||||
}
|
||||
h.DrawText(x, y, h.PadR(line, w.w), dStyle, screen)
|
||||
y--
|
||||
}
|
||||
}
|
||||
y = w.y + w.h - 1
|
||||
cursor := " "
|
||||
pre := "> "
|
||||
var post string
|
||||
if len(w.value) > 0 {
|
||||
pre = fmt.Sprintf("%s%s", pre, w.value[:w.cursor])
|
||||
if w.cursor < len(w.value) {
|
||||
cursor = string(w.value[w.cursor])
|
||||
post = w.value[w.cursor+1:]
|
||||
}
|
||||
}
|
||||
h.DrawText(x, y, pre, dStyle, screen)
|
||||
x += len(pre)
|
||||
h.DrawText(x, y, cursor, dStyle.Reverse(w.active), screen)
|
||||
x += 1
|
||||
h.DrawText(x, y, h.PadR(post, w.w-x), dStyle, screen)
|
||||
// x += len(post) - 1
|
||||
}
|
||||
|
||||
func (w *Chat) Active() bool { return w.active }
|
||||
func (w *Chat) SetActive(a bool) { w.active = a }
|
||||
func (w *Chat) Visible() bool { return w.visible }
|
||||
func (w *Chat) SetVisible(a bool) { w.visible = a }
|
||||
func (w *Chat) Focusable() bool { return true }
|
||||
func (w *Chat) SetX(x int) { w.x = x }
|
||||
func (w *Chat) SetY(y int) { w.y = y }
|
||||
func (w *Chat) GetX() int { return w.x }
|
||||
func (w *Chat) GetY() int { return w.y }
|
||||
func (w *Chat) SetPos(c Coord) { w.x, w.y = c.X, c.Y }
|
||||
func (w *Chat) GetW() int { return w.w }
|
||||
func (w *Chat) GetH() int { return w.h }
|
||||
func (w *Chat) SetW(wd int) { w.w = wd }
|
||||
func (w *Chat) SetH(h int) { w.h = h }
|
||||
func (w *Chat) SetSize(c Coord) { w.w, w.h = c.X, c.Y }
|
||||
func (w *Chat) WantW() int { return w.w }
|
||||
func (w *Chat) WantH() int { return w.h }
|
||||
|
||||
func (w *Chat) initKeyMap() {
|
||||
w.keyMap = NewKeyMap(map[tcell.Key]func() bool{
|
||||
tcell.KeyEsc: func() bool {
|
||||
w.SetActive(false)
|
||||
return true
|
||||
},
|
||||
tcell.KeyUp: func() bool {
|
||||
if w.historyPosition < len(w.history)-1 {
|
||||
w.historyPosition++
|
||||
w.value = w.history[w.historyPosition].Message
|
||||
w.cursor = len(w.value)
|
||||
return true
|
||||
}
|
||||
return false
|
||||
},
|
||||
tcell.KeyDown: func() bool {
|
||||
if w.historyPosition > -1 {
|
||||
w.historyPosition--
|
||||
if w.historyPosition == -1 {
|
||||
w.value = ""
|
||||
} else {
|
||||
w.value = w.history[w.historyPosition].Message
|
||||
}
|
||||
w.cursor = len(w.value)
|
||||
return true
|
||||
}
|
||||
return false
|
||||
},
|
||||
tcell.KeyLeft: func() bool {
|
||||
if w.cursor > 0 {
|
||||
w.cursor--
|
||||
return true
|
||||
}
|
||||
return false
|
||||
},
|
||||
tcell.KeyRight: func() bool {
|
||||
if w.cursor < len(w.value) {
|
||||
w.cursor++
|
||||
return true
|
||||
}
|
||||
return false
|
||||
},
|
||||
tcell.KeyCtrlU: func() bool {
|
||||
w.value = w.value[w.cursor:]
|
||||
w.cursor = 0
|
||||
return true
|
||||
},
|
||||
tcell.KeyTab: func() bool {
|
||||
// Auto-complete
|
||||
// TODO: Find best guess for current word
|
||||
return false
|
||||
},
|
||||
tcell.KeyEnter: func() bool {
|
||||
// TODO: Submit the message
|
||||
return false
|
||||
},
|
||||
})
|
||||
}
|
||||
func (w *Chat) SetTitle(ttl string) { w.title = ttl }
|
||||
func (w *Chat) Title() string { return w.title }
|
||||
func (w *Chat) SetValue(val string) {
|
||||
w.value = val
|
||||
w.cursor = len(val)
|
||||
}
|
||||
|
||||
func (w *Chat) Log(msg *ChatMsg) {
|
||||
w.rawLog = append(w.rawLog, msg.String())
|
||||
w.log = append(w.log, msg)
|
||||
}
|
||||
|
||||
func (w *Chat) Clear() {
|
||||
w.rawLog = []string{}
|
||||
w.log = []*ChatMsg{}
|
||||
}
|
||||
|
||||
const (
|
||||
MsgChat = iota
|
||||
MsgSystem
|
||||
)
|
||||
|
||||
type ChatMsg struct {
|
||||
MsgType int
|
||||
MsgTime time.Time
|
||||
FromName string
|
||||
Message string
|
||||
}
|
||||
|
||||
func (c ChatMsg) String() string {
|
||||
if c.MsgType == MsgChat {
|
||||
from := c.FromName
|
||||
if from == "" {
|
||||
from = "Unknown"
|
||||
}
|
||||
return fmt.Sprintf("%s <%s> %s", c.MsgTime.Format("15:04"), from, c.Message)
|
||||
}
|
||||
return fmt.Sprintf("%s", c.Message)
|
||||
}
|
||||
Reference in New Issue
Block a user