Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Header Expansion #4611

Closed
MatixAndr09 opened this issue Jun 5, 2024 · 6 comments
Closed

Header Expansion #4611

MatixAndr09 opened this issue Jun 5, 2024 · 6 comments

Comments

@MatixAndr09
Copy link

MatixAndr09 commented Jun 5, 2024

Hi everyone!

So I was working on a project using textual, and I saw that 'Header' compared to other elements like 'Button' or 'Static' is neglected. So I want to add a little of work to the Header. First of the icon of the header '⭘' should be customizable so you can enter as an argument to header -> 'yield Header(header_icon='your_icon_here')', second I would change the clock to be possible to show in these formats:

  1. Time 24h -> '22:22:22'
  2. Time 12h -> '10:22:22 PM'
  3. Date Simple + Time 24h -> '05.06 22:22:22'
  4. Date Simple + Time 12h -> '05.06 10:22:22 PM'
  5. Full Date + Time 24h -> '05.06.2024 22:22:22'
  6. Full Date + Time 12h -> 05.06.2024 10:22:22 PM'

so for example you want the 6 format so you do -> 'yield Header(show_clock=True,clock_style="full_date time_12h")'

That's all. I think I can do that my self. Let me know what you think.

Copy link

github-actions bot commented Jun 5, 2024

We found the following entry in the FAQ which you may find helpful:

Feel free to close this issue if you found an answer in the FAQ. Otherwise, please give us a little time to review.

This is an automated reply, generated by FAQtory

@TomJGooding
Copy link
Contributor

TomJGooding commented Jun 5, 2024

This issue from some months ago might be relevant here - unfortunately I know that Textual haven't had the resources to review the Header widget as planned.

@ThisModernDay
Copy link

ThisModernDay commented Jun 5, 2024

Might be worth noting you can make the time in the Header toggleable between 12hr and 24hr like this
You would obviously need to import everything this is just a rough type up from my phone and this code would need to be inside a class

from rich.text import Text

BINDINGS = [ ( "F12", "toggle_12hr_time", "Toggle 12/24 HR")]
def compose(self) -> ComposeResult:
    yield Header()

def on_mount(self):
    self.timefmt = False

def render_12hour_format(self):
    return Text(datetime.now().time().strftime("%I:%M %p"))

def render_24hour_format(self):
    return Text(datetime.now().time().strftime("%X"))

def action_toggle_12hr_time(self) -> None:
        self.timefmt = not self.timefmt
        self.query_one('Header HeaderClock').render = self.render_12hour_format if self.timefmt else self.render_24hour_format

In theory you can do the same for the command palette

@MatixAndr09
Copy link
Author

Okay but what about the icon

@ThisModernDay
Copy link

ThisModernDay commented Jun 6, 2024

It could be done like this.

from textual.app import App, ComposeResult
from textual.widgets import Header
from rich.text import Text

class MyApp(App):
    def on_mount(self) -> None:
        self.query_one("Header HeaderIcon").render = self.change_button

    def change_button(self):
        return Text("🦕")

    def compose(self) -> ComposeResult:
        yield Header()

if __name__ == "__main__":
    MyApp().run()

which gives you this
image

However I suggest you look at the widget files here I say that because this is how you can learn to tweak things. I knew I could change the header Icon by looking at the code here

the HeaderIcon is a class inside the header so i know that I can find it in a query

This approach is known as Monkey Patching

In Python, the term monkey patch only refers to dynamic modifications of a class or module at runtime, motivated by the intent to patch existing third-party code as a workaround to a bug or feature which does not act as you desire.

you may want to read up on Monkey Patching

Copy link

github-actions bot commented Jun 6, 2024

Don't forget to star the repository!

Follow @textualizeio for Textual updates.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

3 participants