Alain Leufroy | 16 Aug 2011 13:11
Picon
Favicon

palette container

Hi,

I'd like to use pygments to colorize source file data displayed in a Text
widget.

To do so, I set a palette with names from pygments tokens. Here is a simple
example::

--- 8< ---- Begin (test.py) ---- 8< ----
#! /usr/bin/env python

import urwid
from pygments import lex, lexers, token

class Foo(object):
     '''Just to get a class'''

     def __str__(self):
         '''return this python source code as a string'''
         fid = open(__file__)
         python_code = fid.read()
         fid.close()
         return python_code

def highlight_code(text):
     """return formated text using pygments"""
     return list(lex(text, lexers.PythonLexer()))

PALETTE = [(token.Token.Text, 'default', 'default'),
            (token.Token.Name, 'dark blue', 'default'),
(Continue reading)

Ian Ward | 17 Aug 2011 04:38
Favicon
Gravatar

Re: palette container

Alain Leufroy wrote on 2011-08-16 07:11:
> Hi,
> 
> I'd like to use pygments to colorize source file data displayed in a Text
> widget.
> 
> To do so, I set a palette with names from pygments tokens. Here is a simple
> example::
> 
...
> 
> Running ``python test.py`` displays a highlighted source code. But it is not
> exactly what as I expect.
> 
> For example, *Foo* is not highlighted because pygments sets the "style" to
> *token.Token.Name.Class*. But I'd like that *Foo* is in "dark blue" because
> *token.Token.Name.Class* is a "sub-style" of *token.Token.Name* (instead of
> defining all "sub-style of "token.Token.Name" to the same style).
> 
> From now urwid stores the palette in a dictionary (ex: raw_display.py line 51
> changeset d6d6ae081dcf of https://excess.org/hg/urwid/).
> Is it possible to add a way to tune the palette container for a dict-like class
> that suite special needs?

Hi Alain,

I suppose you could replace the _palette dictionary in your screen with
your own dictionary-like object that does that sort of advanced lookup.

Ian
(Continue reading)

Yu-Jie Lin | 17 Aug 2011 06:59
Picon
Gravatar

Re: palette container

On Wed, Aug 17, 2011 at 10:38, Ian Ward <ian <at> excess.org> wrote:
> Alain Leufroy wrote on 2011-08-16 07:11:
>> Running ``python test.py`` displays a highlighted source code. But it is not
>> exactly what as I expect.
>>
>> For example, *Foo* is not highlighted because pygments sets the "style" to
>> *token.Token.Name.Class*. But I'd like that *Foo* is in "dark blue" because
>> *token.Token.Name.Class* is a "sub-style" of *token.Token.Name* (instead of
>> defining all "sub-style of "token.Token.Name" to the same style).
>>
>> From now urwid stores the palette in a dictionary (ex: raw_display.py line 51
>> changeset d6d6ae081dcf of https://excess.org/hg/urwid/).
>> Is it possible to add a way to tune the palette container for a dict-like class
>> that suite special needs?
>
> Hi Alain,
>
> I suppose you could replace the _palette dictionary in your screen with
> your own dictionary-like object that does that sort of advanced lookup.
>
> Ian
>

This should get you started:

---

[...]

class CPDict(dict):
(Continue reading)

Alain Leufroy | 17 Aug 2011 10:28
Picon
Favicon

Re: palette container (SOLVED)

>>> Running ``python test.py`` displays a highlighted source code. But it is not
>>> exactly what as I expect.
>>>
>>> For example, *Foo* is not highlighted because pygments sets the "style" to
>>> *token.Token.Name.Class*. But I'd like that *Foo* is in "dark blue" because
>>> *token.Token.Name.Class* is a "sub-style" of *token.Token.Name* (instead of
>>> defining all "sub-style of "token.Token.Name" to the same style).
>>>
>>> From now urwid stores the palette in a dictionary (ex: raw_display.py line 51
>>> changeset d6d6ae081dcf of https://excess.org/hg/urwid/).
>>> Is it possible to add a way to tune the palette container for a dict-like class
>>> that suite special needs?
>>
>> Hi Alain,
>>
>> I suppose you could replace the _palette dictionary in your screen with
>> your own dictionary-like object that does that sort of advanced lookup.
>>
>> Ian
>>
>
> This should get you started:
>
> ---
>
> [...]
>
> class CPDict(dict):
>    """Cascading (Escape) Palette for Pygments token
>
(Continue reading)

Ian Ward | 17 Aug 2011 15:27
Favicon
Gravatar

Re: palette container

Yu-Jie Lin wrote on 2011-08-17 00:59:
> class CPDict(dict):
>     """Cascading (Escape) Palette for Pygments token
> 
>     Only works if ancestor is already defined.
>     """
>     def __contains__(self, k):
...

I've heard that it's a bad idea to subclass dict (or any of the standard
python containers) unless you override pretty much every special method.
 Their implementations assume that the methods haven't been changed in a
subclass, and you might get surprising behaviour.

Ian
Patrick Totzke | 17 Aug 2011 17:14

Fwd: unicode symbols and whitespaces


Hi!

I'm currently seing strange behaviour with Text widgets in combination with
utf-8 and unicode strings:
For certain symbols, like '颖', urwid craches when displaying them (stacktrace below).
But if i encode this to utf08 before handing it to urwid.Text,
Its length gets calculated falsely:
>s=u'\u9896'.encode('utf-8')
> len(s) = 3
so I get two whitespaces after the symbol on my screen..
Best,
/p

   self.mainloop.run()
  File
"/usr/local/lib/python2.7/dist-packages/urwid-1.0.0-py2.7-linux-x86_64.egg/urwid/main_loop.py",
line 271, in run
    self.screen.run_wrapper(self._run)
  File
"/usr/local/lib/python2.7/dist-packages/urwid-1.0.0-py2.7-linux-x86_64.egg/urwid/raw_display.py",
line 228, in run_wrapper
    return fn()
  File
"/usr/local/lib/python2.7/dist-packages/urwid-1.0.0-py2.7-linux-x86_64.egg/urwid/main_loop.py",
line 292, in _run
    self.event_loop.run()
  File
(Continue reading)

Patrick Totzke | 17 Aug 2011 17:15

Fwd: custom padding?


Hi!

I wonder how I can use a Decoration widget to pad a line with something different from whitespaces:
I have a Column of text widgets and a very long string.
If I render my Column line I get something like this

^|first text|second text|third text|                        $

Here ^ is the begining of the line on the screen, $ marks the end.
All texts but the last one are give as fixed length:

first = firststring
second = secondstring
third = thirdstring
Columms([('fixed', len(first), Text(first)),
         ('fixed', len(second), Text(second)),
         Text(third, wrap='clip')])

The reason I don't fix the length of the last string is because its length varies
and if its long, longer as the space on the screen, it doesn't get displayed at all.

Lets say my long string is pi = '3.141592653589793238462643383279502884197169399375105820974944592307'.
I want to render the above line as 

^|first text|second text|third text|3.1415926535897932384626$

Now, I add the long string as a Column.
(Continue reading)

Ian Ward | 19 Aug 2011 20:39
Favicon
Gravatar

Re: Fwd: custom padding?

Patrick Totzke wrote on 2011-08-17 11:15:
> 
> 
> 
> Hi!
> 
> I wonder how I can use a Decoration widget to pad a line with something different from whitespaces:
> I have a Column of text widgets and a very long string.
> If I render my Column line I get something like this
> 
> ^|first text|second text|third text|                        $
> 
> Here ^ is the begining of the line on the screen, $ marks the end.
> All texts but the last one are give as fixed length:
> 
> first = firststring
> second = secondstring
> third = thirdstring
> Columms([('fixed', len(first), Text(first)),
>          ('fixed', len(second), Text(second)),
>          Text(third, wrap='clip')])
> 
> The reason I don't fix the length of the last string is because its length varies
> and if its long, longer as the space on the screen, it doesn't get displayed at all.
> 
> Lets say my long string is pi = '3.141592653589793238462643383279502884197169399375105820974944592307'.
> I want to render the above line as 
> 
> ^|first text|second text|third text|3.1415926535897932384626$
> 
(Continue reading)

Patrick Totzke | 19 Aug 2011 22:57

Re: Fwd: unicode symbols and whitespaces

Hi again.
attached is a minimal example to reproduce this:
It seems to happen only in combination with Culumn widgets.
There are two notable things with this example:
First, the length of the encoded string `problematic` is of course 3,
so the column will get displayed with fixed length 3, which is not what I want.
Secondly, if you comment out the .encode('utf-8') in line 4, the length of the string
becomes 1, and then urwid diverges before displaying anything.
/p

On Aug 17, Patrick Totzke wrote:
>`
>Hi!
>
>I'm currently seing strange behaviour with Text widgets in combination with
>utf-8 and unicode strings:
>For certain symbols, like '颖', urwid craches when displaying them (stacktrace below).
>But if i encode this to utf08 before handing it to urwid.Text,
>Its length gets calculated falsely:
>>s=u'\u9896'.encode('utf-8')
>> len(s) = 3
>so I get two whitespaces after the symbol on my screen..
>Best,
>/p
>
>
>
>
>
>   self.mainloop.run()
(Continue reading)

Patrick Totzke | 20 Aug 2011 14:42

Re: Fwd: custom padding?


On Yest.19pm, Ian Ward wrote:
>For endless repeating characters you can use Divider or a SolidFill
>widget (if you need a box widget).

This only works with single chars right?

>I've thought about making the fill customizable for the Padding and
>Filler widgets (kind of like the background in an Overlay).  That might
>simplify some common cases.

Suppose I wanted to implement something like this for my special case.
Would I subclass Padding and overwrite padding_values ?

I think another possible solution for my case would be if once could
tell Columns that if an element has fixed length and doesn't fit onto the screen
then instead of leaving it out display only a prefix. At least for Text widgets
that would make sense..
best,
/p
_______________________________________________
Urwid mailing list
Urwid <at> lists.excess.org
http://lists.excess.org/mailman/listinfo/urwid

Gmane