FSArray

FSArray is a two dimensional grid of colored and styled characters.

FSArray - Example

>>> from curtsies import FSArray, fsarray
>>> from curtsies.fmtfuncs import green, blue, on_green
>>> a = fsarray([u'*' * 10 for _ in range(4)], bg='blue', fg='red')
>>> a.dumb_display()
**********
**********
**********
**********
>>> a[1:3, 3:7] = fsarray([green(u'msg:'),
...                        blue(on_green(u'hey!'))])
>>> a.dumb_display()
**********
***msg:***
***hey!***
**********

fsarray is a convenience function returning a FSArray constructed from its arguments.

FSArray - Using

FSArray objects can be composed to build up complex text interfaces:

>>> import time
>>> from curtsies import FSArray, fsarray, fmtstr
>>> def clock():
...     return fsarray([u'::'+fmtstr(u'time')+u'::',
...                     fmtstr(time.strftime('%H:%M:%S').decode('ascii'))])
...
>>> def square(width, height, char):
...     return fsarray(char*width for _ in range(height))
...
>>> a = square(40, 10, u'+')
>>> a[2:8, 2:38] = square(36, 6, u'.')
>>> c = clock()
>>> a[2:4, 30:38] = c
>>> a[6:8, 30:38] = c
>>> message = fmtstr(u'compositing several FSArrays').center(40, u'-')
>>> a[4:5, :] = [message]
>>>
>>> a.dumb_display()
++++++++++++++++++++++++++++++++++++++++
++++++++++++++++++++++++++++++++++++++++
++............................::time::++
++............................21:59:31++
------compositing several FSArrays------
++....................................++
++............................::time::++
++............................21:59:31++
++++++++++++++++++++++++++++++++++++++++
++++++++++++++++++++++++++++++++++++++++

An array like shown above might be repeatedly constructed and rendered with a curtsies.window object.

Slicing works like it does with a FmtStr, but in two dimensions. FSArray are mutable, so array assignment syntax can be used for natural compositing as in the above exaple.

If you’re dealing with terminal output, the width of a string becomes more important than it’s length (see FmtStr - len vs width).

In the future FSArray will do slicing and array assignment based on width instead of number of characters, but this is not currently implemented.

FSArray - API docs

curtsies.fsarray(list_of_FmtStrs_or_strings, width=None) → FSArray

Returns a new FSArray of width of the maximum size of the provided strings, or width provided, and height of the number of strings provided. If a width is provided, raises a ValueError if any of the strings are of length greater than this width

class curtsies.FSArray(num_rows, num_columns, *args, **kwargs)

A 2D array of colored text.

Internally represented by a list of FmtStrs of identical size.

classmethod diff(a, b, ignore_formatting=False)

Returns two FSArrays with differences underlined

dumb_display()

Prints each row followed by a newline without regard for the terminal window size

height

The number of rows

shape

tuple of (len(rows, len(num_columns)) numpy-style shape

width

The number of columns