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

Support (and tests) for tuples with nested styles #45

Open
wants to merge 1 commit into
base: master
Choose a base branch
from

Conversation

jimallman
Copy link

In some cases, it would be nice if we could "nest" styles by passing tuples, like so:

# creates a red phrase with some bold words in the middle:
t.red('This is ', t.bold('extremely important'), ' information!')

# deeper nesting of styles (green, then bold, then underline, and back out)
t.green('foo', t.bold('bar', t.underline('baz'), 'herp'), 'derp')

I've added tests with and without TTY, and so far it seems robust. Thoughts?

@jimallman
Copy link
Author

Hi Erik, I just noticed this PR is languishing. Is there anything i can do to make it more palatable, or is this just out of scope for the tool?

@erikrose
Copy link
Owner

erikrose commented Apr 9, 2014

My fault; I just got distracted with other libraries. I'll try to have a look after PyCon. Sorry!

which should be simple strings or FormattingStrings (allows nested
calls for "inner" style).

For example, this creates a red phrase with some bold words in the middle:
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Can you make this rst happy? (double the colons) add a new line after 478 and make sure 479 is 4 spaces in.

It's a nit that I'd do myself, but I have another request below...

@davedash
Copy link
Collaborator

In general I like this, but only because there's no nice way of doing this with a single string format.

The fact that you reset just what's in the nest, IMO, is the value add.

Would be nice to be able to use your fancy resetting to let something like this work:

"{t.red}This {t.bold}is important{t.no_bold}!".format(t=terminal)

but that's another pull request for another time...

if self._normal:
# "refresh" my style in the inner text by inserting it after each 'normal' (reset) code
refresh = self._normal + self
args = [refresh.join(a.split(self._normal)) for a in args]
Copy link
Owner

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

What a crazy idea. It's backtracking, but I do believe it'll work. This lets us compose formatted strings without having to go down the road of assembling a syntax tree and keeping it off to the side someplace. This is a very promising functionality/complexity tradeoff. I'm impressed.

jquast added a commit to jquast/blessed that referenced this pull request Oct 10, 2015
@jimallman brings us this convenience of allowing existing
strings to be joined as a call parameter argument to a
FormattingString, allowing nestation:

This was rejected upstream as erikrose#45

```python
t.red('This is ', t.bold('extremely important'), ' information!')

t.green('foo', t.bold('bar', t.underline('baz'), 'herp'), 'derp')
```
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

Successfully merging this pull request may close these issues.

4 participants