mirror of
https://github.com/mnaberez/py65.git
synced 2024-06-15 04:29:30 +00:00
Moved unbuffered stdin code to console.py
This commit is contained in:
parent
0c78f33b43
commit
99b7f66070
|
@ -54,31 +54,13 @@ class Monitor(cmd.Cmd):
|
||||||
self.prompt = "."
|
self.prompt = "."
|
||||||
self._add_shortcuts()
|
self._add_shortcuts()
|
||||||
|
|
||||||
# Attempt to get a copy of stdin that is unbuffered.
|
# Attempt to get a copy of stdin that is unbuffered on systems
|
||||||
# This allows for immediate response to typed input as well as
|
# that support it. This allows for immediate response to
|
||||||
# pasted input. If unable to get an unbuffered version of
|
# typed input as well as pasted input. If unable to get an
|
||||||
# stdin, use the original version.
|
# unbuffered version of stdin, the original version is returned.
|
||||||
if stdin != None:
|
stdin = console.get_unbuffered_stdin(stdin)
|
||||||
try:
|
|
||||||
# Reopen stdin with no buffer.
|
|
||||||
self.unbuffered_stdin = os.fdopen(stdin.fileno(), 'rb', 0)
|
|
||||||
except Exception as e:
|
|
||||||
print(e)
|
|
||||||
# Unable to reopen this file handle with no buffer.
|
|
||||||
# Just use the original file handle.
|
|
||||||
print("Error opening unbuffered stdin - using buffered version")
|
|
||||||
self.unbuffered_stdin = stdin
|
|
||||||
else:
|
|
||||||
# If stdin is None, try using sys.stdin for input.
|
|
||||||
try:
|
|
||||||
# Reopen the system's stdin with no buffer.
|
|
||||||
self.unbuffered_stdin = os.fdopen(sys.stdin.fileno(), 'rb', 0)
|
|
||||||
except Exception as e:
|
|
||||||
print(e)
|
|
||||||
print("Error opening default unbuffered stdin - using buffered version")
|
|
||||||
self.unbuffered_stdin = None
|
|
||||||
|
|
||||||
cmd.Cmd.__init__(self, stdin=self.unbuffered_stdin, stdout=stdout)
|
cmd.Cmd.__init__(self, stdin=stdin, stdout=stdout)
|
||||||
|
|
||||||
# Save the current input mode so it can be restored after
|
# Save the current input mode so it can be restored after
|
||||||
# after processing commands and before exiting.
|
# after processing commands and before exiting.
|
||||||
|
|
|
@ -3,6 +3,10 @@ import sys
|
||||||
if sys.platform[:3] == "win":
|
if sys.platform[:3] == "win":
|
||||||
import msvcrt
|
import msvcrt
|
||||||
|
|
||||||
|
def get_unbuffered_stdin(stdin):
|
||||||
|
""" get_unbuffered_stdin returns the given stdin on Windows. """
|
||||||
|
return stdin
|
||||||
|
|
||||||
def save_mode(stdin):
|
def save_mode(stdin):
|
||||||
""" save_mode is a no-op on Windows. """
|
""" save_mode is a no-op on Windows. """
|
||||||
return
|
return
|
||||||
|
@ -37,10 +41,37 @@ if sys.platform[:3] == "win":
|
||||||
|
|
||||||
else:
|
else:
|
||||||
import termios
|
import termios
|
||||||
|
import os
|
||||||
from select import select
|
from select import select
|
||||||
|
|
||||||
oldattr = None
|
oldattr = None
|
||||||
|
|
||||||
|
def get_unbuffered_stdin(stdin):
|
||||||
|
""" Attempt to get and return a copy of stdin that is
|
||||||
|
unbuffered. This allows for immediate response to typed input
|
||||||
|
as well as pasted input. If unable to get an unbuffered
|
||||||
|
version of stdin, return the original version.
|
||||||
|
"""
|
||||||
|
if stdin != None:
|
||||||
|
try:
|
||||||
|
# Reopen stdin with no buffer.
|
||||||
|
return os.fdopen(stdin.fileno(), 'rb', 0)
|
||||||
|
except Exception as e:
|
||||||
|
print(e)
|
||||||
|
# Unable to reopen this file handle with no buffer.
|
||||||
|
# Just use the original file handle.
|
||||||
|
print("Error opening unbuffered stdin - using buffered version")
|
||||||
|
return stdin
|
||||||
|
else:
|
||||||
|
# If stdin is None, try using sys.stdin for input.
|
||||||
|
try:
|
||||||
|
# Reopen the system's stdin with no buffer.
|
||||||
|
return os.fdopen(sys.stdin.fileno(), 'rb', 0)
|
||||||
|
except Exception as e:
|
||||||
|
print(e)
|
||||||
|
print("Error opening default unbuffered stdin - using buffered version")
|
||||||
|
return None
|
||||||
|
|
||||||
def save_mode(stdin):
|
def save_mode(stdin):
|
||||||
""" For operating systems that support it, save the original
|
""" For operating systems that support it, save the original
|
||||||
input termios settings so they can be restored later. This
|
input termios settings so they can be restored later. This
|
||||||
|
|
Loading…
Reference in New Issue
Block a user