Skip to content

Commit 10dd79e

Browse files
committed
Made async stuff public
1 parent 5bce3d0 commit 10dd79e

File tree

3 files changed

+30
-30
lines changed

3 files changed

+30
-30
lines changed

cmd2/cmd2.py

Lines changed: 20 additions & 20 deletions
Original file line numberDiff line numberDiff line change
@@ -530,7 +530,7 @@ def __init__(self, completekey: str='tab', stdin=None, stdout=None, persistent_h
530530
# This lock should be acquired before doing any asynchronous changes to the terminal to
531531
# ensure the updates to the terminal don't interfere with the input being typed. It can be
532532
# acquired any time there is a readline prompt on screen.
533-
self._terminal_lock = threading.RLock()
533+
self.terminal_lock = threading.RLock()
534534

535535
# ----- Methods related to presenting output to the user -----
536536

@@ -2085,10 +2085,10 @@ def pseudo_raw_input(self, prompt: str) -> str:
20852085
if self.use_rawinput:
20862086
try:
20872087
if sys.stdin.isatty():
2088-
# Wrap in try since _terminal_lock may not be locked when this function is called from unit tests
2088+
# Wrap in try since terminal_lock may not be locked when this function is called from unit tests
20892089
try:
20902090
# A prompt is about to be drawn. Allow asynchronous changes to the terminal.
2091-
self._terminal_lock.release()
2091+
self.terminal_lock.release()
20922092
except RuntimeError:
20932093
pass
20942094

@@ -2104,7 +2104,7 @@ def pseudo_raw_input(self, prompt: str) -> str:
21042104
finally:
21052105
if sys.stdin.isatty():
21062106
# The prompt is gone. Do not allow asynchronous changes to the terminal.
2107-
self._terminal_lock.acquire()
2107+
self.terminal_lock.acquire()
21082108
else:
21092109
if self.stdin.isatty():
21102110
# on a tty, print the prompt first, then read the line
@@ -3237,25 +3237,25 @@ def _clear_input_lines_str(self) -> str:
32373237

32383238
return terminal_str
32393239

3240-
def _async_alert(self, alert_msg: str, new_prompt: Optional[str] = None) -> None:
3240+
def async_alert(self, alert_msg: str, new_prompt: Optional[str] = None) -> None:
32413241
"""
32423242
Used to display an important message to the user while they are at the prompt in between commands.
32433243
To the user it appears as if an alert message is printed above the prompt and their current input
32443244
text and cursor location is left alone.
32453245
3246-
IMPORTANT: Do not call this unless you have acquired self._terminal_lock
3246+
IMPORTANT: Do not call this unless you have acquired self.terminal_lock
32473247
first, which ensures a prompt is onscreen
32483248
32493249
:param alert_msg: the message to display to the user
32503250
:param new_prompt: if you also want to change the prompt that is displayed, then include it here
32513251
see async_update_prompt() docstring for guidance on updating a prompt
3252-
:raises RuntimeError if called while another thread holds _terminal_lock
3252+
:raises RuntimeError if called while another thread holds terminal_lock
32533253
"""
32543254
if not (vt100_support and self.use_rawinput):
32553255
return
32563256

3257-
# Sanity check that can't fail if self._terminal_lock was acquired before calling this function
3258-
if self._terminal_lock.acquire(blocking=False):
3257+
# Sanity check that can't fail if self.terminal_lock was acquired before calling this function
3258+
if self.terminal_lock.acquire(blocking=False):
32593259

32603260
# Generate a string to clear the prompt and input lines and replace with the alert
32613261
terminal_str = self._clear_input_lines_str()
@@ -3275,30 +3275,30 @@ def _async_alert(self, alert_msg: str, new_prompt: Optional[str] = None) -> None
32753275
# Redraw the prompt and input lines
32763276
rl_force_redisplay()
32773277

3278-
self._terminal_lock.release()
3278+
self.terminal_lock.release()
32793279

32803280
else:
3281-
raise RuntimeError("another thread holds _terminal_lock")
3281+
raise RuntimeError("another thread holds terminal_lock")
32823282

3283-
def _async_update_prompt(self, new_prompt: str) -> None:
3283+
def async_update_prompt(self, new_prompt: str) -> None:
32843284
"""
32853285
Updates the prompt while the user is still typing at it. This is good for alerting the user to system
32863286
changes dynamically in between commands. For instance you could alter the color of the prompt to indicate
32873287
a system status or increase a counter to report an event. If you do alter the actual text of the prompt,
32883288
it is best to keep the prompt the same width as what's on screen. Otherwise the user's input text will
32893289
be shifted and the update will not be seamless.
32903290
3291-
IMPORTANT: Do not call this unless you have acquired self._terminal_lock
3291+
IMPORTANT: Do not call this unless you have acquired self.terminal_lock
32923292
first, which ensures a prompt is onscreen
32933293
32943294
:param new_prompt: what to change the prompt to
3295-
:raises RuntimeError if called while another thread holds _terminal_lock
3295+
:raises RuntimeError if called while another thread holds terminal_lock
32963296
"""
32973297
if not (vt100_support and self.use_rawinput):
32983298
return
32993299

3300-
# Sanity check that can't fail if self._terminal_lock was acquired before calling this function
3301-
if self._terminal_lock.acquire(blocking=False):
3300+
# Sanity check that can't fail if self.terminal_lock was acquired before calling this function
3301+
if self.terminal_lock.acquire(blocking=False):
33023302

33033303
# Generate a string to clear the prompt and input lines
33043304
terminal_str = self._clear_input_lines_str()
@@ -3316,10 +3316,10 @@ def _async_update_prompt(self, new_prompt: str) -> None:
33163316
# Redraw the prompt and input lines
33173317
rl_force_redisplay()
33183318

3319-
self._terminal_lock.release()
3319+
self.terminal_lock.release()
33203320

33213321
else:
3322-
raise RuntimeError("another thread holds _terminal_lock")
3322+
raise RuntimeError("another thread holds terminal_lock")
33233323

33243324
@staticmethod
33253325
def set_window_title(title: str) -> None:
@@ -3368,7 +3368,7 @@ def cmdloop(self, intro: Optional[str]=None) -> None:
33683368
signal.signal(signal.SIGINT, self.sigint_handler)
33693369

33703370
# Grab terminal lock before the prompt has been drawn by readline
3371-
self._terminal_lock.acquire()
3371+
self.terminal_lock.acquire()
33723372

33733373
# Always run the preloop first
33743374
for func in self._preloop_hooks:
@@ -3397,7 +3397,7 @@ def cmdloop(self, intro: Optional[str]=None) -> None:
33973397

33983398
# Release terminal lock now that postloop code should have stopped any terminal updater threads
33993399
# This will also zero the lock count in case cmdloop() is called again
3400-
self._terminal_lock.release()
3400+
self.terminal_lock.release()
34013401

34023402
# Restore the original signal handler
34033403
signal.signal(signal.SIGINT, original_sigint_handler)

docs/unfreefeatures.rst

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -198,12 +198,12 @@ the command line. This means the feedback is provided to the user when they are
198198
the prompt. To use this functionality, the application must be running in any terminal that supports
199199
VT100 control characters and readline. Linux, Mac, and Windows 10 and greater all support these.
200200

201-
_async_alert()
201+
async_alert()
202202
Used to display an important message to the user while they are at the prompt in between commands.
203203
To the user it appears as if an alert message is printed above the prompt and their current input
204204
text and cursor location is left alone.
205205

206-
_async_update_prompt()
206+
async_update_prompt()
207207
Updates the prompt while the user is still typing at it. This is good for alerting the user to system
208208
changes dynamically in between commands. For instance you could alter the color of the prompt to indicate
209209
a system status or increase a counter to report an event.

examples/async_printing.py

Lines changed: 8 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -45,7 +45,7 @@ def __init__(self, *args, **kwargs) -> None:
4545

4646
def _preloop_hook(self) -> None:
4747
""" Start the alerter thread """
48-
# This runs after cmdloop() acquires self._terminal_lock, which will be locked until the prompt appears.
48+
# This runs after cmdloop() acquires self.terminal_lock, which will be locked until the prompt appears.
4949
# Therefore this is the best place to start the alerter thread since there is no risk of it alerting
5050
# before the prompt is displayed. You can also start it via a command if its not something that should
5151
# be running during the entire application. See do_start_alerts().
@@ -57,7 +57,7 @@ def _preloop_hook(self) -> None:
5757
def _postloop_hook(self) -> None:
5858
""" Stops the alerter thread """
5959

60-
# After this function returns, cmdloop() releases self._terminal_lock which could make the alerter
60+
# After this function returns, cmdloop() releases self.terminal_lock which could make the alerter
6161
# thread think the prompt is on screen. Therefore this is the best place to stop the alerter thread.
6262
# You can also stop it via a command. See do_stop_alerts().
6363
self._stop_thread = True
@@ -166,9 +166,9 @@ def _alerter_thread_func(self) -> None:
166166
self._next_alert_time = 0
167167

168168
while not self._stop_thread:
169-
# Always acquire _terminal_lock before printing alerts or updating the prompt
169+
# Always acquire terminal_lock before printing alerts or updating the prompt
170170
# To keep the app responsive, do not block on this call
171-
if self._terminal_lock.acquire(blocking=False):
171+
if self.terminal_lock.acquire(blocking=False):
172172

173173
# Get any alerts that need to be printed
174174
alert_str = self._generate_alert_str()
@@ -178,15 +178,15 @@ def _alerter_thread_func(self) -> None:
178178

179179
# Check if we have alerts to print
180180
if alert_str:
181-
# new_prompt is an optional parameter to _async_alert()
182-
self._async_alert(alert_str, new_prompt)
181+
# new_prompt is an optional parameter to async_alert()
182+
self.async_alert(alert_str, new_prompt)
183183

184184
# No alerts needed to be printed, check if the prompt changed
185185
elif new_prompt != self.prompt:
186-
self._async_update_prompt(new_prompt)
186+
self.async_update_prompt(new_prompt)
187187

188188
# Don't forget to release the lock
189-
self._terminal_lock.release()
189+
self.terminal_lock.release()
190190

191191
time.sleep(0.5)
192192

0 commit comments

Comments
 (0)