Skip to content

Commit 8146750

Browse files
committed
README update
1 parent c69a8f8 commit 8146750

1 file changed

Lines changed: 127 additions & 84 deletions

File tree

README.rst

Lines changed: 127 additions & 84 deletions
Original file line numberDiff line numberDiff line change
@@ -1,161 +1,204 @@
1-
See it at:
1+
| |GitHub badge| |PyPi badge| |python versions badge| |licence badge|
2+
| |test status| |coverage status| |pip downloads badge|
23
3-
- `pypi`_
4-
- `GitHub`_
4+
python-readchar
5+
***************
56

6-
============== =============== ========= ============
7-
VERSION DOWNLOADS TESTS COVERAGE
8-
============== =============== ========= ============
9-
|pip version| |pip downloads| |travis| |coveralls|
10-
============== =============== ========= ============
11-
12-
Library to easily read single chars and key strokes.
13-
14-
Goal and Philosophy
15-
===================
7+
Library to easily read single chars and keystrokes.
168

179
Born as a `python-inquirer`_ requirement.
1810

19-
The idea is to have a portable way to read **single** characters and **key-strokes**.
20-
21-
22-
Documentation
23-
=============
2411

2512
Installation
26-
------------
13+
============
14+
15+
simply install it via :code:`pip`:
2716

28-
::
17+
.. code:: bash
2918
3019
pip install readchar
3120
32-
The :code:`readchar` library works with python 3.6, 3.7, 3.8, 3.9 and 3.10.
21+
Or download the source code from PyPi_.
22+
3323

3424
Usage
35-
-----
25+
=====
3626

37-
Usage example:
27+
Simply read a character or keystroke:
3828

3929
.. code:: python
4030
4131
import readchar
4232
43-
c = readchar.readchar()
4433
key = readchar.readkey()
4534
35+
React to different kinds of keypresses:
36+
37+
.. code:: python
38+
39+
from readchar import readkey, key
40+
41+
while True:
42+
k = readkey()
43+
if k == "a":
44+
# do stuff
45+
if k == key.DOWN:
46+
# do stuff
47+
if k == key.ENTER:
48+
break
49+
50+
4651
API
47-
----
52+
===
4853

4954
There are just two methods:
5055

51-
:code:`readchar()`
52-
//////////////////
56+
:code:`readchar() -> str`
57+
-------------------------
58+
59+
Reads one character from :code:`stdin`, returning it as a string with length 1. Waits until a character is available.
60+
61+
As only ASCII characters are actually a single character, you usually want to use the next function, that also handles longer keys.
62+
63+
64+
:code:`readkey() -> str`
65+
------------------------
66+
67+
Reads the next keystroke from :code:`stdin`, returning it as a string. Waits until a keystroke is available.
68+
69+
A keystroke can be:
5370

54-
Reads the next char from :code:`stdin`, returning it as a string with length 1.
71+
- single characters as returned by :code:`readchar()`. These include:
5572

73+
- character for normal keys: 'a', 'Z', '9',...
74+
- special characters like 'ENTER', 'BACKSPACE', 'TAB',...
75+
- combinations with 'CTRL': 'CTRL'+'A',...
5676

57-
:code:`readkey()`
58-
/////////////////
77+
- keys that are made up of multiple characters:
5978

60-
Reads the next key-stroke from :code:`stdin`, returning it as a string.
79+
- characters for cursors/arrows: 🡩, 🡪, 🡫, 🡨
80+
- navigation keys: 'INSERT', 'HOME',...
81+
- function keys: 'F1' to 'F12'
82+
- combinations with 'ALT': 'ALT'+'A',...
83+
- combinations with 'CTRL' and 'ALT': 'CTRL'+'ALT'+'SUPR',...
6184

62-
A key-stroke can have:
85+
.. attention::
6386

64-
- 1 character for normal keys: 'a', 'z', '9'...
65-
- 2 characters for combinations with ALT: ALT+A, ...
66-
- 3 characters for cursors: ->, <-, ...
67-
- 4 characters for combinations with CTRL and ALT: CTRL+ALT+SUPR, ...
87+
'CTRL'+'C' will not be returned by :code:`readkey()`, but instead raise a :code:`KeyboardInterupt`. If you what to handle it yourself,
88+
use :code:`readchar()`.
6889

69-
There is a list of previously captured chars with their names in :code:`readchar.key`, in order to be used in comparisons and so on. This list is not enough tested and it can have mistakes, so use it carefully. Please, report them if found.
90+
91+
:code:`key` module
92+
---------------------
93+
94+
This submodule contains a list of available keys to compare against. The constants are defined depending on your operating system, so it should be
95+
fully portable. If a key is listed here for your platform, :code:`readkey()` can read it and you can compare against it.
7096

7197

7298
OS Support
73-
----------
99+
==========
100+
101+
This library actively supports these operating systems:
102+
103+
- Linux
104+
- Windows
105+
106+
Some operating systems are enabled, but not actively tested or supported:
74107

75-
This library supports GNU/Linux and Windows. Please, if you can try it in another OS and find a bug, put an issue or send the pull-request.
108+
- macOS
109+
- FreeBSD
110+
111+
Theoretically every Unix based system should work, but they will not be actively tested. It is also required that somebody provides initial test
112+
results before the OS is enabled and added to the list. Feel free to open a PR for that.
76113

77114
Thank you!
78115

116+
79117
How to contribute
80118
=================
81119

82-
You can download the code, make some changes with their tests, and make a pull-request.
120+
You can download the code, make some changes with their tests, and open a pull-request.
83121

84-
In order to develop or running the tests, you can do:
122+
In order to develop and run the tests, follow these steps:
85123

86-
1. Clone the repository.
124+
1. Clone the repository.
87125

88-
.. code:: bash
126+
.. code:: bash
89127
90-
git clone https://github.com/magmax/python-readchar.git
128+
git clone https://github.com/magmax/python-readchar.git
91129
92-
2. Create a virtual environment:
130+
2. Create a virtual environment:
93131

94-
.. code:: bash
132+
.. code:: bash
95133
96-
python -m venv .venv
134+
python -m venv .venv
97135
98-
3. Enter in the virtual environment
136+
3. Enter the virtual environment
99137

100-
.. code:: bash
138+
on Linux systems:
101139

102-
source .venv/bin/activate
140+
.. code:: bash
103141
104-
4. Install dependencies
142+
source .venv/bin/activate
105143
106-
.. code:: bash
144+
or for Windows systems:
107145

108-
pip install -r requirements.txt
146+
.. code:: bash
109147
110-
5. Install the local version of readchar (in edit mode so it automatically reflects changes)
148+
.venv\Scripts\activate
111149
112-
.. code:: bash
150+
4. Install dependencies
113151

114-
pip install -e .
152+
.. code:: bash
115153
116-
6. Run tests
154+
pip install -r requirements.txt
117155
118-
.. code:: bash
156+
5. Install the local version of readchar (in edit mode, so it automatically reflects changes)
119157

120-
make
158+
.. code:: bash
121159
160+
pip install -e .
122161
123-
Please, **Execute the tests before any pull-request**. This will avoid invalid builds.
162+
6. Run tests
124163

164+
.. code:: bash
125165
126-
Licence
127-
=======
166+
make
128167
129-
Copyright (c) 2014-2021 Miguel Angel Garcia (`@magmax_en`_).
168+
(or run the command used inside the Makefile manually, if you don't have/want :code:`make`)
130169

131-
Based on previous work on gist `getch()-like unbuffered character reading from stdin on both Windows and Unix (Python recipe)`_, started by `Danny Yoo`_.
132170

133-
Licensed under `the MIT licence`_.
171+
Please, **Execute the tests before any pull-request**. This will avoid invalid builds.
134172

135173

136-
.. |travis| image:: https://travis-ci.org/magmax/python-readchar.png
137-
:target: `Travis`_
138-
:alt: Travis results
174+
------
139175

140-
.. |coveralls| image:: https://coveralls.io/repos/magmax/python-readchar/badge.png
141-
:target: `Coveralls`_
142-
:alt: Coveralls results_
176+
*Copyright (c) 2014-2022 Miguel Angel Garcia*
143177

144-
.. |pip version| image:: https://img.shields.io/pypi/v/readchar.svg
145-
:target: https://pypi.python.org/pypi/readchar
146-
:alt: Latest PyPI version
147178

148-
.. |pip downloads| image:: https://img.shields.io/pypi/dm/readchar.svg
149-
:target: https://pypi.python.org/pypi/readchar
179+
.. |GitHub badge| image:: https://badges.aleen42.com/src/github.svg
180+
:target: GitHub_
181+
:alt: GitHub Repository
182+
.. |PyPi badge| image:: https://img.shields.io/pypi/v/readchar.svg
183+
:target: PyPi_
184+
:alt: Latest PyPI version
185+
.. |Python versions badge| image:: https://img.shields.io/pypi/pyversions/readchar
186+
:target: PyPi_
187+
:alt: supported Python versions
188+
.. |licence badge| image:: https://img.shields.io/pypi/l/readchar?color=blue
189+
:target: licence_
190+
:alt: Project licence
191+
.. |test status| image:: https://github.com/magmax/python-readchar/actions/workflows/run-tests.yml/badge.svg?branch=master
192+
:target: github.com/magmax/python-readchar/actions/workflows/run-tests.yml?query=branch%3Amaster
193+
:alt: Automated testing results
194+
.. |coverage status| image:: https://coveralls.io/repos/github/magmax/python-readchar/badge.svg?branch=master
195+
:target: https://coveralls.io/github/magmax/python-readchar?branch=master
196+
:alt: Coveralls results
197+
.. |pip downloads badge| image:: https://img.shields.io/pypi/dd/readchar.svg
198+
:target: PyPi_
150199
:alt: Number of PyPI downloads
151200

152-
.. _pypi: https://pypi.python.org/pypi/readchar
153201
.. _GitHub: https://github.com/magmax/python-readchar
202+
.. _PyPi: https://pypi.python.org/pypi/readchar
203+
.. _licence: LICENCE
154204
.. _python-inquirer: https://github.com/magmax/python-inquirer
155-
.. _Travis: https://travis-ci.org/magmax/python-readchar
156-
.. _Coveralls: https://coveralls.io/r/magmax/python-readchar
157-
.. _@magmax_en: https://twitter.com/magmax_en
158-
159-
.. _the MIT licence: LICENCE
160-
.. _getch()-like unbuffered character reading from stdin on both Windows and Unix (Python recipe): http://code.activestate.com/recipes/134892/
161-
.. _Danny Yoo: http://code.activestate.com/recipes/users/98032/

0 commit comments

Comments
 (0)