Colors

610 views
Skip to first unread message

Gianluca Romanin

unread,
Jul 19, 2013, 4:46:31 AM7/19/13
to npys...@googlegroups.com
Hi all,

I really think that npyscreen is a valuable module.
I just need a tip: how can I print the output of a command with standard ANSI characters for colors on a nypyscreen's widget?
Currently the ANSI color chars are not evaluated but just printed.
I'm wondering if colorama can help in this. Answer is probably no, because from what I know colorama adds an evaluation at the last part of the print buffer,
that seems not to be used in curses print.
Someone can hint me?

Thanks!

Gianluca Romanin

Nicholas Cole

unread,
Jul 21, 2013, 5:40:29 AM7/21/13
to npys...@googlegroups.com
Dear Gianluca,

You are right - npyscreen doesn't currently evaluate ANSI colour escapes (or indeed any escapes).  If you needed to to this, it would require a new widget type.  I'm happy to add one if you can show me why you need it.  My worry is that it would be very fragile, and could lead to odd results on different terminals.

Can you not use the npyscreen color system to do what you need?  For example the annotated text widgets?  

What is the kind of output you are trying to display?  If you can give more details, I can be more helpful. 

Best wishes,

Nicholas

 

Gianluca Romanin

unread,
Jul 21, 2013, 1:39:40 PM7/21/13
to npys...@googlegroups.com
Hi Nicholas,
Thank you for the answer!
More detail: I'm trying to print the output coming from a git log command that already have ANSI chars. I would like to print the colors as they are. Do you suggest to use annotation and replace ANSI chars with npyscreen color notation?
It could be a solution. It could be also a standard behaviour of npyscreen?

Thank you!

Nicholas Cole

unread,
Jul 22, 2013, 2:46:57 AM7/22/13
to npys...@googlegroups.com
Yes, I would ignore the ANSI colours, and use npyscreen to colour your data for you.  

There are two options.  If all of the data is of the form:

Keyword: Details

and you just want the Keyword in a particular colour, I would use the annotated classes.

But if you want more complex system of colour, you could try the (experimental) syntax highlighting of the textbox class.

No one has ever asked for this before, so it has remained experimental, but I've put an example of what can be done at the end of this email.

If that is the kind of thing you want, you can use that class as the basis for a multiline display class quite simply.

Best wishes,

Nicholas

#!/usr/bin/env python
# encoding: utf-8
import curses
import npyscreen
#npyscreen.disableColor()

class SyntaxTest(npyscreen.Textfield):
    def update_highlighting(self, start, end):
        # highlighting color
        hl_color  = self.parent.theme_manager.findPair(self, 'IMPORTANT')
        hl_colorb = self.parent.theme_manager.findPair(self, 'WARNING')
        hl_colorc = self.parent.theme_manager.findPair(self, 'CRITICAL')
        
        self._highlightingdata = [curses.A_BOLD, 
                        curses.A_BOLD,
                        hl_color,
                        hl_color,
                        hl_color,
                        hl_color,
                        hl_colorb,
                        hl_colorb,
                        hl_colorc,
                        hl_colorc,
                        hl_colorc,
                        hl_colorc,
        ]


class TestApp(npyscreen.NPSApp):
    def main(self):
        F = npyscreen.Form(name = "Welcome to Npyscreen",)
        t = F.add(SyntaxTest, name = "Text:", value="This is just some text")
        t.syntax_highlighting = True
        
        F.edit()


if __name__ == "__main__":
    App = TestApp()
    App.run()   


####################################

To use that highlighting class as the basis of a multiline class do something like:

class MyDisplayClass(npyscreen.MultiLine):
    _contained_widgets = SyntaxTest




 

Gianluca Romanin

unread,
Jul 25, 2013, 10:19:44 AM7/25/13
to npys...@googlegroups.com
Thank you, Nicholas!

I think I must use the second option with syntax highlighting of the textbox class.
In fact I have no precise ouput (as you can think of a table with a fixed number of columns) but a text in which
I have to color some things.
I will definitely try to use the highlight approach, thanks.

Best regards,

Gianluca

Nicholas Cole

unread,
Jul 25, 2013, 11:04:14 AM7/25/13
to npys...@googlegroups.com
On Thu, Jul 25, 2013 at 3:19 PM, Gianluca Romanin <roma...@gmail.com> wrote:
Thank you, Nicholas!

I think I must use the second option with syntax highlighting of the textbox class.
In fact I have no precise ouput (as you can think of a table with a fixed number of columns) but a text in which
I have to color some things.
I will definitely try to use the highlight approach, thanks.

Best regards,

Gianluca


I'm glad it looks suitable.  Please let me know how you get on, because this is an experimental feature.  If you find it works for you, I will add it to the official documentation.  If you find it needs changes to make it work, let me know.

One thing I forgot to mention, when the function is called to do the highlighting - 

 update_highlighting(self, start, end)

it gets passed two arguments. 'start' should be the index of self.value that is the first character that appears on the screen.  'end' is the index of the last character visible on the screen.

You might not need to pay these any attention, but it might help you to make the update_highlighting function more efficient, since you know what is on the screen and what isn't.

The function is called every time the widget is updated on the screen.  You might want to cache the value of self._highlightingdata to avoid slowing down the program, and only update it if things have changed.

Hope that helps.

Best wishes,

Nicholas


Reply all
Reply to author
Forward
0 new messages