Comments (7)
What was your use case? It looks like the calls can be swapped:
>>> print clint.textui.colored.red("{}".format("hello").rjust(20))
hello
>>> print clint.textui.colored.red("{}".format("hello").rjust(12))
hello
>>> print "{}".format("hello").rjust(20)
hello
Sounds like the docs should have a warning about this. Is there interest in making this work? Seems pretty messy, but I guess a different class could be used for the output of colored.red which implements it's own rjust etc. and keeps track of the normal string part and the terminal color characters.
from clint.
Yeah, a custom class would work. Any chance the colored
classes could implement that method?
from clint.
That approach would make more sense to me. That doesn't make your example work though, does it?
The result of
"{}".format(colored.red("hello"))
is a string, which we couldn't call a custom rjust
on. That would allow
"{}".format(colored.red("hello").rjust(20))
to work though.
from clint.
Yup, you can't make the escape code characters just disappear. :-)
from clint.
What do you think about defining __getattr__
such that any string method would work on a colored string?
Like this:
def __getattr__(self, att):
def func_help(*args, **kwargs):
getatt = getattr(self.s, att)(*args, **kwargs)
return ColoredString(self.color, getatt)
return func_help
This code above solves the .ljust() and .rjust() problem. After some editing and discussion with thomasballinger , it also works for all other string methods with two exceptions and one note:
- any string method that returns a list would be dealt with the same way .split() currently works.
- .join() does not work with ColoredStrings
__add__
adds plain strings, not ColoredStrings, but I would think that's the functionality that would be wanted anyway
Also have a fix for just this bug, but if you're amenable to this idea, it seems useful. Is this fix interesting to you? Or would you prefer that the code were clearer or more separate?
from clint.
Great idea! I think that follows the principle of least surprise, so I'm all for it.
from clint.
see #57
from clint.
Related Issues (20)
- Colors in columns not working?
- Pypi package is outdated HOT 5
- Use proper terminal description rather than `sys.platform` to determine ANSI code support
- Boolean return values swapped for prompt.yn with default="n" HOT 2
- clint.textui.formatter.max_width: unexpected newline when using colorred_string HOT 2
- clint 0.4.1 on pypi is not actul with master repo HOT 2
- Bold attribute does not work when newline
- Problem when attempting to capture stdout in TestCase using "puts"
- Support bold (or underline) text with no color specification
- Support for custom validators
- IndexError while running unicode.py from examples
- Rename / rebrand project? HOT 3
- input() color not working
- Confusing regexp in .textui.colored.clean() HOT 3
- Default on prompt.yn returns True always HOT 1
- PyPi module update HOT 1
- Hide progress after end
- Documentation please
- comparison using is/is not with string literal rasises SyntaxWarning
- bug in progress.bar function 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 clint.