ansi_interface
Various functions to interface with the terminal, using ANSI sequences.
Credits:
- https://wiki.bash-hackers.org/scripting/terminalcodes
- https://gist.github.com/fnky/458719343aabd01cfb17a3a4f7296797
MouseAction
Bases: Enum
An enumeration of all the polled mouse actions
Source code in pytermgui/ansi_interface.py
377 378 379 380 381 382 383 384 385 386 387 388 389 390 391 392 393 394 395 396 397 398 399 400 401 402 403 404 405 406 407 408 409 410 |
|
HOVER = 'hover'
class-attribute
Mouse moved without clicking.
LEFT_CLICK = 'left_click'
class-attribute
Start of a left button action sequence.
LEFT_DRAG = 'left_drag'
class-attribute
Mouse moved while left button was held down.
RELEASE = 'release'
class-attribute
Mouse button released; end of any and all mouse action sequences.
RIGHT_CLICK = 'right_click'
class-attribute
Start of a right button action sequence.
RIGHT_DRAG = 'right_drag'
class-attribute
Mouse moved while right button was held down.
SCROLL_DOWN = 'scroll_down'
class-attribute
Mouse wheel or touchpad scroll downwards.
SCROLL_UP = 'scroll_up'
class-attribute
Mouse wheel or touchpad scroll upwards.
SHIFT_SCROLL_DOWN = 'shift_scroll_down'
class-attribute
Mouse wheel or touchpad scroll downwards.
SHIFT_SCROLL_UP = 'shift_scroll_up'
class-attribute
Mouse wheel or touchpad scroll upwards.
MouseEvent
dataclass
A class to represent events created by mouse actions.
Its first argument is a MouseAction
describing what happened,
and its second argument is a tuple[int, int]
describing where
it happened.
This class mostly exists for readability & typing reasons. It also implements the iterable protocol, so you can use the unpacking syntax, such as:
action, position = MouseEvent(...)
Source code in pytermgui/ansi_interface.py
413 414 415 416 417 418 419 420 421 422 423 424 425 426 427 428 429 430 431 432 433 434 435 436 437 438 439 440 441 442 443 444 445 446 447 448 449 450 451 452 453 454 455 456 457 458 459 460 461 462 463 464 465 466 467 468 |
|
__iter__()
Start iteration
Source code in pytermgui/ansi_interface.py
450 451 452 453 |
|
__next__()
Get next iteration item
Source code in pytermgui/ansi_interface.py
438 439 440 441 442 443 444 445 446 447 448 |
|
__post_init__()
Initialize iteration counter
Source code in pytermgui/ansi_interface.py
433 434 435 436 |
|
is_primary()
Returns True if event.action is one of the primary (left-button) actions.
Source code in pytermgui/ansi_interface.py
460 461 462 463 |
|
is_scroll()
Returns True if event.action is one of the scrolling actions.
Source code in pytermgui/ansi_interface.py
455 456 457 458 |
|
is_secondary()
Returns True if event.action is one of the secondary (secondary-button) actions.
Source code in pytermgui/ansi_interface.py
465 466 467 468 |
|
blink(text, reset_style=True)
Returns text blinking.
Parameters:
Name | Type | Description | Default |
---|---|---|---|
reset_style |
Optional[bool]
|
Boolean that determines whether a reset character should be appended to the end of the string. |
True
|
Source code in pytermgui/ansi_interface.py
681 682 683 684 685 686 687 688 689 |
|
bold(text, reset_style=True)
Returns text in bold.
Parameters:
Name | Type | Description | Default |
---|---|---|---|
reset_style |
Optional[bool]
|
Boolean that determines whether a reset character should be appended to the end of the string. |
True
|
Source code in pytermgui/ansi_interface.py
637 638 639 640 641 642 643 644 645 |
|
clear(what='screen')
Clears the specified screen region.
Parameters:
Name | Type | Description | Default |
---|---|---|---|
what |
str
|
The specifier defining the screen area. |
'screen'
|
Available options: * screen: clear whole screen and go to origin * bos: clear screen from cursor backwards * eos: clear screen from cursor forwards * line: clear line and go to beginning * bol: clear line from cursor backwards * eol: clear line from cursor forwards
Source code in pytermgui/ansi_interface.py
101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 |
|
cursor_column(num=0)
Moves the cursor to the num
-th character of the current line.
Parameters:
Name | Type | Description | Default |
---|---|---|---|
num |
int
|
The new cursor position. |
0
|
Note
This does not flush the terminal for performance reasons. You
can do it manually with sys.stdout.flush()
.
Source code in pytermgui/ansi_interface.py
276 277 278 279 280 281 282 283 284 285 286 287 |
|
cursor_down(num=1)
Moves the cursor up by num
lines.
Parameters:
Name | Type | Description | Default |
---|---|---|---|
num |
int
|
How many lines the cursor should move by. Must be positive,
to move in the opposite direction use |
1
|
Note
This does not flush the terminal for performance reasons. You
can do it manually with sys.stdout.flush()
.
Source code in pytermgui/ansi_interface.py
206 207 208 209 210 211 212 213 214 215 216 217 |
|
cursor_home()
Moves cursor to get_terminal().origin
.
Note
This does not flush the terminal for performance reasons. You
can do it manually with sys.stdout.flush()
.
Source code in pytermgui/ansi_interface.py
290 291 292 293 294 295 296 297 298 |
|
cursor_left(num=1)
Moves the cursor left by num
lines.
Parameters:
Name | Type | Description | Default |
---|---|---|---|
num |
int
|
How many characters the cursor should move by. Must be positive,
to move in the opposite direction use |
1
|
Note
This does not flush the terminal for performance reasons. You
can do it manually with sys.stdout.flush()
.
Source code in pytermgui/ansi_interface.py
234 235 236 237 238 239 240 241 242 243 244 245 |
|
cursor_next_line(num=1)
Moves the cursor to the beginning of the num
-th line downwards.
Parameters:
Name | Type | Description | Default |
---|---|---|---|
num |
int
|
The amount the cursor should move by. Must be positive, to move
in the opposite direction use |
1
|
Note
This does not flush the terminal for performance reasons. You
can do it manually with sys.stdout.flush()
.
Source code in pytermgui/ansi_interface.py
248 249 250 251 252 253 254 255 256 257 258 259 |
|
cursor_prev_line(num=1)
Moves the cursor to the beginning of the num
-th line upwards.
Parameters:
Name | Type | Description | Default |
---|---|---|---|
num |
int
|
The amount the cursor should move by. Must be positive, to move
in the opposite direction use |
1
|
Note
This does not flush the terminal for performance reasons. You
can do it manually with sys.stdout.flush()
.
Source code in pytermgui/ansi_interface.py
262 263 264 265 266 267 268 269 270 271 272 273 |
|
cursor_right(num=1)
Moves the cursor right by num
lines.
Parameters:
Name | Type | Description | Default |
---|---|---|---|
num |
int
|
How many characters the cursor should move by. Must be positive,
to move in the opposite direction use |
1
|
Note
This does not flush the terminal for performance reasons. You
can do it manually with sys.stdout.flush()
.
Source code in pytermgui/ansi_interface.py
220 221 222 223 224 225 226 227 228 229 230 231 |
|
cursor_up(num=1)
Moves the cursor up by num
lines.
Parameters:
Name | Type | Description | Default |
---|---|---|---|
num |
int
|
How many lines the cursor should move by. Must be positive,
to move in the opposite direction use |
1
|
Note
This does not flush the terminal for performance reasons. You
can do it manually with sys.stdout.flush()
.
Source code in pytermgui/ansi_interface.py
192 193 194 195 196 197 198 199 200 201 202 203 |
|
dim(text, reset_style=True)
Returns text in dim.
Parameters:
Name | Type | Description | Default |
---|---|---|---|
reset_style |
Optional[bool]
|
Boolean that determines whether a reset character should be appended to the end of the string. |
True
|
Source code in pytermgui/ansi_interface.py
648 649 650 651 652 653 654 655 656 |
|
hide_cursor()
Stops printing the cursor.
Source code in pytermgui/ansi_interface.py
129 130 131 132 |
|
inverse(text, reset_style=True)
Returns text inverse-colored.
Parameters:
Name | Type | Description | Default |
---|---|---|---|
reset_style |
Optional[bool]
|
Boolean that determines whether a reset character should be appended to the end of the string. |
True
|
Source code in pytermgui/ansi_interface.py
692 693 694 695 696 697 698 699 700 |
|
invisible(text, reset_style=True)
Returns text as invisible.
Parameters:
Name | Type | Description | Default |
---|---|---|---|
reset_style |
Optional[bool]
|
Boolean that determines whether a reset character should be appended to the end of the string. |
True
|
Note
This isn't very widely supported.
Source code in pytermgui/ansi_interface.py
703 704 705 706 707 708 709 710 711 712 713 714 |
|
italic(text, reset_style=True)
Returns text in italic.
Parameters:
Name | Type | Description | Default |
---|---|---|---|
reset_style |
Optional[bool]
|
Boolean that determines whether a reset character should be appended to the end of the string. |
True
|
Source code in pytermgui/ansi_interface.py
659 660 661 662 663 664 665 666 667 |
|
move_cursor(pos)
Moves the cursor.
Parameters:
Name | Type | Description | Default |
---|---|---|---|
pos |
tuple[int, int]
|
Tuple of that the cursor will be moved to. |
required |
This does not flush the terminal for performance reasons. You
can do it manually with sys.stdout.flush()
.
Source code in pytermgui/ansi_interface.py
178 179 180 181 182 183 184 185 186 187 188 189 |
|
overline(text, reset_style=True)
Return text overlined.
Parameters:
Name | Type | Description | Default |
---|---|---|---|
reset_style |
Optional[bool]
|
Boolean that determines whether a reset character should be appended to the end of the string. |
True
|
Note
This isnt' very widely supported.
Source code in pytermgui/ansi_interface.py
728 729 730 731 732 733 734 735 736 737 738 739 |
|
print_to(pos, *args, **kwargs)
Prints text to given pos
.
Note
This method passes through all arguments (except for pos
) to the print
method.
Source code in pytermgui/ansi_interface.py
619 620 621 622 623 624 625 626 627 628 |
|
report_cursor()
Gets position of cursor.
Returns:
Type | Description |
---|---|
Optional[tuple[int, int]]
|
A tuple of integers, (columns, rows), describing the |
Optional[tuple[int, int]]
|
current (printing) cursor's position. Returns None if |
Optional[tuple[int, int]]
|
this could not be determined. |
Optional[tuple[int, int]]
|
Note that this position is not the mouse position. See |
Optional[tuple[int, int]]
|
|
Source code in pytermgui/ansi_interface.py
156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 |
|
report_mouse(event, method='decimal_xterm', stop=False)
Starts reporting of mouse events.
You can specify multiple events to report on.
Parameters:
Name | Type | Description | Default |
---|---|---|---|
event |
str
|
The type of event to report on. See below for options. |
required |
method |
Optional[str]
|
The method of reporting to use. See below for options. |
'decimal_xterm'
|
stop |
bool
|
If set to True, the stopping code is written to stdout. |
False
|
Raises:
Type | Description |
---|---|
NotImplementedError
|
The given event is not supported. |
Note
If you need this functionality, you're probably better off using the wrapper
pytermgui.context_managers.mouse_handler
, which allows listening on multiple
events, gives a translator method and handles exceptions.
Possible events
- press: Report when the mouse is clicked, left or right button.
- highlight: Report highlighting.
- press_hold: Report with a left or right click, as well as both left & right drag and release.
- hover: Report even when no active action is done, only the mouse is moved.
Methods
- None: Non-decimal xterm method. Limited in coordinates.
- decimal_xterm: The default setting. Most universally supported.
- decimal_urxvt: Older, less compatible, but useful on some systems.
- decimal_utf8: Apparently not too stable.
More information here.
Source code in pytermgui/ansi_interface.py
471 472 473 474 475 476 477 478 479 480 481 482 483 484 485 486 487 488 489 490 491 492 493 494 495 496 497 498 499 500 501 502 503 504 505 506 507 508 509 510 511 512 513 514 515 516 517 518 519 520 521 522 523 524 525 526 527 528 529 530 531 532 533 534 535 536 537 538 539 540 |
|
reset()
Resets printing mode.
Source code in pytermgui/ansi_interface.py
631 632 633 634 |
|
restore_cursor()
Restore cursor position as saved by save_cursor
.
Source code in pytermgui/ansi_interface.py
150 151 152 153 |
|
restore_screen()
Restores the contents of the screen saved by save_screen()
.
Source code in pytermgui/ansi_interface.py
83 84 85 86 |
|
save_cursor()
Saves the current cursor position.
Use restore_cursor
to restore it.
Source code in pytermgui/ansi_interface.py
141 142 143 144 145 146 147 |
|
save_screen()
Saves the contents of the screen, and wipes it.
Use restore_screen()
to get them back.
Source code in pytermgui/ansi_interface.py
74 75 76 77 78 79 80 |
|
set_alt_buffer()
Starts an alternate buffer.
Source code in pytermgui/ansi_interface.py
89 90 91 92 |
|
set_echo()
Starts echoing of user input.
Note
This is currently only available on POSIX.
Source code in pytermgui/ansi_interface.py
351 352 353 354 355 356 357 358 359 360 361 |
|
set_mode(mode, write=True)
Sets terminal display mode.
This is better left internal. To use these modes, you can call their
specific functions, such as bold("text")
or italic("text")
.
Parameters:
Name | Type | Description | Default |
---|---|---|---|
mode |
Union[str, int]
|
One of the available modes. Strings and integers both work. |
required |
write |
bool
|
Boolean that determines whether the output should be written to stdout. |
True
|
Returns:
Type | Description |
---|---|
str
|
A string that sets the given mode. |
Available modes
- 0: reset
- 1: bold
- 2: dim
- 3: italic
- 4: underline
- 5: blink
- 7: inverse
- 8: invisible
- 9: strikethrough
- 53: overline
Source code in pytermgui/ansi_interface.py
301 302 303 304 305 306 307 308 309 310 311 312 313 314 315 316 317 318 319 320 321 322 323 324 325 326 327 328 329 330 331 332 333 334 335 336 337 338 339 340 341 342 343 344 345 346 347 348 |
|
show_cursor()
Starts printing the cursor.
Source code in pytermgui/ansi_interface.py
135 136 137 138 |
|
strikethrough(text, reset_style=True)
Return text as strikethrough.
Parameters:
Name | Type | Description | Default |
---|---|---|---|
reset_style |
Optional[bool]
|
Boolean that determines whether a reset character should be appended to the end of the string. |
True
|
Source code in pytermgui/ansi_interface.py
717 718 719 720 721 722 723 724 725 |
|
translate_mouse(code, method)
Translates the output of produced by setting report_mouse
into MouseEvents.
This method currently only supports decimal_xterm
and decimal_urxvt
.
Parameters:
Name | Type | Description | Default |
---|---|---|---|
code |
str
|
The string of mouse code(s) to translate. |
required |
method |
str
|
The reporting method to translate. One of |
required |
Returns:
Type | Description |
---|---|
list[MouseEvent | None] | None
|
A list of optional mouse events obtained from the code argument. If the code was malformed, |
list[MouseEvent | None] | None
|
and no codes could be determined None is returned. |
Source code in pytermgui/ansi_interface.py
543 544 545 546 547 548 549 550 551 552 553 554 555 556 557 558 559 560 561 562 563 564 565 566 567 568 569 570 571 572 573 574 575 576 577 578 579 580 581 582 583 584 585 586 587 588 589 590 591 592 593 594 595 596 597 598 599 600 601 602 603 604 605 606 607 608 609 610 611 612 613 614 615 |
|
underline(text, reset_style=True)
Returns text underlined.
Parameters:
Name | Type | Description | Default |
---|---|---|---|
reset_style |
Optional[bool]
|
Boolean that determines whether a reset character should be appended to the end of the string. |
True
|
Source code in pytermgui/ansi_interface.py
670 671 672 673 674 675 676 677 678 |
|
unset_alt_buffer()
Returns to main buffer, restoring its original state.
Source code in pytermgui/ansi_interface.py
95 96 97 98 |
|
unset_echo()
Stops echoing of user input.
Note
This is currently only available on POSIX.
Source code in pytermgui/ansi_interface.py
364 365 366 367 368 369 370 371 372 373 374 |
|