fa-console 2.0.0__tar.gz
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- fa_console-2.0.0/LICENSE +21 -0
- fa_console-2.0.0/PKG-INFO +231 -0
- fa_console-2.0.0/README.md +194 -0
- fa_console-2.0.0/fa_console.egg-info/PKG-INFO +231 -0
- fa_console-2.0.0/fa_console.egg-info/SOURCES.txt +9 -0
- fa_console-2.0.0/fa_console.egg-info/dependency_links.txt +1 -0
- fa_console-2.0.0/fa_console.egg-info/requires.txt +4 -0
- fa_console-2.0.0/fa_console.egg-info/top_level.txt +1 -0
- fa_console-2.0.0/fa_console.py +1117 -0
- fa_console-2.0.0/pyproject.toml +58 -0
- fa_console-2.0.0/setup.cfg +4 -0
fa_console-2.0.0/LICENSE
ADDED
|
@@ -0,0 +1,21 @@
|
|
|
1
|
+
MIT License
|
|
2
|
+
|
|
3
|
+
Copyright (c) 2026 Alireza Hosseini
|
|
4
|
+
|
|
5
|
+
Permission is hereby granted, free of charge, to any person obtaining a copy
|
|
6
|
+
of this software and associated documentation files (the "Software"), to deal
|
|
7
|
+
in the Software without restriction, including without limitation the rights
|
|
8
|
+
to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
|
|
9
|
+
copies of the Software, and to permit persons to whom the Software is
|
|
10
|
+
furnished to do so, subject to the following conditions:
|
|
11
|
+
|
|
12
|
+
The above copyright notice and this permission notice shall be included in all
|
|
13
|
+
copies or substantial portions of the Software.
|
|
14
|
+
|
|
15
|
+
THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
|
|
16
|
+
IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
|
|
17
|
+
FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
|
|
18
|
+
AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
|
|
19
|
+
LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
|
|
20
|
+
OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
|
|
21
|
+
SOFTWARE.
|
|
@@ -0,0 +1,231 @@
|
|
|
1
|
+
Metadata-Version: 2.4
|
|
2
|
+
Name: fa-console
|
|
3
|
+
Version: 2.0.0
|
|
4
|
+
Summary: Correct Persian (Farsi) console input/output for Python on Windows: automatic UTF-8 setup plus visual letter shaping and bidi for the classic console.
|
|
5
|
+
Author-email: Alireza Hosseini <alireza.hosseini@hotmail.com>
|
|
6
|
+
License: MIT
|
|
7
|
+
Project-URL: Homepage, https://github.com/Padandish/fa-console
|
|
8
|
+
Project-URL: Repository, https://github.com/Padandish/fa-console
|
|
9
|
+
Project-URL: Issues, https://github.com/Padandish/fa-console/issues
|
|
10
|
+
Project-URL: Tech Channel (Bale), https://ble.ir/TechInsightsHub
|
|
11
|
+
Project-URL: Tech Channel (Eitaa), https://eitaa.com/TechInsightsHub
|
|
12
|
+
Project-URL: Video Tutorials (Aparat), https://aparat.com/TechInsightsHub
|
|
13
|
+
Keywords: persian,farsi,console,terminal,windows,unicode,utf-8,bidi,rtl,arabic,reshaper,cmd,codepage
|
|
14
|
+
Classifier: Development Status :: 5 - Production/Stable
|
|
15
|
+
Classifier: Environment :: Console
|
|
16
|
+
Classifier: Intended Audience :: Developers
|
|
17
|
+
Classifier: License :: OSI Approved :: MIT License
|
|
18
|
+
Classifier: Operating System :: Microsoft :: Windows
|
|
19
|
+
Classifier: Programming Language :: Python :: 3
|
|
20
|
+
Classifier: Programming Language :: Python :: 3.8
|
|
21
|
+
Classifier: Programming Language :: Python :: 3.9
|
|
22
|
+
Classifier: Programming Language :: Python :: 3.10
|
|
23
|
+
Classifier: Programming Language :: Python :: 3.11
|
|
24
|
+
Classifier: Programming Language :: Python :: 3.12
|
|
25
|
+
Classifier: Programming Language :: Python :: 3.13
|
|
26
|
+
Classifier: Programming Language :: Python :: 3.14
|
|
27
|
+
Classifier: Topic :: Software Development :: Internationalization
|
|
28
|
+
Classifier: Topic :: Software Development :: Libraries :: Python Modules
|
|
29
|
+
Classifier: Topic :: Terminals
|
|
30
|
+
Requires-Python: >=3.8
|
|
31
|
+
Description-Content-Type: text/markdown
|
|
32
|
+
License-File: LICENSE
|
|
33
|
+
Provides-Extra: high-fidelity
|
|
34
|
+
Requires-Dist: arabic-reshaper>=3.0; extra == "high-fidelity"
|
|
35
|
+
Requires-Dist: python-bidi>=0.4.2; extra == "high-fidelity"
|
|
36
|
+
Dynamic: license-file
|
|
37
|
+
|
|
38
|
+
# fa-console
|
|
39
|
+
|
|
40
|
+
> **One import. Correct Persian (Farsi) in the Windows console.**
|
|
41
|
+
|
|
42
|
+
**English** | [فارسی](README.fa.md)
|
|
43
|
+
|
|
44
|
+
[](https://pypi.org/project/fa-console/)
|
|
45
|
+
[](https://pypi.org/project/fa-console/)
|
|
46
|
+
[](LICENSE)
|
|
47
|
+
[](#compatibility)
|
|
48
|
+
|
|
49
|
+
`fa_console` makes Python's built-in `print()` and `input()` work correctly
|
|
50
|
+
with Persian text on Windows — **automatically**, with zero required
|
|
51
|
+
dependencies and no changes to your code beyond a single `import`.
|
|
52
|
+
|
|
53
|
+
It fixes both independent layers of the problem:
|
|
54
|
+
|
|
55
|
+
| Layer | Problem without `fa_console` | With `fa_console` |
|
|
56
|
+
|-------|------------------------------|-------------------|
|
|
57
|
+
| **Encoding** | Legacy console code page (720 / 1252 / …) mangles UTF-8 on redirect or piping; crashes with `UnicodeEncodeError` | Console code page switched to UTF-8 (65001); standard streams reconfigured to UTF-8; child processes pinned via `PYTHONUTF8` |
|
|
58
|
+
| **Rendering** | Classic console host (`conhost`) has no Arabic shaping and no bidirectional reordering — correct text is *drawn* disconnected and backwards | Every write is converted to visual order (letter shaping + bidi) so text appears joined and right-to-left. While typing, `fa_input` echoes correctly in real time |
|
|
59
|
+
|
|
60
|
+
**Before**
|
|
61
|
+
|
|
62
|
+
```
|
|
63
|
+
>>> print("سلام دنیا")
|
|
64
|
+
ﺎﯿﻧﺩ ﻡﻼﺳ # disconnected, reversed, unreadable
|
|
65
|
+
```
|
|
66
|
+
|
|
67
|
+
**After**
|
|
68
|
+
|
|
69
|
+
```
|
|
70
|
+
>>> import fa_console
|
|
71
|
+
>>> print("سلام دنیا")
|
|
72
|
+
سلام دنیا # joined, right-to-left, readable
|
|
73
|
+
```
|
|
74
|
+
|
|
75
|
+
## Features
|
|
76
|
+
|
|
77
|
+
- ✅ **One-line setup** — `import fa_console` configures everything at import time
|
|
78
|
+
- ✅ **Encoding layer** — UTF-8 code page, UTF-8 `sys.stdin`/`sys.stdout`/`sys.stderr`
|
|
79
|
+
(neutralises even a broken `PYTHONIOENCODING`), and pinned environment variables
|
|
80
|
+
for spawned subprocesses
|
|
81
|
+
- ✅ **Rendering layer** — logical→visual transformation (letter shaping into
|
|
82
|
+
Unicode Arabic Presentation Forms + bidirectional reordering) applied to *all*
|
|
83
|
+
output, including plain `print()` and f-strings
|
|
84
|
+
- ✅ **Two transformation backends** — uses `arabic-reshaper` + `python-bidi`
|
|
85
|
+
when installed (full UAX #9, lam-alef ligatures, Persian rules); otherwise a
|
|
86
|
+
built-in, dependency-free engine
|
|
87
|
+
- ✅ **Live-typing echo** — `fa_input()` renders letters joined and right-to-left
|
|
88
|
+
*while you type*, with built-in-`input()`-compatible semantics
|
|
89
|
+
(`KeyboardInterrupt` / `EOFError`)
|
|
90
|
+
- ✅ **Smart detection** — Windows Terminal, VS Code-style terminals, IDEs and
|
|
91
|
+
redirected files/pipes are detected and left untouched (standard text flows
|
|
92
|
+
there, as it should)
|
|
93
|
+
- ✅ **Fail-open design** — the module never crashes the host application; every
|
|
94
|
+
internal failure is logged through the `fa_console` logger and degrades
|
|
95
|
+
gracefully to standard Python behaviour
|
|
96
|
+
- ✅ **Zero required dependencies** — pure standard library, Python 3.8+
|
|
97
|
+
|
|
98
|
+
## Installation
|
|
99
|
+
|
|
100
|
+
```bash
|
|
101
|
+
pip install fa-console
|
|
102
|
+
```
|
|
103
|
+
|
|
104
|
+
Optional, higher-fidelity rendering backend:
|
|
105
|
+
|
|
106
|
+
```bash
|
|
107
|
+
pip install "fa-console[high-fidelity]" # arabic-reshaper + python-bidi
|
|
108
|
+
```
|
|
109
|
+
|
|
110
|
+
Or simply copy `fa_console.py` into your project — it is intentionally a
|
|
111
|
+
single, self-contained module.
|
|
112
|
+
|
|
113
|
+
## Quick start
|
|
114
|
+
|
|
115
|
+
```python
|
|
116
|
+
import fa_console # everything is configured here
|
|
117
|
+
|
|
118
|
+
print("سلام دنیا!") # renders correctly everywhere
|
|
119
|
+
name = fa_console.fa_input("نام شما: ") # live, correct echo while typing
|
|
120
|
+
print(f"سلام، {name} جان!") # logical data, correct display
|
|
121
|
+
```
|
|
122
|
+
|
|
123
|
+
The value returned by `input()` / `fa_input()` is always **logical-order**
|
|
124
|
+
Unicode: comparisons, `len()`, slicing, regexes and file writes all see plain,
|
|
125
|
+
correct Persian text. Visual transformation is a display-time-only concern.
|
|
126
|
+
|
|
127
|
+
## API overview
|
|
128
|
+
|
|
129
|
+
| Member | Purpose |
|
|
130
|
+
|--------|---------|
|
|
131
|
+
| `setup_console(force=False)` | Apply the full configuration; idempotent. Returns a `ConsoleSetupReport` |
|
|
132
|
+
| `fa_print(*args, **kwargs)` | Persian-safe drop-in for `print()` |
|
|
133
|
+
| `fa_input(prompt="")` | Persian-safe `input()` with live correct echo |
|
|
134
|
+
| `is_visual_mode()` | `True` when the display transform is active |
|
|
135
|
+
| `get_console_info()` | Dict snapshot of the detected environment (great for bug reports) |
|
|
136
|
+
| `VisualStream` | The transparent stream wrapper (advanced use) |
|
|
137
|
+
| `FaConsoleError` | Deliberate low-level failures (consumed internally — fail-open) |
|
|
138
|
+
|
|
139
|
+
### Self-diagnostics
|
|
140
|
+
|
|
141
|
+
```bash
|
|
142
|
+
python fa_console.py
|
|
143
|
+
```
|
|
144
|
+
|
|
145
|
+
Prints a full environment report: encodings, console code page, detected
|
|
146
|
+
terminal type, active bidi backend and a rendered sample.
|
|
147
|
+
|
|
148
|
+
## How it works
|
|
149
|
+
|
|
150
|
+
1. **Encoding layer** — `SetConsoleCP`/`SetConsoleOutputCP` (Win32) switch the
|
|
151
|
+
console to UTF-8; `sys.stdin`/`sys.stdout`/`sys.stderr` are reconfigured to
|
|
152
|
+
UTF-8; `PYTHONUTF8=1` / `PYTHONIOENCODING=utf-8` are pinned for child
|
|
153
|
+
processes.
|
|
154
|
+
2. **Rendering layer** — on classic `conhost` only, a `VisualStream` wrapper is
|
|
155
|
+
installed on stdout/stderr. Each write is converted: Arabic/Persian letters
|
|
156
|
+
are replaced by their contextual presentation forms (isolated / initial /
|
|
157
|
+
medial / final, including پ چ ژ ک گ ی), then the line is reordered for an
|
|
158
|
+
LTR-drawing terminal while Latin words and numbers stay upright and brackets
|
|
159
|
+
are mirrored.
|
|
160
|
+
3. **Detection** — the transform engages only when stdout is an interactive
|
|
161
|
+
classic console. Windows Terminal (`WT_SESSION`), VS Code terminals
|
|
162
|
+
(`TERM_PROGRAM`) and redirected output are bypassed, because they render
|
|
163
|
+
Arabic correctly or must receive standard logical text.
|
|
164
|
+
|
|
165
|
+
## Configuration
|
|
166
|
+
|
|
167
|
+
| Environment variable | Effect |
|
|
168
|
+
|----------------------|--------|
|
|
169
|
+
| `FA_CONSOLE_FORCE_VISUAL=1` | Force the visual transform on (debugging) |
|
|
170
|
+
| `FA_CONSOLE_NO_VISUAL=1` | Unconditionally disable the visual transform |
|
|
171
|
+
|
|
172
|
+
## Compatibility
|
|
173
|
+
|
|
174
|
+
| Environment | Behaviour |
|
|
175
|
+
|-------------|-----------|
|
|
176
|
+
| `cmd.exe` / standalone PowerShell (conhost) | Encoding fixed + visual transform + live typing echo |
|
|
177
|
+
| Windows Terminal | Encoding fixed; native rendering (module steps aside) |
|
|
178
|
+
| VS Code / JetBrains / IDLE consoles | Encoding fixed; module steps aside |
|
|
179
|
+
| Output redirected to file / pipe | Standard logical UTF-8 — safe for other tools |
|
|
180
|
+
| Linux / macOS | Safe no-op (standard behaviour) |
|
|
181
|
+
|
|
182
|
+
## Limitations & FAQ
|
|
183
|
+
|
|
184
|
+
- **Letters look disconnected while I type in `cmd`.** `fa_input()` redraws the
|
|
185
|
+
line correctly as you type; the console's own echo of system prompts is
|
|
186
|
+
outside any program's control.
|
|
187
|
+
- **Perfect native rendering?** Use [Windows Terminal](https://aka.ms/terminal)
|
|
188
|
+
— the module detects it and lets it do the shaping.
|
|
189
|
+
- **Does the transform corrupt my data?** No — it is display-only. Values,
|
|
190
|
+
comparisons and files always hold logical text.
|
|
191
|
+
- **Other RTL languages?** The shaping tables cover Arabic and Persian letters;
|
|
192
|
+
the engine is language-agnostic for the letters it knows.
|
|
193
|
+
|
|
194
|
+
## Development
|
|
195
|
+
|
|
196
|
+
```bash
|
|
197
|
+
git clone https://github.com/Padandish/fa-console.git
|
|
198
|
+
cd fa-console
|
|
199
|
+
python -m pip install build
|
|
200
|
+
python -m build # creates dist/
|
|
201
|
+
```
|
|
202
|
+
|
|
203
|
+
Run the interactive example in any console:
|
|
204
|
+
|
|
205
|
+
```bash
|
|
206
|
+
python example.py
|
|
207
|
+
```
|
|
208
|
+
|
|
209
|
+
Run the full environment diagnostics:
|
|
210
|
+
|
|
211
|
+
```bash
|
|
212
|
+
python fa_console.py
|
|
213
|
+
```
|
|
214
|
+
|
|
215
|
+
## Author
|
|
216
|
+
|
|
217
|
+
**Alireza Hosseini** — [alireza.hosseini@hotmail.com](mailto:alireza.hosseini@hotmail.com)
|
|
218
|
+
|
|
219
|
+
### Tech channels
|
|
220
|
+
|
|
221
|
+
Follow **TechInsightsHub** for updates, tutorials and related projects:
|
|
222
|
+
|
|
223
|
+
| Platform | Link |
|
|
224
|
+
|----------|------|
|
|
225
|
+
| Bale | [ble.ir/TechInsightsHub](https://ble.ir/TechInsightsHub) |
|
|
226
|
+
| Eitaa | [eitaa.com/TechInsightsHub](https://eitaa.com/TechInsightsHub) |
|
|
227
|
+
| Aparat | [aparat.com/TechInsightsHub](https://aparat.com/TechInsightsHub) |
|
|
228
|
+
|
|
229
|
+
## License
|
|
230
|
+
|
|
231
|
+
[MIT](LICENSE) © Alireza Hosseini
|
|
@@ -0,0 +1,194 @@
|
|
|
1
|
+
# fa-console
|
|
2
|
+
|
|
3
|
+
> **One import. Correct Persian (Farsi) in the Windows console.**
|
|
4
|
+
|
|
5
|
+
**English** | [فارسی](README.fa.md)
|
|
6
|
+
|
|
7
|
+
[](https://pypi.org/project/fa-console/)
|
|
8
|
+
[](https://pypi.org/project/fa-console/)
|
|
9
|
+
[](LICENSE)
|
|
10
|
+
[](#compatibility)
|
|
11
|
+
|
|
12
|
+
`fa_console` makes Python's built-in `print()` and `input()` work correctly
|
|
13
|
+
with Persian text on Windows — **automatically**, with zero required
|
|
14
|
+
dependencies and no changes to your code beyond a single `import`.
|
|
15
|
+
|
|
16
|
+
It fixes both independent layers of the problem:
|
|
17
|
+
|
|
18
|
+
| Layer | Problem without `fa_console` | With `fa_console` |
|
|
19
|
+
|-------|------------------------------|-------------------|
|
|
20
|
+
| **Encoding** | Legacy console code page (720 / 1252 / …) mangles UTF-8 on redirect or piping; crashes with `UnicodeEncodeError` | Console code page switched to UTF-8 (65001); standard streams reconfigured to UTF-8; child processes pinned via `PYTHONUTF8` |
|
|
21
|
+
| **Rendering** | Classic console host (`conhost`) has no Arabic shaping and no bidirectional reordering — correct text is *drawn* disconnected and backwards | Every write is converted to visual order (letter shaping + bidi) so text appears joined and right-to-left. While typing, `fa_input` echoes correctly in real time |
|
|
22
|
+
|
|
23
|
+
**Before**
|
|
24
|
+
|
|
25
|
+
```
|
|
26
|
+
>>> print("سلام دنیا")
|
|
27
|
+
ﺎﯿﻧﺩ ﻡﻼﺳ # disconnected, reversed, unreadable
|
|
28
|
+
```
|
|
29
|
+
|
|
30
|
+
**After**
|
|
31
|
+
|
|
32
|
+
```
|
|
33
|
+
>>> import fa_console
|
|
34
|
+
>>> print("سلام دنیا")
|
|
35
|
+
سلام دنیا # joined, right-to-left, readable
|
|
36
|
+
```
|
|
37
|
+
|
|
38
|
+
## Features
|
|
39
|
+
|
|
40
|
+
- ✅ **One-line setup** — `import fa_console` configures everything at import time
|
|
41
|
+
- ✅ **Encoding layer** — UTF-8 code page, UTF-8 `sys.stdin`/`sys.stdout`/`sys.stderr`
|
|
42
|
+
(neutralises even a broken `PYTHONIOENCODING`), and pinned environment variables
|
|
43
|
+
for spawned subprocesses
|
|
44
|
+
- ✅ **Rendering layer** — logical→visual transformation (letter shaping into
|
|
45
|
+
Unicode Arabic Presentation Forms + bidirectional reordering) applied to *all*
|
|
46
|
+
output, including plain `print()` and f-strings
|
|
47
|
+
- ✅ **Two transformation backends** — uses `arabic-reshaper` + `python-bidi`
|
|
48
|
+
when installed (full UAX #9, lam-alef ligatures, Persian rules); otherwise a
|
|
49
|
+
built-in, dependency-free engine
|
|
50
|
+
- ✅ **Live-typing echo** — `fa_input()` renders letters joined and right-to-left
|
|
51
|
+
*while you type*, with built-in-`input()`-compatible semantics
|
|
52
|
+
(`KeyboardInterrupt` / `EOFError`)
|
|
53
|
+
- ✅ **Smart detection** — Windows Terminal, VS Code-style terminals, IDEs and
|
|
54
|
+
redirected files/pipes are detected and left untouched (standard text flows
|
|
55
|
+
there, as it should)
|
|
56
|
+
- ✅ **Fail-open design** — the module never crashes the host application; every
|
|
57
|
+
internal failure is logged through the `fa_console` logger and degrades
|
|
58
|
+
gracefully to standard Python behaviour
|
|
59
|
+
- ✅ **Zero required dependencies** — pure standard library, Python 3.8+
|
|
60
|
+
|
|
61
|
+
## Installation
|
|
62
|
+
|
|
63
|
+
```bash
|
|
64
|
+
pip install fa-console
|
|
65
|
+
```
|
|
66
|
+
|
|
67
|
+
Optional, higher-fidelity rendering backend:
|
|
68
|
+
|
|
69
|
+
```bash
|
|
70
|
+
pip install "fa-console[high-fidelity]" # arabic-reshaper + python-bidi
|
|
71
|
+
```
|
|
72
|
+
|
|
73
|
+
Or simply copy `fa_console.py` into your project — it is intentionally a
|
|
74
|
+
single, self-contained module.
|
|
75
|
+
|
|
76
|
+
## Quick start
|
|
77
|
+
|
|
78
|
+
```python
|
|
79
|
+
import fa_console # everything is configured here
|
|
80
|
+
|
|
81
|
+
print("سلام دنیا!") # renders correctly everywhere
|
|
82
|
+
name = fa_console.fa_input("نام شما: ") # live, correct echo while typing
|
|
83
|
+
print(f"سلام، {name} جان!") # logical data, correct display
|
|
84
|
+
```
|
|
85
|
+
|
|
86
|
+
The value returned by `input()` / `fa_input()` is always **logical-order**
|
|
87
|
+
Unicode: comparisons, `len()`, slicing, regexes and file writes all see plain,
|
|
88
|
+
correct Persian text. Visual transformation is a display-time-only concern.
|
|
89
|
+
|
|
90
|
+
## API overview
|
|
91
|
+
|
|
92
|
+
| Member | Purpose |
|
|
93
|
+
|--------|---------|
|
|
94
|
+
| `setup_console(force=False)` | Apply the full configuration; idempotent. Returns a `ConsoleSetupReport` |
|
|
95
|
+
| `fa_print(*args, **kwargs)` | Persian-safe drop-in for `print()` |
|
|
96
|
+
| `fa_input(prompt="")` | Persian-safe `input()` with live correct echo |
|
|
97
|
+
| `is_visual_mode()` | `True` when the display transform is active |
|
|
98
|
+
| `get_console_info()` | Dict snapshot of the detected environment (great for bug reports) |
|
|
99
|
+
| `VisualStream` | The transparent stream wrapper (advanced use) |
|
|
100
|
+
| `FaConsoleError` | Deliberate low-level failures (consumed internally — fail-open) |
|
|
101
|
+
|
|
102
|
+
### Self-diagnostics
|
|
103
|
+
|
|
104
|
+
```bash
|
|
105
|
+
python fa_console.py
|
|
106
|
+
```
|
|
107
|
+
|
|
108
|
+
Prints a full environment report: encodings, console code page, detected
|
|
109
|
+
terminal type, active bidi backend and a rendered sample.
|
|
110
|
+
|
|
111
|
+
## How it works
|
|
112
|
+
|
|
113
|
+
1. **Encoding layer** — `SetConsoleCP`/`SetConsoleOutputCP` (Win32) switch the
|
|
114
|
+
console to UTF-8; `sys.stdin`/`sys.stdout`/`sys.stderr` are reconfigured to
|
|
115
|
+
UTF-8; `PYTHONUTF8=1` / `PYTHONIOENCODING=utf-8` are pinned for child
|
|
116
|
+
processes.
|
|
117
|
+
2. **Rendering layer** — on classic `conhost` only, a `VisualStream` wrapper is
|
|
118
|
+
installed on stdout/stderr. Each write is converted: Arabic/Persian letters
|
|
119
|
+
are replaced by their contextual presentation forms (isolated / initial /
|
|
120
|
+
medial / final, including پ چ ژ ک گ ی), then the line is reordered for an
|
|
121
|
+
LTR-drawing terminal while Latin words and numbers stay upright and brackets
|
|
122
|
+
are mirrored.
|
|
123
|
+
3. **Detection** — the transform engages only when stdout is an interactive
|
|
124
|
+
classic console. Windows Terminal (`WT_SESSION`), VS Code terminals
|
|
125
|
+
(`TERM_PROGRAM`) and redirected output are bypassed, because they render
|
|
126
|
+
Arabic correctly or must receive standard logical text.
|
|
127
|
+
|
|
128
|
+
## Configuration
|
|
129
|
+
|
|
130
|
+
| Environment variable | Effect |
|
|
131
|
+
|----------------------|--------|
|
|
132
|
+
| `FA_CONSOLE_FORCE_VISUAL=1` | Force the visual transform on (debugging) |
|
|
133
|
+
| `FA_CONSOLE_NO_VISUAL=1` | Unconditionally disable the visual transform |
|
|
134
|
+
|
|
135
|
+
## Compatibility
|
|
136
|
+
|
|
137
|
+
| Environment | Behaviour |
|
|
138
|
+
|-------------|-----------|
|
|
139
|
+
| `cmd.exe` / standalone PowerShell (conhost) | Encoding fixed + visual transform + live typing echo |
|
|
140
|
+
| Windows Terminal | Encoding fixed; native rendering (module steps aside) |
|
|
141
|
+
| VS Code / JetBrains / IDLE consoles | Encoding fixed; module steps aside |
|
|
142
|
+
| Output redirected to file / pipe | Standard logical UTF-8 — safe for other tools |
|
|
143
|
+
| Linux / macOS | Safe no-op (standard behaviour) |
|
|
144
|
+
|
|
145
|
+
## Limitations & FAQ
|
|
146
|
+
|
|
147
|
+
- **Letters look disconnected while I type in `cmd`.** `fa_input()` redraws the
|
|
148
|
+
line correctly as you type; the console's own echo of system prompts is
|
|
149
|
+
outside any program's control.
|
|
150
|
+
- **Perfect native rendering?** Use [Windows Terminal](https://aka.ms/terminal)
|
|
151
|
+
— the module detects it and lets it do the shaping.
|
|
152
|
+
- **Does the transform corrupt my data?** No — it is display-only. Values,
|
|
153
|
+
comparisons and files always hold logical text.
|
|
154
|
+
- **Other RTL languages?** The shaping tables cover Arabic and Persian letters;
|
|
155
|
+
the engine is language-agnostic for the letters it knows.
|
|
156
|
+
|
|
157
|
+
## Development
|
|
158
|
+
|
|
159
|
+
```bash
|
|
160
|
+
git clone https://github.com/Padandish/fa-console.git
|
|
161
|
+
cd fa-console
|
|
162
|
+
python -m pip install build
|
|
163
|
+
python -m build # creates dist/
|
|
164
|
+
```
|
|
165
|
+
|
|
166
|
+
Run the interactive example in any console:
|
|
167
|
+
|
|
168
|
+
```bash
|
|
169
|
+
python example.py
|
|
170
|
+
```
|
|
171
|
+
|
|
172
|
+
Run the full environment diagnostics:
|
|
173
|
+
|
|
174
|
+
```bash
|
|
175
|
+
python fa_console.py
|
|
176
|
+
```
|
|
177
|
+
|
|
178
|
+
## Author
|
|
179
|
+
|
|
180
|
+
**Alireza Hosseini** — [alireza.hosseini@hotmail.com](mailto:alireza.hosseini@hotmail.com)
|
|
181
|
+
|
|
182
|
+
### Tech channels
|
|
183
|
+
|
|
184
|
+
Follow **TechInsightsHub** for updates, tutorials and related projects:
|
|
185
|
+
|
|
186
|
+
| Platform | Link |
|
|
187
|
+
|----------|------|
|
|
188
|
+
| Bale | [ble.ir/TechInsightsHub](https://ble.ir/TechInsightsHub) |
|
|
189
|
+
| Eitaa | [eitaa.com/TechInsightsHub](https://eitaa.com/TechInsightsHub) |
|
|
190
|
+
| Aparat | [aparat.com/TechInsightsHub](https://aparat.com/TechInsightsHub) |
|
|
191
|
+
|
|
192
|
+
## License
|
|
193
|
+
|
|
194
|
+
[MIT](LICENSE) © Alireza Hosseini
|
|
@@ -0,0 +1,231 @@
|
|
|
1
|
+
Metadata-Version: 2.4
|
|
2
|
+
Name: fa-console
|
|
3
|
+
Version: 2.0.0
|
|
4
|
+
Summary: Correct Persian (Farsi) console input/output for Python on Windows: automatic UTF-8 setup plus visual letter shaping and bidi for the classic console.
|
|
5
|
+
Author-email: Alireza Hosseini <alireza.hosseini@hotmail.com>
|
|
6
|
+
License: MIT
|
|
7
|
+
Project-URL: Homepage, https://github.com/Padandish/fa-console
|
|
8
|
+
Project-URL: Repository, https://github.com/Padandish/fa-console
|
|
9
|
+
Project-URL: Issues, https://github.com/Padandish/fa-console/issues
|
|
10
|
+
Project-URL: Tech Channel (Bale), https://ble.ir/TechInsightsHub
|
|
11
|
+
Project-URL: Tech Channel (Eitaa), https://eitaa.com/TechInsightsHub
|
|
12
|
+
Project-URL: Video Tutorials (Aparat), https://aparat.com/TechInsightsHub
|
|
13
|
+
Keywords: persian,farsi,console,terminal,windows,unicode,utf-8,bidi,rtl,arabic,reshaper,cmd,codepage
|
|
14
|
+
Classifier: Development Status :: 5 - Production/Stable
|
|
15
|
+
Classifier: Environment :: Console
|
|
16
|
+
Classifier: Intended Audience :: Developers
|
|
17
|
+
Classifier: License :: OSI Approved :: MIT License
|
|
18
|
+
Classifier: Operating System :: Microsoft :: Windows
|
|
19
|
+
Classifier: Programming Language :: Python :: 3
|
|
20
|
+
Classifier: Programming Language :: Python :: 3.8
|
|
21
|
+
Classifier: Programming Language :: Python :: 3.9
|
|
22
|
+
Classifier: Programming Language :: Python :: 3.10
|
|
23
|
+
Classifier: Programming Language :: Python :: 3.11
|
|
24
|
+
Classifier: Programming Language :: Python :: 3.12
|
|
25
|
+
Classifier: Programming Language :: Python :: 3.13
|
|
26
|
+
Classifier: Programming Language :: Python :: 3.14
|
|
27
|
+
Classifier: Topic :: Software Development :: Internationalization
|
|
28
|
+
Classifier: Topic :: Software Development :: Libraries :: Python Modules
|
|
29
|
+
Classifier: Topic :: Terminals
|
|
30
|
+
Requires-Python: >=3.8
|
|
31
|
+
Description-Content-Type: text/markdown
|
|
32
|
+
License-File: LICENSE
|
|
33
|
+
Provides-Extra: high-fidelity
|
|
34
|
+
Requires-Dist: arabic-reshaper>=3.0; extra == "high-fidelity"
|
|
35
|
+
Requires-Dist: python-bidi>=0.4.2; extra == "high-fidelity"
|
|
36
|
+
Dynamic: license-file
|
|
37
|
+
|
|
38
|
+
# fa-console
|
|
39
|
+
|
|
40
|
+
> **One import. Correct Persian (Farsi) in the Windows console.**
|
|
41
|
+
|
|
42
|
+
**English** | [فارسی](README.fa.md)
|
|
43
|
+
|
|
44
|
+
[](https://pypi.org/project/fa-console/)
|
|
45
|
+
[](https://pypi.org/project/fa-console/)
|
|
46
|
+
[](LICENSE)
|
|
47
|
+
[](#compatibility)
|
|
48
|
+
|
|
49
|
+
`fa_console` makes Python's built-in `print()` and `input()` work correctly
|
|
50
|
+
with Persian text on Windows — **automatically**, with zero required
|
|
51
|
+
dependencies and no changes to your code beyond a single `import`.
|
|
52
|
+
|
|
53
|
+
It fixes both independent layers of the problem:
|
|
54
|
+
|
|
55
|
+
| Layer | Problem without `fa_console` | With `fa_console` |
|
|
56
|
+
|-------|------------------------------|-------------------|
|
|
57
|
+
| **Encoding** | Legacy console code page (720 / 1252 / …) mangles UTF-8 on redirect or piping; crashes with `UnicodeEncodeError` | Console code page switched to UTF-8 (65001); standard streams reconfigured to UTF-8; child processes pinned via `PYTHONUTF8` |
|
|
58
|
+
| **Rendering** | Classic console host (`conhost`) has no Arabic shaping and no bidirectional reordering — correct text is *drawn* disconnected and backwards | Every write is converted to visual order (letter shaping + bidi) so text appears joined and right-to-left. While typing, `fa_input` echoes correctly in real time |
|
|
59
|
+
|
|
60
|
+
**Before**
|
|
61
|
+
|
|
62
|
+
```
|
|
63
|
+
>>> print("سلام دنیا")
|
|
64
|
+
ﺎﯿﻧﺩ ﻡﻼﺳ # disconnected, reversed, unreadable
|
|
65
|
+
```
|
|
66
|
+
|
|
67
|
+
**After**
|
|
68
|
+
|
|
69
|
+
```
|
|
70
|
+
>>> import fa_console
|
|
71
|
+
>>> print("سلام دنیا")
|
|
72
|
+
سلام دنیا # joined, right-to-left, readable
|
|
73
|
+
```
|
|
74
|
+
|
|
75
|
+
## Features
|
|
76
|
+
|
|
77
|
+
- ✅ **One-line setup** — `import fa_console` configures everything at import time
|
|
78
|
+
- ✅ **Encoding layer** — UTF-8 code page, UTF-8 `sys.stdin`/`sys.stdout`/`sys.stderr`
|
|
79
|
+
(neutralises even a broken `PYTHONIOENCODING`), and pinned environment variables
|
|
80
|
+
for spawned subprocesses
|
|
81
|
+
- ✅ **Rendering layer** — logical→visual transformation (letter shaping into
|
|
82
|
+
Unicode Arabic Presentation Forms + bidirectional reordering) applied to *all*
|
|
83
|
+
output, including plain `print()` and f-strings
|
|
84
|
+
- ✅ **Two transformation backends** — uses `arabic-reshaper` + `python-bidi`
|
|
85
|
+
when installed (full UAX #9, lam-alef ligatures, Persian rules); otherwise a
|
|
86
|
+
built-in, dependency-free engine
|
|
87
|
+
- ✅ **Live-typing echo** — `fa_input()` renders letters joined and right-to-left
|
|
88
|
+
*while you type*, with built-in-`input()`-compatible semantics
|
|
89
|
+
(`KeyboardInterrupt` / `EOFError`)
|
|
90
|
+
- ✅ **Smart detection** — Windows Terminal, VS Code-style terminals, IDEs and
|
|
91
|
+
redirected files/pipes are detected and left untouched (standard text flows
|
|
92
|
+
there, as it should)
|
|
93
|
+
- ✅ **Fail-open design** — the module never crashes the host application; every
|
|
94
|
+
internal failure is logged through the `fa_console` logger and degrades
|
|
95
|
+
gracefully to standard Python behaviour
|
|
96
|
+
- ✅ **Zero required dependencies** — pure standard library, Python 3.8+
|
|
97
|
+
|
|
98
|
+
## Installation
|
|
99
|
+
|
|
100
|
+
```bash
|
|
101
|
+
pip install fa-console
|
|
102
|
+
```
|
|
103
|
+
|
|
104
|
+
Optional, higher-fidelity rendering backend:
|
|
105
|
+
|
|
106
|
+
```bash
|
|
107
|
+
pip install "fa-console[high-fidelity]" # arabic-reshaper + python-bidi
|
|
108
|
+
```
|
|
109
|
+
|
|
110
|
+
Or simply copy `fa_console.py` into your project — it is intentionally a
|
|
111
|
+
single, self-contained module.
|
|
112
|
+
|
|
113
|
+
## Quick start
|
|
114
|
+
|
|
115
|
+
```python
|
|
116
|
+
import fa_console # everything is configured here
|
|
117
|
+
|
|
118
|
+
print("سلام دنیا!") # renders correctly everywhere
|
|
119
|
+
name = fa_console.fa_input("نام شما: ") # live, correct echo while typing
|
|
120
|
+
print(f"سلام، {name} جان!") # logical data, correct display
|
|
121
|
+
```
|
|
122
|
+
|
|
123
|
+
The value returned by `input()` / `fa_input()` is always **logical-order**
|
|
124
|
+
Unicode: comparisons, `len()`, slicing, regexes and file writes all see plain,
|
|
125
|
+
correct Persian text. Visual transformation is a display-time-only concern.
|
|
126
|
+
|
|
127
|
+
## API overview
|
|
128
|
+
|
|
129
|
+
| Member | Purpose |
|
|
130
|
+
|--------|---------|
|
|
131
|
+
| `setup_console(force=False)` | Apply the full configuration; idempotent. Returns a `ConsoleSetupReport` |
|
|
132
|
+
| `fa_print(*args, **kwargs)` | Persian-safe drop-in for `print()` |
|
|
133
|
+
| `fa_input(prompt="")` | Persian-safe `input()` with live correct echo |
|
|
134
|
+
| `is_visual_mode()` | `True` when the display transform is active |
|
|
135
|
+
| `get_console_info()` | Dict snapshot of the detected environment (great for bug reports) |
|
|
136
|
+
| `VisualStream` | The transparent stream wrapper (advanced use) |
|
|
137
|
+
| `FaConsoleError` | Deliberate low-level failures (consumed internally — fail-open) |
|
|
138
|
+
|
|
139
|
+
### Self-diagnostics
|
|
140
|
+
|
|
141
|
+
```bash
|
|
142
|
+
python fa_console.py
|
|
143
|
+
```
|
|
144
|
+
|
|
145
|
+
Prints a full environment report: encodings, console code page, detected
|
|
146
|
+
terminal type, active bidi backend and a rendered sample.
|
|
147
|
+
|
|
148
|
+
## How it works
|
|
149
|
+
|
|
150
|
+
1. **Encoding layer** — `SetConsoleCP`/`SetConsoleOutputCP` (Win32) switch the
|
|
151
|
+
console to UTF-8; `sys.stdin`/`sys.stdout`/`sys.stderr` are reconfigured to
|
|
152
|
+
UTF-8; `PYTHONUTF8=1` / `PYTHONIOENCODING=utf-8` are pinned for child
|
|
153
|
+
processes.
|
|
154
|
+
2. **Rendering layer** — on classic `conhost` only, a `VisualStream` wrapper is
|
|
155
|
+
installed on stdout/stderr. Each write is converted: Arabic/Persian letters
|
|
156
|
+
are replaced by their contextual presentation forms (isolated / initial /
|
|
157
|
+
medial / final, including پ چ ژ ک گ ی), then the line is reordered for an
|
|
158
|
+
LTR-drawing terminal while Latin words and numbers stay upright and brackets
|
|
159
|
+
are mirrored.
|
|
160
|
+
3. **Detection** — the transform engages only when stdout is an interactive
|
|
161
|
+
classic console. Windows Terminal (`WT_SESSION`), VS Code terminals
|
|
162
|
+
(`TERM_PROGRAM`) and redirected output are bypassed, because they render
|
|
163
|
+
Arabic correctly or must receive standard logical text.
|
|
164
|
+
|
|
165
|
+
## Configuration
|
|
166
|
+
|
|
167
|
+
| Environment variable | Effect |
|
|
168
|
+
|----------------------|--------|
|
|
169
|
+
| `FA_CONSOLE_FORCE_VISUAL=1` | Force the visual transform on (debugging) |
|
|
170
|
+
| `FA_CONSOLE_NO_VISUAL=1` | Unconditionally disable the visual transform |
|
|
171
|
+
|
|
172
|
+
## Compatibility
|
|
173
|
+
|
|
174
|
+
| Environment | Behaviour |
|
|
175
|
+
|-------------|-----------|
|
|
176
|
+
| `cmd.exe` / standalone PowerShell (conhost) | Encoding fixed + visual transform + live typing echo |
|
|
177
|
+
| Windows Terminal | Encoding fixed; native rendering (module steps aside) |
|
|
178
|
+
| VS Code / JetBrains / IDLE consoles | Encoding fixed; module steps aside |
|
|
179
|
+
| Output redirected to file / pipe | Standard logical UTF-8 — safe for other tools |
|
|
180
|
+
| Linux / macOS | Safe no-op (standard behaviour) |
|
|
181
|
+
|
|
182
|
+
## Limitations & FAQ
|
|
183
|
+
|
|
184
|
+
- **Letters look disconnected while I type in `cmd`.** `fa_input()` redraws the
|
|
185
|
+
line correctly as you type; the console's own echo of system prompts is
|
|
186
|
+
outside any program's control.
|
|
187
|
+
- **Perfect native rendering?** Use [Windows Terminal](https://aka.ms/terminal)
|
|
188
|
+
— the module detects it and lets it do the shaping.
|
|
189
|
+
- **Does the transform corrupt my data?** No — it is display-only. Values,
|
|
190
|
+
comparisons and files always hold logical text.
|
|
191
|
+
- **Other RTL languages?** The shaping tables cover Arabic and Persian letters;
|
|
192
|
+
the engine is language-agnostic for the letters it knows.
|
|
193
|
+
|
|
194
|
+
## Development
|
|
195
|
+
|
|
196
|
+
```bash
|
|
197
|
+
git clone https://github.com/Padandish/fa-console.git
|
|
198
|
+
cd fa-console
|
|
199
|
+
python -m pip install build
|
|
200
|
+
python -m build # creates dist/
|
|
201
|
+
```
|
|
202
|
+
|
|
203
|
+
Run the interactive example in any console:
|
|
204
|
+
|
|
205
|
+
```bash
|
|
206
|
+
python example.py
|
|
207
|
+
```
|
|
208
|
+
|
|
209
|
+
Run the full environment diagnostics:
|
|
210
|
+
|
|
211
|
+
```bash
|
|
212
|
+
python fa_console.py
|
|
213
|
+
```
|
|
214
|
+
|
|
215
|
+
## Author
|
|
216
|
+
|
|
217
|
+
**Alireza Hosseini** — [alireza.hosseini@hotmail.com](mailto:alireza.hosseini@hotmail.com)
|
|
218
|
+
|
|
219
|
+
### Tech channels
|
|
220
|
+
|
|
221
|
+
Follow **TechInsightsHub** for updates, tutorials and related projects:
|
|
222
|
+
|
|
223
|
+
| Platform | Link |
|
|
224
|
+
|----------|------|
|
|
225
|
+
| Bale | [ble.ir/TechInsightsHub](https://ble.ir/TechInsightsHub) |
|
|
226
|
+
| Eitaa | [eitaa.com/TechInsightsHub](https://eitaa.com/TechInsightsHub) |
|
|
227
|
+
| Aparat | [aparat.com/TechInsightsHub](https://aparat.com/TechInsightsHub) |
|
|
228
|
+
|
|
229
|
+
## License
|
|
230
|
+
|
|
231
|
+
[MIT](LICENSE) © Alireza Hosseini
|