"Fossies" - the Fresh Open Source Software Archive

Member "polysh-polysh-0.13/polysh/console.py" (11 May 2020, 2052 Bytes) of package /linux/privat/polysh-polysh-0.13.tar.gz:


As a special service "Fossies" has tried to format the requested source page into HTML format using (guessed) Python source code syntax highlighting (style: standard) with prefixed line numbers. Alternatively you can here view or download the uninterpreted source code file. For more information about "console.py" see the Fossies "Dox" file reference documentation.

    1 """Polysh - Console Utilities
    2 
    3 Copyright (c) 2006 Guillaume Chazarain <guichaz@gmail.com>
    4 Copyright (c) 2018 InnoGames GmbH
    5 """
    6 # This program is free software: you can redistribute it and/or modify
    7 # it under the terms of the GNU General Public License as published by
    8 # the Free Software Foundation, either version 2 of the License, or
    9 # (at your option) any later version.
   10 #
   11 # This program is distributed in the hope that it will be useful,
   12 # but WITHOUT ANY WARRANTY; without even the implied warranty of
   13 # MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
   14 # GNU General Public License for more details.
   15 #
   16 # You should have received a copy of the GNU General Public License
   17 # along with this program.  If not, see <http://www.gnu.org/licenses/>.
   18 
   19 import errno
   20 import os
   21 from typing import Optional
   22 
   23 # We remember the length of the last printed status in order to
   24 # clear it with ' ' characters
   25 last_status_length = None
   26 
   27 
   28 def safe_write(buf: bytes) -> None:
   29     """We can get a SIGWINCH when printing, which will cause write to raise
   30     an EINTR. That's not a reason to stop printing."""
   31     while True:
   32         try:
   33             os.write(1, buf)
   34             break
   35         except IOError as e:
   36             if e.errno != errno.EINTR:
   37                 raise
   38 
   39 
   40 def console_output(msg: bytes, logging_msg: Optional[bytes] = None) -> None:
   41     """Use instead of print, to clear the status information before printing"""
   42     from polysh import remote_dispatcher
   43 
   44     remote_dispatcher.log(logging_msg or msg)
   45     if remote_dispatcher.options.interactive:
   46         from polysh.stdin import the_stdin_thread
   47         the_stdin_thread.no_raw_input()
   48         global last_status_length
   49         if last_status_length:
   50             safe_write('\r{}\r'.format(
   51                 last_status_length * ' ').encode())
   52             last_status_length = 0
   53     safe_write(msg)
   54 
   55 
   56 def set_last_status_length(length: int) -> None:
   57     """The length of the prefix to be cleared when printing something"""
   58     global last_status_length
   59     last_status_length = length