Giter Club home page Giter Club logo

Comments (7)

thomasballinger avatar thomasballinger commented on September 3, 2024

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.

eproxus avatar eproxus commented on September 3, 2024

Yeah, a custom class would work. Any chance the colored classes could implement that method?

from clint.

thomasballinger avatar thomasballinger commented on September 3, 2024

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.

eproxus avatar eproxus commented on September 3, 2024

Yup, you can't make the escape code characters just disappear. :-)

from clint.

aerenchyma avatar aerenchyma commented on September 3, 2024

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.

eproxus avatar eproxus commented on September 3, 2024

Great idea! I think that follows the principle of least surprise, so I'm all for it.

from clint.

thomasballinger avatar thomasballinger commented on September 3, 2024

see #57

from clint.

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.