~tim-ats-d/more-curses

c8bd00bdd108ee9a0cc7dd5668f5bca5298c4f11 — Tim-ats-d 6 months ago 614a069 master
Add examples in readme, fix in types hint.
4 files changed, 83 insertions(+), 6 deletions(-)

M README.md
M more_curses/__init__.py
R more_curses/{interactions.py => input.py}
M more_curses/type.py
M README.md => README.md +73 -0
@@ 13,6 13,79 @@ cd More-curses
pip install .
```

## Requirements

* Python 3.8+

## Examples

### Typing annotations

Several typing annotations are provide by `More-curses`:
```py
from more_curses import CursesTextAttr, CursesKey, CursesWin
```

### Attributes

Combinations of attribute of any iterable:
```py
>>> from curses import A_BOLD, A_REVERSE
>>> from more_curses import combine_attr
>>> combine_attr([A_BOLD, A_REVERSE]) == (A_BOLD | A_REVERSE)
True
```

### Colors

`More-curses` provides a context manager to improve readability.

Without `More-curses`:
```py
from curses import A_BOLD, A_ITALIC

def func(win)
    win.attron(A_BOLD, A_ITALIC)
    win.addstr(0, 0, "foobar")
    win.attroff(A_BOLD, A_ITALIC)
```
With `More-curses`:
```py
from curses import A_BOLD, A_ITALIC

from more_curses import TextAttributes

def func(win):
    with TextAttributes(win, A_BOLD, A_ITALIC):
        win.addstr(0, 0, "foobar")
```

Also exist as a decorator:
```py
from curses import A_ALTCHARSET, A_REVERSE

from more_curses import text_attributes

attr = (A_REVERSE, A_ALTCHARSET)

@text_attributes(*attr)
def greeting(win):
    win.addstr(0, 0, "Hello")
```

### Inputs

Hide the obtaining of keyboard inputs by a classical iteration mechanism:
```py
from more_curses import iterkey

keys = iterkey(win, ["q"], method="getch")

for key in keys:
    win.addstr(0, 0, str(key))
    win.refresh()
```

## Contributing

Feel free to contribute your own snippets.

M more_curses/__init__.py => more_curses/__init__.py +1 -1
@@ 6,5 6,5 @@ __version__ = 0.1
__author__ = "Timéo Arnouts"

from .color import *
from .interactions import *
from .input import *
from .type import *

R more_curses/interactions.py => more_curses/input.py +4 -4
@@ 1,13 1,13 @@
# interactions.py

from typing import Iterable, Sequence
from typing import Container, Iterator, Sequence

from .type import CursesKey
from .type import CursesKey, CursesInputMethod


def iterkey(win,
            exit_keys: Sequence[CursesKey] = (),
            method: str = "getkey") -> Iterable[CursesKey]:
            exit_keys: Container[CursesKey] = (),
            method: CursesInputMethod = "getkey") -> Iterator[CursesKey]:
    """Yield key pressed as long as a key contained in exit_keys is not
    pressed.
    """

M more_curses/type.py => more_curses/type.py +5 -1
@@ 1,6 1,6 @@
# type.py

from typing import NoReturn, Union
from typing import Literal, NoReturn, Union


try:


@@ 13,3 13,7 @@ CursesTextAttr = int
CursesKey = Union[int, str]

ConsoleEffect = NoReturn

CursesInputMethod = Literal["getch",
                            "getkey",
                            "getwch"]