1111import subprocess
1212import sys
1313import threading
14+
1415import unicodedata
1516from enum import Enum
16- from typing import Any , Callable , Dict , Iterable , List , Optional , TextIO , Union
17+ from typing import Any , Callable , Dict , IO , Iterable , List , Optional , TextIO , Type , Union
1718
1819from . import constants
1920
@@ -470,10 +471,15 @@ def getbytes(self) -> bytes:
470471 """Get the internal contents as bytes"""
471472 return bytes (self .buffer .byte_buf )
472473
473- def read (self ) -> str :
474+ def read (self , size : Optional [ int ] = - 1 ) -> str :
474475 """Read from the internal contents as a str and then clear them out"""
475- result = self .getvalue ()
476- self .clear ()
476+ if size is None or size == - 1 :
477+ result = self .getvalue ()
478+ self .clear ()
479+ else :
480+ result = self .buffer .byte_buf [:size ].decode (encoding = self .encoding , errors = self .errors )
481+ self .buffer .byte_buf = self .buffer .byte_buf [size :]
482+
477483 return result
478484
479485 def readbytes (self ) -> bytes :
@@ -668,7 +674,7 @@ def __exit__(self, *args) -> None:
668674
669675class RedirectionSavedState :
670676 """Created by each command to store information required to restore state after redirection"""
671- def __init__ (self , self_stdout : Union [StdSim , TextIO ] , sys_stdout : Union [StdSim , TextIO ],
677+ def __init__ (self , self_stdout : Union [StdSim , IO [ str ]] , sys_stdout : Union [StdSim , IO [ str ] ],
672678 pipe_proc_reader : Optional [ProcReader ], saved_redirecting : bool ) -> None :
673679 """
674680 RedirectionSavedState initializer
@@ -1025,11 +1031,12 @@ def categorize(func: Union[Callable, Iterable[Callable]], category: str) -> None
10251031
10261032 :Example:
10271033
1034+ >>> import cmd2
10281035 >>> class MyApp(cmd2.Cmd):
10291036 >>> def do_echo(self, arglist):
10301037 >>> self.poutput(' '.join(arglist)
10311038 >>>
1032- >>> utils.categorize(do_echo, "Text Processing")
1039+ >>> cmd2. utils.categorize(do_echo, "Text Processing")
10331040
10341041 For an alternative approach to categorizing commands using a decorator, see
10351042 :func:`~cmd2.decorators.with_category`
@@ -1044,7 +1051,7 @@ def categorize(func: Union[Callable, Iterable[Callable]], category: str) -> None
10441051 setattr (func , constants .CMD_ATTR_HELP_CATEGORY , category )
10451052
10461053
1047- def get_defining_class (meth ):
1054+ def get_defining_class (meth ) -> Type :
10481055 """
10491056 Attempts to resolve the class that defined a method.
10501057
0 commit comments