| 1 | <!----> |
| 2 | |
| 3 |  |
| 4 | |
| 5 | > Python TUI framework with mouse support, modular widget system, customizable and rapid terminal markup language and more! |
| 6 | |
| 7 | ```bash |
| 8 | pip3 install pytermgui |
| 9 | ``` |
| 10 | |
| 11 | <p align=center> |
| 12 | <a href="https://pypi.org/project/pytermgui"> |
| 13 | <img alt="PyPi project" src="https://img.shields.io/pypi/v/pytermgui?color=brightgreen"> |
| 14 | </a> |
| 15 | <a href="https://github.com/bczsalba/pytermgui/blob/master/utils/create_badge.py"> |
| 16 | <img alt="Code quality" src="https://raw.githubusercontent.com/bczsalba/pytermgui/master/assets/badges/quality.svg"> |
| 17 | </a> |
| 18 | <a href="http://ptg.bczsalba.com/"> |
| 19 | <img src="https://img.shields.io/badge/documentation-up%20to%20date-brightgreen"> |
| 20 | </a> |
| 21 | <a href="https://github.com/bczsalba/pytermgui/actions/workflows/pytest.yml"> |
| 22 | <img src="https://github.com/bczsalba/pytermgui/actions/workflows/pytest.yml/badge.svg"> |
| 23 | </a> |
| 24 | </p> |
| 25 | <p align=center> |
| 26 | <a href="https://discord.gg/g4bqMvpG4U"> |
| 27 | <img src="https://img.shields.io/discord/999374285686706367?label=join%20our%20discord"> |
| 28 | </a> |
| 29 | </p> |
| 30 | |
| 31 | ## Notice |
| 32 | |
| 33 | A much better, more complete version of PTG's core ideas now exists over at [Shade 40](https://github.com/shade40). While PTG is not yet fully obsolete, |
| 34 | those libraries will be the primary focus of development going forward. |
| 35 | |
| 36 | <hr> |
| 37 | |
| 38 | ## Why? |
| 39 | |
| 40 | Mostly because terminals are cool, but creating terminal apps has historically been difficult. PyTermGUI aims to provide a simple, readable and modular way to make the app of your dreams! |
| 41 | |
| 42 | Terminal apps are (often): |
| 43 | |
| 44 | - Easier to install |
| 45 | - Faster & more resource efficient |
| 46 | - Less prone to differences between environments (no IE7 here!) |
| 47 | |
| 48 | ...than their web or native counterparts. |
| 49 | |
| 50 | ## How? |
| 51 | |
| 52 | We provide a couple of things to make your life easier: |
| 53 | |
| 54 | - Sensible abstractions over most terminal standards |
| 55 | - A fully fledged, desktop-inspired window manager system with modals and completely customizable windows |
| 56 | - Mouse support out of the box with **0** configuration |
| 57 | - YAML (or Python) based styling engines |
| 58 | - TIM, our markup language for creating styled terminal text with expressive text, including systems for aliases & macros |
| 59 | - A bunch of things I can't think of right now :slightly_smiling_face: |
| 60 | |
| 61 | Additionally, there are a couple of neat tools to make your general Python development easier: |
| 62 | |
| 63 | - An inspection utility |
| 64 | - A pretty printer for both the REPL and IPython |
| 65 | - A way to create SVG and HTML screenshots of your terminal |
| 66 | |
| 67 | <!-- HATCH README END --> |
| 68 | |
| 69 | ## Examples |
| 70 | |
| 71 | > All images below are generated directly from the source displayed by a PyTermGUI-powered SVG exporter tool, [Termage](https://github.com/bczsalba/termage). |
| 72 | |
| 73 | Your first application, a simple clock: |
| 74 | |
| 75 | ```python3 |
| 76 | import time |
| 77 | |
| 78 | import pytermgui as ptg |
| 79 | |
| 80 | def macro_time(fmt: str) -> str: |
| 81 | return time.strftime(fmt) |
| 82 | |
| 83 | ptg.tim.define("!time", macro_time) |
| 84 | |
| 85 | with ptg.WindowManager() as manager: |
| 86 | manager.layout.add_slot("Body") |
| 87 | manager.add( |
| 88 | ptg.Window("[bold]The current time is:[/]\n\n[!time 75]%c", box="EMPTY") |
| 89 | ) |
| 90 | ``` |
| 91 | |
| 92 | Since strings are converted into the `Label` widget, and all widgets use markup for styling, we can use a custom-defined TIM macro function to return the current time. After running the above, you should see something like: |
| 93 | |
| 94 | <p align="center"> |
| 95 | <img alt="Clock example output" src="https://github.com/bczsalba/pytermgui/raw/master/assets/readme/clock.svg"> |
| 96 | </p> |
| 97 | |
| 98 | For something a bit more in-depth, see this contact form inspired by [asciimatics' example](https://github.com/peterbrittain/asciimatics#how-to-use-it): |
| 99 | |
| 100 | ```python3 |
| 101 | import pytermgui as ptg |
| 102 | |
| 103 | CONFIG = """ |
| 104 | config: |
| 105 | InputField: |
| 106 | styles: |
| 107 | prompt: dim italic |
| 108 | cursor: '@72' |
| 109 | Label: |
| 110 | styles: |
| 111 | value: dim bold |
| 112 | |
| 113 | Window: |
| 114 | styles: |
| 115 | border: '60' |
| 116 | corner: '60' |
| 117 | |
| 118 | Container: |
| 119 | styles: |
| 120 | border: '96' |
| 121 | corner: '96' |
| 122 | """ |
| 123 | |
| 124 | with ptg.YamlLoader() as loader: |
| 125 | loader.load(CONFIG) |
| 126 | |
| 127 | with ptg.WindowManager() as manager: |
| 128 | window = ( |
| 129 | ptg.Window( |
| 130 | "", |
| 131 | ptg.InputField("Balazs", prompt="Name: "), |
| 132 | ptg.InputField("Some street", prompt="Address: "), |
| 133 | ptg.InputField("+11 0 123 456", prompt="Phone number: "), |
| 134 | "", |
| 135 | ptg.Container( |
| 136 | "Additional notes:", |
| 137 | ptg.InputField( |
| 138 | "A whole bunch of\nMeaningful notes\nand stuff", multiline=True |
| 139 | ), |
| 140 | box="EMPTY_VERTICAL", |
| 141 | ), |
| 142 | "", |
| 143 | ["Submit", lambda *_: submit(manager, window)], |
| 144 | width=60, |
| 145 | box="DOUBLE", |
| 146 | ) |
| 147 | .set_title("[210 bold]New contact") |
| 148 | .center() |
| 149 | ) |
| 150 | |
| 151 | manager.add(window) |
| 152 | ``` |
| 153 | |
| 154 | This showcases the YAML-based config system, as well as some additional API. I recommended checking out the [source file](https://github.com/bczsalba/pytermgui/blob/master/utils/readme_scripts/contact.py) to see how the `submit` callback works. |
| 155 | |
| 156 | <p align="center"> |
| 157 | <img alt="Contact form example output" src="https://github.com/bczsalba/pytermgui/raw/master/assets/readme/contact.svg"> |
| 158 | </p> |
| 159 | |
| 160 | ## Not a fan of colors? We've got you! |
| 161 | |
| 162 | PyTermGUI is one of the only TUI libraries that offers [NO_COLOR](https://no-color.org) support that doesn't ~~suck~~ ruin the usability & design of your apps. |
| 163 | |
| 164 | This is how the above example looks like with the environment variable `NO_COLOR` set to anything. Note how contrast between colors is retained, as well as the inclusion of background colors: |
| 165 | |
| 166 | |
| 167 | <p align="center"> |
| 168 | <img alt="Contact form NO_COLOR output" src="https://github.com/bczsalba/pytermgui/raw/master/assets/readme/contact_no_color.svg"> |
| 169 | </p> |
| 170 | |
| 171 | |
| 172 | ## Older terminals? No problem! |
| 173 | |
| 174 | We use algorithms based on human vision to convert and downgrade colors when the current terminal emulator doesn't support them. Here is a cool screenshot: |
| 175 | |
| 176 | <p align="center"> |
| 177 | <img alt="Contact form NO_COLOR output" src="https://github.com/bczsalba/pytermgui/raw/master/assets/readme/colorgrids.png"> |
| 178 | <figcaption><em>Disclaimer:</em> Termage currently doesn't play nicely to changing colorsystems during runtime, so this image had to be captured natively :(</figcaption> |
| 179 | </p> |
| 180 | |
| 181 | |
| 182 | ## Questions? See the docs! |
| 183 | |
| 184 | Pretty much every single name in the library, private or public, has an insightful dockstring attached to it, and we are accumulating a growing amount of walkthrough-based documentations articles. See 'em all on the [doc website](https://ptg.bczsalba.com)! |
| 185 | |
| 186 | |
| 187 | ## Contributions, issues et al. |
| 188 | |
| 189 | If you have any problems using the library, feel free to open up a discussion or raise an issue ticket. If you would prefer to hack on the library yourself, see the [contribution guidelines](https://github.com/bczsalba/pytermgui/blob/master/CONTRIBUTING.md). Pull requests are encouraged, but make sure you aren't trying to fix an issue that others are already working on, for your own sake. :slightly_smiling_face: |