forked from block/goose
-
Notifications
You must be signed in to change notification settings - Fork 0
/
healthbar.py
50 lines (39 loc) · 1.45 KB
/
healthbar.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
import time
import threading
class HealthBar:
def __init__(self, sessionName, showHealthBar=True):
self.sessionName = sessionName
self.exchangeTokenCount = 0
self.totalCost = 0.0
self.sessionStartTime = time.time()
self.isRunning = False
self.isGenerating = False
self.showHealthBar = showHealthBar
self._stopFlag = threading.Event()
def startSession(self):
self.isRunning = True
self.sessionStartTime = time.time()
def stopSession(self):
self.isRunning = False
def startGenerating(self):
self.isGenerating = True
def stopGenerating(self):
self.isGenerating = False
def addTokens(self, count):
self.exchangeTokenCount += count
def addCost(self, cost):
self.totalCost += cost
def getSessionUptime(self):
if not self.isRunning:
return 0
return time.time() - self.sessionStartTime
def displayHealthBar(self):
if not self.showHealthBar:
return
while not self._stopFlag.is_set():
uptime = self.getSessionUptime()
status = f"Session: {self.sessionName} | Tokens: {self.exchangeTokenCount} | Cost: ${self.totalCost:.2f} | Uptime: {uptime:.2f}s | Running: {self.isRunning} | Generating: {self.isGenerating}"
print(status, end="\r")
time.sleep(1) # Update every second
def stopDisplay(self):
self._stopFlag.set()