diff options
Diffstat (limited to 'market.py')
-rwxr-xr-x | market.py | 41 |
1 files changed, 27 insertions, 14 deletions
@@ -1,13 +1,14 @@ | |||
1 | #!/usr/bin/python | 1 | #!/usr/bin/python |
2 | import argparse | 2 | import argparse |
3 | from collections import namedtuple | 3 | from collections import namedtuple |
4 | from datetime import datetime | ||
4 | import requests | 5 | import requests |
5 | import os | 6 | import os |
6 | import sys | 7 | import sys |
7 | import time | 8 | import time |
8 | 9 | ||
9 | from textual.app import App, ComposeResult | 10 | from textual.app import App, ComposeResult |
10 | from textual.widgets import DataTable | 11 | from textual.widgets import DataTable, Label |
11 | 12 | ||
12 | # Rapid API key must be set in the environment. | 13 | # Rapid API key must be set in the environment. |
13 | RAPIDAPI_KEY = os.environ.get('RAPIDAPI_KEY') | 14 | RAPIDAPI_KEY = os.environ.get('RAPIDAPI_KEY') |
@@ -212,6 +213,11 @@ def update_state(t: float, updaters: list[Updater], state: State) -> State: | |||
212 | 213 | ||
213 | class MarketApp(App): | 214 | class MarketApp(App): |
214 | TITLE = "Market Watch" | 215 | TITLE = "Market Watch" |
216 | CSS = """ | ||
217 | #footer { | ||
218 | dock: bottom; | ||
219 | } | ||
220 | """ | ||
215 | 221 | ||
216 | def __init__(self, updaters: list[Updater]): | 222 | def __init__(self, updaters: list[Updater]): |
217 | super().__init__() | 223 | super().__init__() |
@@ -220,9 +226,29 @@ class MarketApp(App): | |||
220 | self.updaters = updaters | 226 | self.updaters = updaters |
221 | self.min_update_delay = min([updater.delay for updater in updaters]) | 227 | self.min_update_delay = min([updater.delay for updater in updaters]) |
222 | 228 | ||
229 | def compose(self) -> ComposeResult: | ||
230 | table = DataTable() | ||
231 | table.show_cursor = False | ||
232 | self.table = table | ||
233 | yield table | ||
234 | |||
235 | footer = Label(id="footer") | ||
236 | self.footer = footer | ||
237 | yield footer | ||
238 | |||
239 | def on_mount(self): | ||
240 | self.state = load_state(STATE_FILE) | ||
241 | self.update() | ||
242 | self.render() | ||
243 | self.set_interval(self.min_update_delay, self.update) | ||
244 | |||
223 | def render(self): | 245 | def render(self): |
224 | assert self.state is not None | 246 | assert self.state is not None |
225 | assert self.table is not None | 247 | assert self.table is not None |
248 | assert self.footer is not None | ||
249 | |||
250 | update_time = datetime.now() | ||
251 | self.footer.update(f"Last update: {update_time}") | ||
226 | 252 | ||
227 | # Stock/ex | Price | Change | 253 | # Stock/ex | Price | Change |
228 | # xyz | xxx | xxx | 254 | # xyz | xxx | xxx |
@@ -236,19 +262,6 @@ class MarketApp(App): | |||
236 | for exchange in self.state.exchanges: | 262 | for exchange in self.state.exchanges: |
237 | table.add_row(format_exchange_name(exchange), exchange.rate, "", "") | 263 | table.add_row(format_exchange_name(exchange), exchange.rate, "", "") |
238 | 264 | ||
239 | def compose(self) -> ComposeResult: | ||
240 | self.state = load_state(STATE_FILE) | ||
241 | |||
242 | table = DataTable() | ||
243 | table.show_cursor = False | ||
244 | self.table = table | ||
245 | yield table | ||
246 | |||
247 | self.render() | ||
248 | |||
249 | self.update() | ||
250 | self.set_interval(self.min_update_delay, self.update) | ||
251 | |||
252 | def update(self) -> None: | 265 | def update(self) -> None: |
253 | t = time.time() | 266 | t = time.time() |
254 | self.state = update_state(t, self.updaters, self.state) | 267 | self.state = update_state(t, self.updaters, self.state) |