cchat-gtk/internal/ui/service/session/server/commander/buffer.go

64 lines
1.4 KiB
Go
Raw Normal View History

2020-07-01 01:09:22 +00:00
package commander
import (
2020-10-15 06:32:11 +00:00
"bytes"
2020-07-01 01:09:22 +00:00
"fmt"
"github.com/diamondburned/cchat"
"github.com/diamondburned/cchat-gtk/internal/ui/rich"
2020-07-01 01:09:22 +00:00
"github.com/gotk3/gotk3/gtk"
)
2020-10-15 06:32:11 +00:00
// Buffer represents an unbuffered API around the text buffer.
2020-07-01 01:09:22 +00:00
type Buffer struct {
*gtk.TextBuffer
name rich.LabelStateStorer
2020-10-15 06:32:11 +00:00
cmder cchat.Commander
2020-07-01 01:09:22 +00:00
}
// NewBuffer creates a new buffer with the given SessionCommander, or returns
// nil if cmder is nil.
func NewBuffer(name rich.LabelStateStorer, cmder cchat.Commander) *Buffer {
2020-07-01 01:09:22 +00:00
if cmder == nil {
return nil
}
b, _ := gtk.TextBufferNew(nil)
b.CreateTag("error", map[string]interface{}{
"foreground": "#FF0000",
})
b.CreateTag("system", map[string]interface{}{
"foreground": "#808080",
})
2020-10-15 06:32:11 +00:00
return &Buffer{b, name, cmder}
2020-07-01 01:09:22 +00:00
}
// WriteError is not thread-safe.
func (b *Buffer) WriteError(err error) {
b.InsertWithTagByName(b.GetEndIter(), err.Error()+"\n", "error")
}
// WriteSystem is not thread-safe.
func (b *Buffer) WriteSystem(bytes []byte) {
b.InsertWithTagByName(b.GetEndIter(), string(bytes), "system")
}
2020-10-15 06:32:11 +00:00
// Systemlnf is not thread-safe.
func (b *Buffer) Systemlnf(f string, v ...interface{}) {
2020-07-01 01:09:22 +00:00
b.WriteSystem([]byte(fmt.Sprintf(f+"\n", v...)))
}
2020-10-15 06:32:11 +00:00
func (b *Buffer) WriteOutput(output []byte) {
var iter = b.GetEndIter()
b.Insert(iter, string(output))
if !bytes.HasSuffix(output, []byte("\n")) {
b.Insert(iter, "\n")
}
2020-07-01 01:09:22 +00:00
}
func (b *Buffer) ShowDialog() {
SpawnDialog(b)
}