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

Plumbum.color #206

Closed
wants to merge 89 commits into from
Closed

Plumbum.color #206

wants to merge 89 commits into from

Conversation

henryiii
Copy link
Collaborator

This is the plumbum.colors module I've been working on, designed to make adding colors easy and safe. I've prepared a post about it here, this post has been updated, and there is extensive documentation in the addition. As a quick example:

from plumbum import colors
print("This is red" << colors.red + "This is not red")
with (colors.blue+colors.bold):
    print("Styles can be combined, and are context managers.")

You can access the ANSI 16 basic colors, 256 colors (with names), and full 24 bit color - and can convert between them (This is the only library I know of that can do that). You can easily subclass Style to create more representations (HTMLStyle is included to make documentation easier to write). Styles are smart and know how to combine to create the most effective ANSI sequence. Styles correctly handle being turned off, and don't turn on for non-posix tty systems (can be forced, though). Basic colors can be used on Windows with a tool (like colorama) to convert ANSI codes. (See examples).

Docs (with list of color names), API docs, tests, examples included.

Closes #128

Henry Schreiner (Muon Linux) and others added 30 commits July 13, 2015 17:00
…thods, full property name support, properties now dynamic, some support for setting .use_color
Conflicts:
	docs/_news.rst
	docs/local_commands.rst
Conflicts:
	.travis.yml
	CHANGELOG.rst
	plumbum/machines/base.py
	tests/test_local.py
@henryiii
Copy link
Collaborator Author

If it was a stand-alone package, it couldn't a.) be integrated with plumbum.cli, and b.) it would run in to the same problem as colorama and all other color packages: they have to be installed to be used, and they are generally "non-essential", and would require a dependency that you would have to check for. If a script depends on plumbum, now it has guaranteed easy access to color.

The bracket syntax works just fine, try it. :) (red + bold)["red and bold"]

* or << is easier when actually writing code IMO, because you don't have to wrap it.

Instead of having an experimental directory, how about making an experimental branch, and then PRing parts into the main branch when ready? That way testing it would be easy, just checkout a branch. Putting things in a separate directory breaks all test scripts, docs, code, etc. I'll set it up if you want me to. I think we really should have two branches at least, a master that mostly takes bug fixes, and a experimental/dev that takes new features and matures them.

(PS: It's also easy to checkout my fork branch into a local branch with a different name, that's how I am working now)

@henryiii
Copy link
Collaborator Author

PS: With the exception of the CLI integration, tests, and docs, everything new is contained in plumbum.color. If I rename the COLOR object to color, I'll need a new directory name. style? colorlib?

The COLOR object is much more powerful than a module could be, for example:

with COLOR:
    COLOR.RED()
    print("This is in red")
    COLOR["Plum4"]()
    print("This is in plum, an extended color")
print("This is fully reset")

And, of course, you can set up new ones with one line, look at HTMLCOLOR, for example.

Also, several color packages seemed to use uppercase names, and colors are "constants" (or, at least, in the first iteration of the package they were str subclasses, so they were), so that's why they are uppercase. That's easy to change.

@henryiii
Copy link
Collaborator Author

The 16 default colors, attributes, and reset are all available in plumbum.color now. For the extended colors, rgb, hex colors, color iteration, etc. still require the COLOR object (which is still called COLOR for now).

Suggested name change:

  • COLOR -> colors
  • Uppercase color names -> lowercase color names

Then you could do from plumbum.color import colors for all the colors and methods. (or from plumbum import colors for short)

Since I'll have to change all docs, tests, etc., would like feedback before making that change. :)

@henryiii
Copy link
Collaborator Author

I've renamed all colors and attributes to lowercase, and I've even made the module syntax work. So from plumbum.colors import red or even from plumbum.colors.bg import blue works now! (similar to plumbum.cmd trick). All the non-module like features, like with colors: are still supported.

Note that you still have to do import plumbum.colors or from plumbum import colors, simply import plumbum and then using plumbum.colors does not work (intentional currently, adding one line to __init__.py would cause this to work, but this way the colors library doesn't have to load when using plumbum).

@henryiii
Copy link
Collaborator Author

I've added access to colors to the command line, too, using

$ python -m plumbum.colors

This was always available as an emergency terminal recovery (no arguments is still full reset), but selecting all available styles and using colors instead of colorlib is new. tom Have you had time to look at it yet? If you need prebuilt access to the docs, they are at http://henryiiiplumbum.readthedocs.org/en/latest/.

@henryiii henryiii modified the milestone: v1.6.0 Aug 27, 2015
@henryiii henryiii mentioned this pull request Aug 27, 2015
@henryiii
Copy link
Collaborator Author

Closing in favor of new PR from color branch.

@henryiii henryiii closed this Aug 27, 2015
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.

3 participants