Velocity/core/components.py

73 lines
2.4 KiB
Python
Raw Normal View History

2022-03-19 15:13:28 +00:00
from core import utils
import curses
class StatusBar:
def __init__(self, instance):
self.mode = instance.mode.upper()
self.file = instance.buffer.name or "[No Name]"
self.icon = instance.config["icon"] or "λ"
2022-04-15 16:38:39 +01:00
self.theme = "default"
self.colors = [7, 5, 13]
self.components = [self.icon, self.mode, self.file]
def update(self, instance):
self.mode = instance.mode.upper()
2022-03-19 15:13:28 +00:00
self.components = [self.icon, self.mode, self.file]
2022-03-19 15:41:40 +00:00
def render(self, instance):
# Clear the status bar
2022-03-19 15:41:40 +00:00
utils.clear(instance, instance.height - 2, 0)
# Update variables
self.update(instance)
if self.theme == "inverted":
2022-04-15 16:38:39 +01:00
# Initialise the x position for each component
x = 1
# Render each component
for count, component in enumerate(self.components):
instance.screen.addstr(instance.height - 2, x, component,
curses.color_pair(self.colors[count]) | curses.A_BOLD)
x += len(component) + 1
else:
# Initialise temporary colors for inverted theme
colors = []
# Add 1 to each color temporarily
for color in self.colors:
colors.append(color + 1)
# Initialise the x position for each component
x = 0
2022-04-15 16:38:39 +01:00
# Render each component
for count, component in enumerate(self.components):
2022-04-15 16:38:39 +01:00
component = f" {component} "
instance.screen.addstr(instance.height - 2, x, component,
curses.color_pair(colors[count]) | curses.A_BOLD)
x += len(component)
# Add a space at the end of the status bar
instance.screen.addstr(instance.height - 2, x, " " * (instance.width - x),
curses.color_pair(2))
2022-03-19 15:13:28 +00:00
class Components:
def __init__(self, instance, components: dict = None):
2022-03-19 15:13:28 +00:00
self.components = components or {
"left": [" "],
"bottom": [StatusBar(instance)],
2022-03-19 15:13:28 +00:00
}
curses.endwin()
2022-03-19 15:13:28 +00:00
2022-03-19 19:19:25 +00:00
@staticmethod
def get_component_width(component: list) -> int:
return len(max(component))
2022-03-19 19:19:25 +00:00
2022-03-19 15:13:28 +00:00
def render(self, instance):
for component in self.components["bottom"]:
component.render(instance)