1
0
Fork 0
mirror of https://github.com/diamondburned/cchat-gtk.git synced 2024-11-01 03:54:16 +00:00
cchat-gtk/internal/ui/service/session/server/commander/buffer.go

63 lines
1.3 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/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
2020-10-15 06:32:11 +00:00
name string
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.
2020-10-15 06:32:11 +00:00
func NewBuffer(name string, 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)
}