Comments (13)
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.
from pytermgui.
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.
Great. I'll try subclassing, thank you.
from pytermgui.
Hey there! Did it end up working?
from pytermgui.
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.
from pytermgui.
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.
Now the buttons don't show at all after that patch. eg sandbox/scrolling.py
from pytermgui.
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.
Works great, thanks.
from pytermgui.
Great to hear! Is there anything else I can do related to this issue, or can we close it?
from pytermgui.
from pytermgui.
Glad I could help then!
from pytermgui.
Related Issues (20)
- [BUG] Button label truncated due to long markup string HOT 2
- [REQUEST] Opening An Application In A Slot
- [BUG] Long unicode (emojis) get wrong length calculated HOT 1
- Enter doesn't activate buttons HOT 1
- Windows compatibility and example apps HOT 13
- tutorial HOT 10
- [REQUEST] Python 3.8 HOT 2
- [BUG] Cannot delete text in InputField HOT 11
- [BUG] [WSL2] XTerm does not display "box drawing" characters (but other terminals do) HOT 1
- [BUG] Inline widget: Mouse report event 'hover' is not supported HOT 1
- No documentation available HOT 4
- Question regarding `ptg.InputField` HOT 5
- How do I update the values screen HOT 1
- [HOWTO] How can I use the terminal widget? HOT 2
- Official Debian package HOT 1
- [REQUEST] Include pyproject.toml in pip package HOT 1
- [BUG] Multiline InputField fails to display correctly a in scrollable container HOT 2
- [BUG] All colors are mapped wrong on Windows when styling HOT 2
- [REQUEST] WindowManager.add unexpected behvaiour with slot names HOT 1
- [REQUEST] Browsing between selectables does not trigger scroll HOT 1
Recommend Projects
-
React
A declarative, efficient, and flexible JavaScript library for building user interfaces.
-
Vue.js
🖖 Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.
-
Typescript
TypeScript is a superset of JavaScript that compiles to clean JavaScript output.
-
TensorFlow
An Open Source Machine Learning Framework for Everyone
-
Django
The Web framework for perfectionists with deadlines.
-
Laravel
A PHP framework for web artisans
-
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.
-
Visualization
Some thing interesting about visualization, use data art
-
Game
Some thing interesting about game, make everyone happy.
Recommend Org
-
Facebook
We are working to build community through open source technology. NB: members must have two-factor auth.
-
Microsoft
Open source projects and samples from Microsoft.
-
Google
Google ❤️ Open Source for everyone.
-
Alibaba
Alibaba Open Source for everyone
-
D3
Data-Driven Documents codes.
-
Tencent
China tencent open source team.
from pytermgui.