|
| 1 | +# MIT License |
| 2 | +# |
| 3 | +# Copyright (c) 2023 Eugenio Parodi <ceccopierangiolieugenio AT googlemail DOT com> |
| 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. |
| 22 | + |
| 23 | +__all__ = ['TTkTerm'] |
| 24 | + |
| 25 | +from ..TTkTerm.term_base import TTkTermBase |
| 26 | +from .term_unix import * |
| 27 | + |
| 28 | +class _TTkTermSerial(): |
| 29 | + @staticmethod |
| 30 | + def _getTerminalSizeSerial(): |
| 31 | + import sys, os, tty, select, re |
| 32 | + try: import termios, fcntl |
| 33 | + except Exception as e: |
| 34 | + print(f'ERROR: {e}') |
| 35 | + exit(1) |
| 36 | + _attr = termios.tcgetattr(sys.stdin) |
| 37 | + tty.setcbreak(sys.stdin) |
| 38 | + def _read_new(): |
| 39 | + stdinRead = '' |
| 40 | + while rlist := select.select( [sys.stdin], [], [] )[0]: |
| 41 | + _fl = fcntl.fcntl(sys.stdin, fcntl.F_GETFL) |
| 42 | + fcntl.fcntl(sys.stdin, fcntl.F_SETFL, _fl | os.O_NONBLOCK) # Set the input as NONBLOCK to read the full sequence |
| 43 | + stdinRead = sys.stdin.buffer.read() |
| 44 | + fcntl.fcntl(sys.stdin, fcntl.F_SETFL, _fl) |
| 45 | + try: |
| 46 | + stdinRead = stdinRead.decode() |
| 47 | + except Exception as e: |
| 48 | + yield f"bin: {stdinRead}" |
| 49 | + continue |
| 50 | + print(f"{len(stdinRead)=}") |
| 51 | + if '\033' in stdinRead: |
| 52 | + stdinSplit = stdinRead.split('\033') |
| 53 | + for ansi in stdinSplit[1:]: |
| 54 | + print(f"{ansi=}") |
| 55 | + yield ansi |
| 56 | + else: |
| 57 | + for ch in stdinRead: |
| 58 | + yield ch |
| 59 | + w,h = 80,24 |
| 60 | + try: |
| 61 | + sys.stdout.write('\033[s\033[999;999H\033[6n\033[u') |
| 62 | + sys.stdout.flush() |
| 63 | + for stdinRead in _read_new(): |
| 64 | + if m := re.match(r"\[(\d+);(\d+)R",stdinRead): |
| 65 | + h = int(m.group(1)) |
| 66 | + w = int(m.group(2)) |
| 67 | + break |
| 68 | + finally: |
| 69 | + termios.tcsetattr(sys.stdin, termios.TCSANOW, _attr) |
| 70 | + return w,h |
| 71 | + _w, _h = None,None |
| 72 | + |
| 73 | + @staticmethod |
| 74 | + def _getTerminalSize(): |
| 75 | + if _TTkTermSerial._w is None: |
| 76 | + _TTkTermSerial._w, _TTkTermSerial._h = _TTkTermSerial._getTerminalSizeSerial() |
| 77 | + return _TTkTermSerial._w, _TTkTermSerial._h |
| 78 | + |
| 79 | + TTkTermBase.getTerminalSize = _getTerminalSize |
| 80 | + |
| 81 | + |
| 82 | + |
0 commit comments