2021-11-13 11:22:16 -07:00
|
|
|
package main
|
|
|
|
|
|
|
|
import (
|
|
|
|
"time"
|
|
|
|
|
2021-11-15 09:18:17 -07:00
|
|
|
"github.com/aditya-K2/tview"
|
2021-11-13 11:22:16 -07:00
|
|
|
"github.com/gdamore/tcell/v2"
|
|
|
|
)
|
|
|
|
|
|
|
|
/* Notification Primitive */
|
|
|
|
type Notification struct {
|
|
|
|
*tview.Box
|
|
|
|
Text string
|
|
|
|
}
|
|
|
|
|
|
|
|
/* Get A Pointer to A Notification Struct */
|
|
|
|
func NewNotification(s string) *Notification {
|
|
|
|
return &Notification{
|
|
|
|
Box: tview.NewBox(),
|
|
|
|
Text: s,
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
/* Draw Function for the Notification Primitive */
|
|
|
|
func (self *Notification) Draw(screen tcell.Screen) {
|
|
|
|
termDetails := getWidth()
|
|
|
|
|
|
|
|
var (
|
|
|
|
COL int = int(termDetails.Col)
|
|
|
|
TEXTLENGTH int = len(self.Text)
|
|
|
|
HEIGHT int = 3
|
|
|
|
TEXTPOSITION int = 2
|
|
|
|
)
|
|
|
|
|
|
|
|
self.Box.SetBackgroundColor(tcell.GetColor("#15191a"))
|
|
|
|
self.SetRect(COL-(TEXTLENGTH+7), 1, TEXTLENGTH+4, HEIGHT)
|
|
|
|
self.DrawForSubclass(screen, self.Box)
|
|
|
|
tview.Print(screen, self.Text,
|
|
|
|
COL-(TEXTLENGTH+5), TEXTPOSITION, TEXTLENGTH,
|
|
|
|
tview.AlignCenter, tcell.GetColor("#ffffff"))
|
|
|
|
}
|
|
|
|
|
|
|
|
/* Notification Server : Not an actual Server*/
|
|
|
|
type NotificationServer struct {
|
|
|
|
c chan string
|
|
|
|
}
|
|
|
|
|
|
|
|
/* Get A Pointer to a NotificationServer Struct */
|
|
|
|
func NewNotificationServer() *NotificationServer {
|
|
|
|
return &NotificationServer{
|
|
|
|
c: make(chan string),
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
/* This Method Starts the go routine for the NotificationServer */
|
|
|
|
func (self *NotificationServer) Start() {
|
|
|
|
go NotificationRoutine(self.c, "EMPTY NOTIFICATION")
|
|
|
|
}
|
|
|
|
|
|
|
|
/* The Notification Server is just a string channel and the NotificationRoutine
|
|
|
|
the channel is used to receive the Notification Data through the Send Function
|
|
|
|
The Channel keeps listening for the Notification when it receives a Notification it checks if it
|
|
|
|
is Empty or not if it is an empty Notification it calls the NotificationRoutine with the empty routine else
|
|
|
|
it will call the go routine that renders the Notification for the Notification Interval and agains start listening
|
|
|
|
for the notfications sort of works like a que */
|
|
|
|
func NotificationRoutine(c chan string, s string) {
|
|
|
|
if s != "EMPTY NOTIFICATION" {
|
|
|
|
go func() {
|
|
|
|
currentTime := time.Now().String()
|
|
|
|
UI.Pages.AddPage(currentTime, NewNotification(s), false, true)
|
|
|
|
UI.App.SetFocus(UI.ExpandedView)
|
|
|
|
time.Sleep(time.Second * 1)
|
|
|
|
UI.Pages.RemovePage(currentTime)
|
|
|
|
UI.App.SetFocus(UI.ExpandedView)
|
|
|
|
}()
|
|
|
|
}
|
|
|
|
NewNotification := <-c
|
|
|
|
if NewNotification == "EMPTY NOTIFICATION" {
|
|
|
|
NotificationRoutine(c, "EMPTY NOTIFICATION")
|
|
|
|
} else {
|
|
|
|
NotificationRoutine(c, NewNotification)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
/* Sends the Notification to the Notification Server */
|
|
|
|
func (self NotificationServer) Send(text string) {
|
|
|
|
self.c <- text
|
|
|
|
}
|