Skip to content

Commit

Permalink
Added copy text to clipboard button
Browse files Browse the repository at this point in the history
Fixed bug with starting text
Updated text showing container sizes to reflect the new inner/outer system
  • Loading branch information
edward-jazzhands committed Oct 27, 2024
1 parent e5d31e1 commit a01fad8
Show file tree
Hide file tree
Showing 6 changed files with 60 additions and 46 deletions.
5 changes: 5 additions & 0 deletions CHANGELOG
Original file line number Diff line number Diff line change
@@ -1,3 +1,8 @@
# 2024-10-26 0.4.2
- Added copy text to clipboard button
- Fixed bug with starting text
- Updated text showing container sizes to reflect the new inner/outer system

# 2024-10-26 0.4.0
- Enormous improvement to container logic with inner/outer containers.
- Fixed up docstrings in numerous places
Expand Down
2 changes: 1 addition & 1 deletion pyproject.toml
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
[tool.poetry]
name = "textual-pyfiglet"
version = "0.4.0"
version = "0.4.2"
description = "A Widget implementation of PyFiglet for Textual"
authors = ["edward-jazzhands <ed.jazzhands@gmail.com>"]
license = "MIT"
Expand Down
45 changes: 26 additions & 19 deletions textual_pyfiglet/__main__.py
Original file line number Diff line number Diff line change
Expand Up @@ -72,10 +72,11 @@ def compose(self):
with Vertical(id="sidebar"):
yield Label("Set container width:", classes="sidebar_label")
yield Input(id="width_input", classes="sidebar_input")
yield Label("Set container height:", classes="sidebar_label")
yield Label("\nSet container height:", classes="sidebar_label")
yield Input(id="height_input", classes="sidebar_input")
yield Label("\nLeave blank for auto", classes="sidebar_label")
yield Button("Set", id="set_button")
yield Label("\nLeave blank\n for auto", classes="sidebar_label")
yield Button("Set", id="set_button", classes="sidebar_button")
yield Button("Copy text\nto clipboard", id="copy_button", classes="sidebar_button")

with VerticalScroll(id="main_window"):
yield FigletWidget("Starter Text", id="figlet_widget")
Expand All @@ -95,14 +96,17 @@ def compose(self):

def on_mount(self):

self.figlet_widget = cast(FigletWidget, self.query_one("#figlet_widget"))
self.font_select = cast(Select, self.query_one("#font_select"))
self.text_input = cast(TextArea, self.query_one("#text_input")) # chad type hinting convenience vars
self.font_switch = cast(Switch, self.query_one("#switch"))
self.figlet_widget = cast(FigletWidget, self.query_one("#figlet_widget"))
self.font_select = cast(Select, self.query_one("#font_select"))
self.text_input = cast(TextArea, self.query_one("#text_input")) # chad type hinting convenience vars
self.font_switch = cast(Switch, self.query_one("#switch"))
self.notification1 = cast(Label, self.query_one("#notification_box1"))
self.notification2 = cast(Label, self.query_one("#notification_box2"))
self.width_input = cast(Input, self.query_one("#width_input"))
self.height_input = cast(Input, self.query_one("#height_input"))
self.width_input = cast(Input, self.query_one("#width_input"))
self.height_input = cast(Input, self.query_one("#height_input"))

self.figlet_widget._inner_figlet.tooltip = "Inner Figlet Widget"
self.figlet_widget.tooltip = "Outer Figlet Widget"

self.fonts_list = self.figlet_widget.get_fonts_list(get_all=True)
self.base_fonts = self.figlet_widget.get_fonts_list(get_all=False)
Expand Down Expand Up @@ -143,26 +147,29 @@ def toggle_fonts(self, event: Switch.Changed) -> None:
else:
self.show_notification1("Scanning folder... Note the Extended fonts are not installed.")

@on(Button.Pressed)
@on(Button.Pressed, selector="#set_button")
def set_container_size(self):
width = self.width_input.value
height = self.height_input.value
self.log(f"Setting container size to: ({width} x {height})")
if width:
self.figlet_widget.styles.width = int(width)
else:
self.figlet_widget.set_styles('width: 1fr;')
if height:
self.figlet_widget.styles.height = int(height)
if not width:
self.figlet_widget.set_styles('width: 1fr;')
if not height:
else:
self.figlet_widget.set_styles('height: auto;')

self.figlet_widget.update()
@on(Button.Pressed, selector="#copy_button")
def copy_text(self):
self.figlet_widget.copy_figlet_to_clipboard()

@on(FigletWidget.Updated)
def figlet_updated(self, event: FigletWidget.Updated):
width, height = event.widget.size
self.show_notification2(f"width: {width}, height: {height}")
outer_width, outer_height = event.widget.size
inner_width, inner_height = event.widget._inner_figlet.size
self.show_notification2(f"Outer: {outer_width}W x {outer_height}H | Inner: {inner_width}W x {inner_height}H")

@on(TextArea.Changed)
async def text_updated(self):
Expand All @@ -182,9 +189,9 @@ def show_notification1(self, message: str):

def show_notification2(self, message: str):
self.notification2.update(message)
if self.timer2:
self.timer2.stop()
self.timer2 = self.set_timer(3, self.clear_notification2)
# if self.timer2:
# self.timer2.stop()
# self.timer2 = self.set_timer(3, self.clear_notification2)

def clear_notification1(self):
self.notification1.update('')
Expand Down
45 changes: 25 additions & 20 deletions textual_pyfiglet/figletwidget.py
Original file line number Diff line number Diff line change
Expand Up @@ -25,7 +25,8 @@ def __init__(self, *args, font, justify, **kwargs) -> None:
"""Private class for the FigletWidget.
Args:
renderable: A Rich renderable, or string containing console markup.
font (PyFiglet): Font to use for the ASCII art. Default is "calvin_s".
font (PyFiglet): Font to use for the ASCII art.
justify (PyFiglet): Justification for the text.
expand: Expand content if required to fill container.
shrink: Shrink content if required to fill container.
markup: True if markup should be parsed and rendered.
Expand All @@ -35,16 +36,11 @@ def __init__(self, *args, font, justify, **kwargs) -> None:
disabled: Whether the static is disabled or not."""

super().__init__(*args, **kwargs)
self.stored_text = None # we init with no stored text
self.font = font
self.justify = justify
self.figlet = Figlet(font=font, justify=justify)

def update(self, new_text: str | None = None) -> None:
def update(self, new_text) -> None:
"""Custom update method for the FigletWidget.
This method is private so docstring is in the FigletWidget class."""
if new_text is not None:
self.stored_text = new_text

# for dev debugging
# self.log.debug(
Expand All @@ -53,11 +49,13 @@ def update(self, new_text: str | None = None) -> None:
# )

if self.parent.size.width == 0:
self.log.error('parent.size.width is 0. Exiting update.')
return
self.figlet.width = self.parent.size.width

self.renderable = self.figlet.renderText(self.stored_text)
if new_text == '':
self.renderable = ''
else:
self.renderable = self.figlet.renderText(new_text)

# this line is very key to the widget resizing properly
# activates textual's layout system in some magical way.
Expand Down Expand Up @@ -105,6 +103,7 @@ class Updated(Message):
def __init__(self, widget: FigletWidget) -> None:
super().__init__()
self.widget = widget
'''The FigletWidget that was updated.'''

@property
def control(self) -> FigletWidget:
Expand Down Expand Up @@ -154,23 +153,24 @@ def __init__(self, *args, font: str = "calvin_s", justify: str = "center", **kwa
"""
super().__init__(*args, **kwargs)
self.stored_text = str(self.renderable)
self.renderable = ''
self.font = font
self.justify = justify

# NOTE: Figlet also has a "direction" argument
# TODO Add Direction arguments

def compose(self):
yield _InnerFiglet(self.stored_text, id='inner_figlet', font=self.font, justify=self.justify)
self._inner_figlet = _InnerFiglet(id='inner_figlet', font=self.font, justify=self.justify)
yield self._inner_figlet

def on_mount(self):
self._inner_figlet = cast(_InnerFiglet, self.query_one('#inner_figlet'))
self.update(new_text=self.stored_text)
self.update(self.stored_text)

def on_resize(self):
self._inner_figlet.update()
self._inner_figlet.update(self.stored_text)

def update(self, new_text: str|None = None) -> None:
def update(self, new_text) -> None:
'''Update the PyFiglet area with the new text.
Note that this over-rides the standard update method in the Static widget.
Unlike the Static widget, this method does not take a Rich renderable.
Expand All @@ -179,9 +179,9 @@ def update(self, new_text: str|None = None) -> None:
Args:
new_text: The text to update the PyFiglet widget with. Default is None.'''

if new_text is not None:
self.stored_text = new_text
self._inner_figlet.update(new_text=self.stored_text)
self.stored_text = new_text
self._inner_figlet.update(self.stored_text)
self.post_message(self.Updated(self))

def set_font(self, font: str) -> None:
"""Set the font for the PyFiglet widget.
Expand All @@ -194,7 +194,7 @@ def set_font(self, font: str) -> None:
font: The name of the font to set."""

self._inner_figlet.figlet.setFont(font=font)
self.update()
self.update(self.stored_text)

def set_justify(self, justify: str) -> None:
"""Set the justification for the PyFiglet widget.
Expand All @@ -206,8 +206,8 @@ def set_justify(self, justify: str) -> None:
Args:
justify: The justification to set."""

self._inner_figlet.figlet.setJustify(justify=justify)
self.update()
self._inner_figlet.figlet.justify = justify
self.update(self.stored_text)

def get_fonts_list(self, get_all: bool = True) -> list:
"""Scans the fonts folder.
Expand All @@ -228,3 +228,8 @@ def get_fonts_list(self, get_all: bool = True) -> list:
if filename.endswith('.flf') or filename.endswith('.tlf'):
fonts_list.append(os.path.splitext(filename)[0])
return fonts_list

def copy_figlet_to_clipboard(self) -> None:
"""Copy the PyFiglet text to the clipboard."""
self.app.copy_to_clipboard(str(self._inner_figlet.renderable))

4 changes: 2 additions & 2 deletions textual_pyfiglet/styles.tcss
Original file line number Diff line number Diff line change
Expand Up @@ -15,7 +15,7 @@
width: 20;
}

#set_button {
.sidebar_button {
align: center bottom;
margin: 1 0 0 0;
}
Expand Down Expand Up @@ -47,7 +47,7 @@
#sidebar {
padding: 0 1 0 2;
margin: 0;
width: 17;
width: 18;
height: 1fr;
align: left middle;
background: $panel;
Expand Down
5 changes: 1 addition & 4 deletions todo.md
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,4 @@

- Change code in PyFiglet to look for extended fonts in more than one folder,
So that we don't need to copy them into the main fonts folder.
- Get original Pyfiglet test suite fully working
- Add in more options avaible in PyFiglet
ie Justify, Direction
-Improve the widget update method to not require a timer. Hopefully by getting better messages from the event system.
- Get original Pyfiglet test suite fully working

0 comments on commit a01fad8

Please sign in to comment.