Coder Social home page Coder Social logo

[QUESTION] Truncate on width about pytermgui HOT 13 CLOSED

bczsalba avatar bczsalba commented on September 17, 2024
[QUESTION] Truncate on width

from pytermgui.

Comments (13)

bczsalba avatar bczsalba commented on September 17, 2024

I think the "cleanest" way to achieve this would be by setting static_width for each Button. Sadly, as far as I can remember this won't actually do anything. Please let me know if I'm wrong though, I might have implemented it at some point.

If that doesn't work, what I personally would do is do some preprocessing on the lines of data you have. You can, for example, slice them to your target width - 3, and add ... to the end to indicate "more". Something like the below should work:

max_width = ...
for line in data:
    if len(line) <= max_width:
        continue
    line = line[:max_width - 3] + "..."

Technically, you can also do this slicing within a Button subclass' get_lines:

def get_lines(self) -> list[str]:
    full = self.label

    if len(full) > MAX_WIDTH:
        self.label = self.label[:MAX_WIDTH - 3] + '...'

    lines = super().get_lines()
    self.label = original

    return lines

I am writing this on my phone, so apologies if formatting of the code is weird/broken. The idea should be there.

Thanks for using the library, and feel free to ask about anything else that might be unclear!

from pytermgui.

jonnieey avatar jonnieey commented on September 17, 2024

from pytermgui.

bczsalba avatar bczsalba commented on September 17, 2024

the strings don't scale with window on resize

Actually, I think if you used the subclassing method that should no longer be the case!

Is that something that you plan on adding?

For sure, though the timeline is unclear at the moment. I'm currently hard at work on layouts and highlighters, next update is planned to be about colorschemes and general color related business, but I might be able to sneak a ScrollView in, depending on the implementation difficulty.

One thing though: As far as I've tested, terminals don't send horizontal scroll events, only vertical ones. So you sadly won't be able to use the touchpad to scroll left or right, which will probably feel a bit clunky. There will be scrollbars for it though, so it should be alright.

from pytermgui.

jonnieey avatar jonnieey commented on September 17, 2024

Great. I'll try subclassing, thank you.

from pytermgui.

bczsalba avatar bczsalba commented on September 17, 2024

Hey there! Did it end up working?

from pytermgui.

jonnieey avatar jonnieey commented on September 17, 2024
import pytermgui as ptg
from pytermgui.window_manager.layouts import Layout

class MainWindow(ptg.Window):

    title: "Title"
    overflow = ptg.Overflow.SCROLL

    def __init__(self, *widgets, **attrs):
        super().__init__(*widgets, **attrs)
        self.box = "DOUBLE"

class CustomButton(ptg.Button):
    def __init__(self, label, max_width, **attrs):
        self.max_width = max_width
        super().__init__(label, **attrs)

    def get_lines(self):
        original = self.label
        if len(original) > self.max_width:
            self.label = self.label[:self.max_width] + '...'
        lines = super().get_lines()
        self.label = original

        return lines

def client_container(clients: list = []):
    scrollview = ptg.Container(overflow=ptg.Overflow.SCROLL, box="DOUBLE", height=10)
    t = "The quick brown fox jumped over the lazy dog on a bike and did not return until the end of the line."
    for i in clients:
        scrollview.lazy_add(CustomButton(t, max_width=scrollview.width))
    return scrollview

def app_layout():
    layout = Layout()
    layout.add_slot("Clients", height=30)
    layout.add_slot("Jobs", height=30)

    return layout

def main():
    with ptg.WindowManager() as manager:
        manager.layout = app_layout()
        window = MainWindow(client_container([1, 2, 3]))
        manager.add(window)
        window2 = MainWindow(client_container([1, 3]))
        manager.add(window2)

main()

So this is what I've come up with so far. Subclassing works but only upto the initial width of the container. I've been trying to figure out how to update the width of container as the window is resized.

2022-05-09-014558

from pytermgui.

bczsalba avatar bczsalba commented on September 17, 2024

One thing I could suggest is instead of passing the width value by name, maybe wrap it into a callable that can be called every time Button.get_lines is called. That way it is always up to date, which should be what you are looking for.

from pytermgui.

jonnieey avatar jonnieey commented on September 17, 2024

Now the buttons don't show at all after that patch. eg sandbox/scrolling.py

from pytermgui.

bczsalba avatar bczsalba commented on September 17, 2024

Now the buttons don't show at all after that patch. eg sandbox/scrolling.py

Now that you say it, I'm pretty sure I forgot to give the buttons a default width. Will fix that tomorrow, but until then just setting some width to each button should fix it. Sorry!

from pytermgui.

jonnieey avatar jonnieey commented on September 17, 2024

Works great, thanks.

from pytermgui.

bczsalba avatar bczsalba commented on September 17, 2024

Great to hear! Is there anything else I can do related to this issue, or can we close it?

from pytermgui.

jonnieey avatar jonnieey commented on September 17, 2024

from pytermgui.

bczsalba avatar bczsalba commented on September 17, 2024

Glad I could help then!

from pytermgui.

Related Issues (20)

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    🖖 Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. 📊📈🎉

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google ❤️ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.