Repository navigation
Expand file tree
/
Copy pathmodel.py
More file actions
66 lines (50 loc) · 1.85 KB
/
Copy pathmodel.py
File metadata and controls
66 lines (50 loc) · 1.85 KB
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
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
"""Model protocol for Bubble Tea applications."""
from abc import ABC, abstractmethod
from typing import TYPE_CHECKING, Optional, Tuple
if TYPE_CHECKING:
from .commands import Cmd
from .messages import Msg
class Model(ABC):
"""
Abstract base class for Bubble Tea models.
A Model represents the state of your application. You must implement
three methods:
- init(): Returns an optional initial command
- update(msg): Handles messages and returns updated model + optional command
- view(): Returns a string representation of the UI
"""
@abstractmethod
def init(self) -> Optional["Cmd"]:
"""
Initialize the model. Called once when the program starts.
Returns an optional Cmd to perform initial I/O.
Return None for no initial command.
"""
pass
@abstractmethod
def update(self, msg: "Msg") -> Tuple["Model", Optional["Cmd"]]:
"""
Update the model based on a message.
Args:
msg: The message to process
Returns:
A tuple of (updated_model, optional_command)
"""
pass
@abstractmethod
def view(self) -> str:
"""
Render the model as a string for display.
The returned string must end with a trailing newline (``\n``).
The renderer counts newlines to determine how many lines to erase
on the next frame; omitting the trailing newline causes the count
to be off by one, making the UI drift down by one line per redraw.
The renderer auto-corrects a missing trailing newline, but relying
on this is not recommended.
Example::
def view(self) -> str:
return f"Counter: {self.count}\n"
Returns:
A string representation of the current UI state, ending with ``\n``.
"""
pass