-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
Showing
5 changed files
with
123 additions
and
32 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,29 +1,59 @@ | ||
package screen | ||
|
||
import "github.com/rivo/tview" | ||
import ( | ||
"scc/utils" | ||
"time" | ||
|
||
"github.com/gdamore/tcell/v2" | ||
"github.com/rivo/tview" | ||
) | ||
|
||
// Initial value, gets adjusted once it's known how much space is available | ||
var maxMessages = 20 | ||
|
||
type Cammie struct { | ||
ScreenApp *ScreenApp | ||
screenApp *ScreenApp | ||
view *tview.TextView | ||
|
||
queue *utils.Queue[string] | ||
text string | ||
buffer string | ||
} | ||
|
||
func NewCammie(screenApp *ScreenApp) *Cammie { | ||
cammie := Cammie{ | ||
ScreenApp: screenApp, | ||
view: tview.NewTextView().SetDynamicColors(true).SetRegions(true).SetWordWrap(true), | ||
screenApp: screenApp, | ||
view: tview.NewTextView().SetWrap(true).SetWordWrap(true).SetText("pls"), | ||
|
||
queue: utils.NewQueue[string](maxMessages), | ||
} | ||
|
||
cammie.view.SetTitle(" Cammie ") | ||
cammie.view.SetBorder(true) | ||
cammie.view.SetTextColor(tview.Styles.PrimaryTextColor) | ||
cammie.view.SetBorderColor(tview.Styles.BorderColor) | ||
cammie.view.SetTextColor(tcell.ColorOrange) | ||
cammie.view.SetBorderColor(tcell.ColorOrange) | ||
cammie.view.SetTitleColor(tcell.ColorOrange) | ||
|
||
return &cammie | ||
} | ||
|
||
func (cammie *Cammie) Run() { | ||
time.Sleep(5 * time.Second) | ||
|
||
_, _, _, h := cammie.view.GetInnerRect() | ||
cammie.queue.SetMaxSize(h) | ||
} | ||
|
||
func (cammie *Cammie) Update(text string) { | ||
cammie.view.SetText(text) | ||
func (cammie *Cammie) Update(message string) { | ||
cammie.queue.Enqueue(message) | ||
|
||
cammie.screenApp.execute(func() { | ||
cammie.screenApp.app.QueueUpdateDraw(func() { | ||
cammie.view.Clear() | ||
|
||
for _, message := range cammie.queue.Get() { | ||
cammie.view.Write([]byte(message + "\n")) | ||
} | ||
}) | ||
}) | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,52 @@ | ||
package utils | ||
|
||
type Queue[T any] struct { | ||
maxSize int | ||
Items []T | ||
} | ||
|
||
func NewQueue[T any](maxSize int) *Queue[T] { | ||
return &Queue[T]{ | ||
maxSize: maxSize, | ||
Items: make([]T, 0, maxSize), | ||
} | ||
} | ||
|
||
func (q *Queue[T]) Enqueue(item T) { | ||
if len(q.Items) >= q.maxSize { | ||
q.Items = q.Items[1:] | ||
} | ||
q.Items = append(q.Items, item) | ||
} | ||
|
||
func (q *Queue[T]) Dequeue() (T, bool) { | ||
if len(q.Items) == 0 { | ||
var zero T | ||
return zero, false | ||
} | ||
|
||
item := q.Items[0] | ||
q.Items = q.Items[1:] | ||
return item, true | ||
} | ||
|
||
func (q *Queue[T]) Peek() (T, bool) { | ||
if len(q.Items) == 0 { | ||
var zero T | ||
return zero, false | ||
} | ||
|
||
return q.Items[0], true | ||
} | ||
|
||
func (q *Queue[T]) Get() []T { | ||
return q.Items | ||
} | ||
|
||
func (q *Queue[T]) SetMaxSize(maxSize int) { | ||
q.maxSize = maxSize | ||
|
||
if len(q.Items) > maxSize { | ||
q.Items = q.Items[:maxSize] | ||
} | ||
} |