當前位置: 首頁>>代碼示例>>Python>>正文


Python tty.setcbreak方法代碼示例

本文整理匯總了Python中tty.setcbreak方法的典型用法代碼示例。如果您正苦於以下問題:Python tty.setcbreak方法的具體用法?Python tty.setcbreak怎麽用?Python tty.setcbreak使用的例子?那麽, 這裏精選的方法代碼示例或許可以為您提供幫助。您也可以進一步了解該方法所在tty的用法示例。


在下文中一共展示了tty.setcbreak方法的15個代碼示例,這些例子默認根據受歡迎程度排序。您可以為喜歡或者感覺有用的代碼點讚,您的評價將有助於係統推薦出更棒的Python代碼示例。

示例1: yesno

# 需要導入模塊: import tty [as 別名]
# 或者: from tty import setcbreak [as 別名]
def yesno(cli_opts: AskCLIOptions, items: List[str]) -> Response:
    import tty
    with alternate_screen():
        if cli_opts.message:
            print(styled(cli_opts.message, bold=True))
        print()
        print(' ', styled('Y', fg='green') + 'es', ' ', styled('N', fg='red') + 'o', set_cursor_visible(False))
        sys.stdout.flush()
        tty.setraw(sys.stdin.fileno())
        try:
            response = sys.stdin.buffer.read(1)
            yes = response in (b'y', b'Y', b'\r', b'\n' b' ')
            return {'items': items, 'response': 'y' if yes else 'n'}
        finally:
            sys.stdout.write(set_cursor_visible(True))
            tty.setcbreak(sys.stdin.fileno())
            sys.stdout.flush() 
開發者ID:kovidgoyal,項目名稱:kitty,代碼行數:19,代碼來源:main.py

示例2: main

# 需要導入模塊: import tty [as 別名]
# 或者: from tty import setcbreak [as 別名]
def main():
    #setcbreak(stdin, TCSANOW)
    stdin = sys.stdin.buffer
    stdout = sys.stdout.buffer

    stdout.write(LCP1_EN + LCP1_EN[1:])
    stdout.flush()
    assert stdin.read(len(LCP1_EN)) == LCP1_EN
    assert stdin.read(len(LCP1_EN)) == LCP1_EN

    time.sleep(0.2)  # waiting for auth ok

    stdout.write(IP1_EN)
    stdout.flush()
    assert stdin.read(len(IP1_EN)) == IP1_EN
    stdout.write(IP1_EN)
    stdout.flush()

    time.sleep(0.2) 
開發者ID:sorz,項目名稱:sstp-server,代碼行數:21,代碼來源:pppd.py

示例3: ttypager

# 需要導入模塊: import tty [as 別名]
# 或者: from tty import setcbreak [as 別名]
def ttypager(text):
    """Page through text on a text terminal."""
    lines = plain(text).split('\n')
    try:
        import tty
        fd = sys.stdin.fileno()
        old = tty.tcgetattr(fd)
        tty.setcbreak(fd)
        getchar = lambda: sys.stdin.read(1)
    except (ImportError, AttributeError):
        tty = None
        getchar = lambda: sys.stdin.readline()[:-1][:1]

    try:
        r = inc = os.environ.get('LINES', 25) - 1
        sys.stdout.write('\n'.join(lines[:inc]) + '\n')
        while lines[r:]:
            sys.stdout.write('-- more --')
            sys.stdout.flush()
            c = getchar()

            if c in ('q', 'Q'):
                sys.stdout.write('\r          \r')
                break
            elif c in ('\r', '\n'):
                sys.stdout.write('\r          \r' + lines[r] + '\n')
                r = r + 1
                continue
            if c in ('b', 'B', '\x1b'):
                r = r - inc - inc
                if r < 0: r = 0
            sys.stdout.write('\n' + '\n'.join(lines[r:r+inc]) + '\n')
            r = r + inc

    finally:
        if tty:
            tty.tcsetattr(fd, tty.TCSAFLUSH, old) 
開發者ID:war-and-code,項目名稱:jawfish,代碼行數:39,代碼來源:pydoc.py

示例4: ttypager

# 需要導入模塊: import tty [as 別名]
# 或者: from tty import setcbreak [as 別名]
def ttypager(text):
    """Page through text on a text terminal."""
    lines = split(plain(text), '\n')
    try:
        import tty
        fd = sys.stdin.fileno()
        old = tty.tcgetattr(fd)
        tty.setcbreak(fd)
        getchar = lambda: sys.stdin.read(1)
    except (ImportError, AttributeError):
        tty = None
        getchar = lambda: sys.stdin.readline()[:-1][:1]

    try:
        r = inc = os.environ.get('LINES', 25) - 1
        sys.stdout.write(join(lines[:inc], '\n') + '\n')
        while lines[r:]:
            sys.stdout.write('-- more --')
            sys.stdout.flush()
            c = getchar()

            if c in ('q', 'Q'):
                sys.stdout.write('\r          \r')
                break
            elif c in ('\r', '\n'):
                sys.stdout.write('\r          \r' + lines[r] + '\n')
                r = r + 1
                continue
            if c in ('b', 'B', '\x1b'):
                r = r - inc - inc
                if r < 0: r = 0
            sys.stdout.write('\n' + join(lines[r:r+inc], '\n') + '\n')
            r = r + inc

    finally:
        if tty:
            tty.tcsetattr(fd, tty.TCSAFLUSH, old) 
開發者ID:glmcdona,項目名稱:meddle,代碼行數:39,代碼來源:pydoc.py

示例5: main

# 需要導入模塊: import tty [as 別名]
# 或者: from tty import setcbreak [as 別名]
def main(self,url):

        tty.setraw(sys.stdin)
        while self.tab != -1:
            self.run_tab(0, self.print_history, self.run_history_tab)
            self.run_tab(1, self.print_send_tab, self.run_send_tab)
            self.run_tab(2, self.print_receive, self.run_receive_tab)
            self.run_tab(3, self.print_contacts, self.run_contacts_tab)
            self.run_tab(4, self.print_banner, self.run_banner_tab)

        tty.setcbreak(sys.stdin)
        curses.nocbreak()
        self.stdscr.keypad(0)
        curses.echo()
        curses.endwin() 
開發者ID:mazaclub,項目名稱:encompass,代碼行數:17,代碼來源:text.py

示例6: posix_shell

# 需要導入模塊: import tty [as 別名]
# 或者: from tty import setcbreak [as 別名]
def posix_shell(chan):
    import termios
    import tty

    oldtty = termios.tcgetattr(sys.stdin)
    try:
        tty.setraw(sys.stdin.fileno())
        tty.setcbreak(sys.stdin.fileno())
        chan.settimeout(0.0)

        while True:
            r, w, e = select.select([chan, sys.stdin], [], [])
            if chan in r:
                try:
                    x = chan.recv(1024).decode("UTF-8")
                    if len(x) == 0:
                        break
                    sys.stdout.write(x)
                    sys.stdout.flush()
                except socket.timeout:
                    pass

            if sys.stdin in r:
                x = sys.stdin.read(1)
                if len(x) == 0:
                    break
                chan.send(x)
    finally:
        termios.tcsetattr(sys.stdin, termios.TCSADRAIN, oldtty)
        return 
開發者ID:d0ubl3g,項目名稱:Industrial-Security-Auditing-Framework,代碼行數:32,代碼來源:__init__.py

示例7: ask_user

# 需要導入模塊: import tty [as 別名]
# 或者: from tty import setcbreak [as 別名]
def ask_user(question: str, default: bool, new_line: bool = False) -> bool:
    """
    Asks the user a yes/no question.
    :param question:    The question to ask
    :param default:     The default answer, if user presses enter.
                        True for yes, False for no
    :param new_line:    If new_line before printing the question
    :return:            yes: True, no: False
    """

    yes = ["y"]
    no = ["n"]
    if default:
        yes.append("")
        choices = "Y/n"
    else:
        no.append("")
        choices = "N/y"

    while True:
        print(aurman_question("{} {}: ".format(question, choices), new_line=new_line, to_print=False),
              end='', flush=True)

        # see https://stackoverflow.com/a/36974338
        fd = sys.stdin.fileno()
        old_settings = termios.tcgetattr(fd)

        try:
            tty.setcbreak(fd)
            answer = sys.stdin.read(1)
        finally:
            termios.tcsetattr(fd, termios.TCSADRAIN, old_settings)

        print(flush=True)
        user_choice = answer.strip().lower()
        if user_choice in yes or user_choice in no:
            return user_choice in yes
        aurman_error("That was not a valid choice!") 
開發者ID:polygamma,項目名稱:aurman,代碼行數:40,代碼來源:utilities.py

示例8: interactive_shell

# 需要導入模塊: import tty [as 別名]
# 或者: from tty import setcbreak [as 別名]
def interactive_shell(chan, callback=None):
    oldtty = termios.tcgetattr(sys.stdin)
    try:
        tty.setraw(sys.stdin.fileno())
        tty.setcbreak(sys.stdin.fileno())
        chan.settimeout(0.0)

        while True:
            r, w, e = select.select([chan, sys.stdin], [], [])
            if chan in r:
                try:
                    x = u(chan.recv(1024))
                    if len(x) == 0:
                        sys.stdout.write("\r\n[+] Terminating SSH connection\r\n")
                        sys.stdout.flush()
                        if callback != None:
                            callback()
                        break
                    sys.stdout.write(x)
                    sys.stdout.flush()
                except socket.timeout:
                    pass
            if sys.stdin in r:
                x = sys.stdin.read(1)
                if len(x) == 0:
                    break
                chan.send(x)

    finally:
        termios.tcsetattr(sys.stdin, termios.TCSADRAIN, oldtty) 
開發者ID:ivRodriguezCA,項目名稱:decrypt-ios-apps-script,代碼行數:32,代碼來源:helpers.py

示例9: __enter__

# 需要導入模塊: import tty [as 別名]
# 或者: from tty import setcbreak [as 別名]
def __enter__(self) -> os.terminal_size:
        real_term_geometry = shutil.get_terminal_size((80, 80))
        if sys.stdin.isatty():
            tty.setcbreak(sys.stdin.fileno())
            if sys.stderr.isatty():
                tty.setcbreak(sys.stderr.fileno())
            if sys.stdout.isatty():
                tty.setcbreak(sys.stdout.fileno())
        return real_term_geometry 
開發者ID:actionless,項目名稱:pikaur,代碼行數:11,代碼來源:pikspect.py

示例10: _wait_for_interval

# 需要導入模塊: import tty [as 別名]
# 或者: from tty import setcbreak [as 別名]
def _wait_for_interval(self):
        """Wait for the time interval to expire

        This method issues a timing loop to wait for the specified interval to
        expire or quit if the user presses 'q' or 'Q'. The method passes all
        other keyboard requests to the _do_command() method for processing.

        If the interval expires, the method returns None.
        If the user presses a key, the method returns the numeric key number.

        Returns - None or int (see above)
        """
        # If on *nix systems, set the terminal IO sys to not echo
        if not self.no_keyboard and os.name == "posix":
            import tty
            import termios
            old_settings = termios.tcgetattr(sys.stdin)
            tty.setcbreak(sys.stdin.fileno())

        key = None
        done = False
        try:
            # Loop for interval in seconds while detecting keypress
            while not done:
                done = self.alarm <= time.time()
                if not self.no_keyboard and kbhit() and not done:
                    key = getch()
                    done = True
                if os.name != "posix":
                    # On Windows wait a few ms to avoid 100% CPU usage for
                    # polling input (handled in kbhit() for posix systems).
                    time.sleep(_IDLE_TIME_INPUT_POLLING)
        finally:
            # Ensure terminal IO sys is reset to older state.
            if not self.no_keyboard and os.name == "posix":
                termios.tcsetattr(sys.stdin, termios.TCSADRAIN, old_settings)

        return key 
開發者ID:mysql,項目名稱:mysql-utilities,代碼行數:40,代碼來源:failover_console.py

示例11: cbreak

# 需要導入模塊: import tty [as 別名]
# 或者: from tty import setcbreak [as 別名]
def cbreak(self):
        """Return a context manager that enters 'cbreak' mode: disabling line
        buffering of keyboard input, making characters typed by the user
        immediately available to the program.  Also referred to as 'rare'
        mode, this is the opposite of 'cooked' mode, the default for most
        shells.

        In 'cbreak' mode, echo of input is also disabled: the application must
        explicitly print any input received, if they so wish.

        More information can be found in the manual page for curses.h,
        http://www.openbsd.org/cgi-bin/man.cgi?query=cbreak

        The python manual for curses,
        http://docs.python.org/2/library/curses.html

        Note also that setcbreak sets VMIN = 1 and VTIME = 0,
        http://www.unixwiz.net/techtips/termios-vmin-vtime.html
        """
        if self.keyboard_fd is not None:
            # save current terminal mode,
            save_mode = termios.tcgetattr(self.keyboard_fd)
            tty.setcbreak(self.keyboard_fd, termios.TCSANOW)
            try:
                yield
            finally:
                # restore prior mode,
                termios.tcsetattr(self.keyboard_fd,
                                  termios.TCSAFLUSH,
                                  save_mode)
        else:
            yield 
開發者ID:xtiankisutsa,項目名稱:MARA_Framework,代碼行數:34,代碼來源:terminal.py

示例12: test_cbreak_no_kb

# 需要導入模塊: import tty [as 別名]
# 或者: from tty import setcbreak [as 別名]
def test_cbreak_no_kb():
    "cbreak() should not call tty.setcbreak() without keyboard."
    @as_subprocess
    def child():
        with tempfile.NamedTemporaryFile() as stream:
            term = TestTerminal(stream=stream)
            with mock.patch("tty.setcbreak") as mock_setcbreak:
                with term.cbreak():
                    assert not mock_setcbreak.called
    child() 
開發者ID:xtiankisutsa,項目名稱:MARA_Framework,代碼行數:12,代碼來源:test_keyboard.py

示例13: posix_shell

# 需要導入模塊: import tty [as 別名]
# 或者: from tty import setcbreak [as 別名]
def posix_shell(chan):
    import select
    
    oldtty = termios.tcgetattr(sys.stdin)
    try:
        tty.setraw(sys.stdin.fileno())
        tty.setcbreak(sys.stdin.fileno())
        chan.settimeout(0.0)

        while True:
            r, w, e = select.select([chan, sys.stdin], [], [])
            if chan in r:
                try:
                    x = chan.recv(1024)
                    if len(x) == 0:
                        print('\r\n*** EOF\r\n', end=' ')
                        break
                    sys.stdout.write(x)
                    sys.stdout.flush()
                except socket.timeout:
                    pass
            if sys.stdin in r:
                x = sys.stdin.read(1)
                if len(x) == 0:
                    break
                chan.send(x)

    finally:
        termios.tcsetattr(sys.stdin, termios.TCSADRAIN, oldtty)

    
# thanks to Mike Looijmans for this code 
開發者ID:VirtueSecurity,項目名稱:aws-extender,代碼行數:34,代碼來源:interactive.py

示例14: start

# 需要導入模塊: import tty [as 別名]
# 或者: from tty import setcbreak [as 別名]
def start(self, alternate_buffer=True):
        """
        Initialize the screen and input mode.

        alternate_buffer -- use alternate screen buffer
        """
        assert not self._started
        if alternate_buffer:
            self._term_output_file.write(escape.SWITCH_TO_ALTERNATE_BUFFER)
            self._rows_used = None
        else:
            self._rows_used = 0

        fd = self._term_input_file.fileno()
        if os.isatty(fd):
            self._old_termios_settings = termios.tcgetattr(fd)
            tty.setcbreak(fd)

        self.signal_init()
        self._alternate_buffer = alternate_buffer
        self._input_iter = self._run_input_iter()
        self._next_timeout = self.max_wait

        if not self._signal_keys_set:
            self._old_signal_keys = self.tty_signal_keys(fileno=fd)

        super(Screen, self).start()

        signals.emit_signal(self, INPUT_DESCRIPTORS_CHANGED)
        # restore mouse tracking to previous state
        self._mouse_tracking(self._mouse_tracking_enabled) 
開發者ID:AnyMesh,項目名稱:anyMesh-Python,代碼行數:33,代碼來源:raw_display.py

示例15: posix_shell

# 需要導入模塊: import tty [as 別名]
# 或者: from tty import setcbreak [as 別名]
def posix_shell(chan):
    import termios
    import tty

    oldtty = termios.tcgetattr(sys.stdin)
    try:
        tty.setraw(sys.stdin.fileno())
        tty.setcbreak(sys.stdin.fileno())
        chan.settimeout(0.0)

        while True:
            r, w, e = select.select([chan, sys.stdin], [], [])
            if chan in r:
                try:
                    x = unicode(chan.recv(1024))
                    if len(x) == 0:
                        break
                    sys.stdout.write(x)
                    sys.stdout.flush()
                except socket.timeout:
                    pass

            if sys.stdin in r:
                x = sys.stdin.read(1)
                if len(x) == 0:
                    break
                chan.send(x)
    finally:
        termios.tcsetattr(sys.stdin, termios.TCSADRAIN, oldtty)
        return 
開發者ID:dark-lbp,項目名稱:isf,代碼行數:32,代碼來源:__init__.py


注:本文中的tty.setcbreak方法示例由純淨天空整理自Github/MSDocs等開源代碼及文檔管理平台,相關代碼片段篩選自各路編程大神貢獻的開源項目,源碼版權歸原作者所有,傳播和使用請參考對應項目的License;未經允許,請勿轉載。