@ -55,25 +55,17 @@ class PrintUI(object):
self . _stderr = codecs . getwriter ( self . encoding ) ( _get_raw_stderr ( ) ,
errors = ' replace ' )
def print_out ( self , * strings , * * kwargs ) :
""" Like print, but rather than raising an error when a character
is not in the terminal ' s encoding ' s character set , just silently
replaces it .
"""
print ( ' ' . join ( strings ) , file = self . _stdout , * * kwargs )
def print_err ( self , * strings , * * kwargs ) :
""" Like print, but rather than raising an error when a character
is not in the terminal ' s encoding ' s character set , just silently
replaces it .
"""
print ( ' ' . join ( strings ) , file = self . _stderr , * * kwargs )
def message ( self , * messages , * * kwargs ) :
kwargs [ ' file ' ] = self . _stdout
print ( * messages , * * kwargs )
def warning ( self , message ) :
self . print_err ( " %s : %s " % ( color . dye_err ( ' warning ' , ' yellow ' ) , message ) )
def warning ( self , message , * * kwargs ) :
kwargs [ ' file ' ] = self . _stderr
print ( ' {} : {} ' . format ( color . dye_err ( ' warning ' , ' yellow ' ) , message ) , * * kwargs )
def error ( self , message ) :
self . print_err ( ' {} : {} ' . format ( color . dye_err ( ' error ' , ' red ' ) , message ) )
def error ( self , message , * * kwargs ) :
kwargs [ ' file ' ] = self . _stderr
print ( ' {} : {} ' . format ( color . dye_err ( ' error ' , ' red ' ) , message ) , * * kwargs )
def exit ( self , error_code = 1 ) :
sys . exit ( error_code )
@ -119,7 +111,7 @@ class InputUI(PrintUI):
option_str = ' / ' . join ( [ " {} {} " . format ( color . dye_out ( c , ' bold ' ) , s [ 1 : ] )
for c , s in zip ( displayed_chars , options ) ] )
self . print_out ( ' {} {} : ' . format ( question , option_str ) , end = ' ' )
self . message ( ' {} {} : ' . format ( question , option_str ) , end = ' ' )
while True :
answer = self . input ( )
if answer is None or answer == ' ' :
@ -133,7 +125,7 @@ class InputUI(PrintUI):
return option_chars . index ( answer . lower ( ) )
except ValueError :
pass
self . print_out ( ' Incorrect option. ' , option_str )
self . message ( ' Incorrect option. ' , option_str )
def input_choice ( self , options , option_chars , default = None , question = ' ' ) :
@ -155,7 +147,7 @@ class InputUI(PrintUI):
for i , s in enumerate ( option_chars ) ]
option_str = ' , ' . join ( [ " [ %s ] %s " % ( color . dye_out ( c , ' cyan ' ) , o )
for c , o in zip ( displayed_chars , options ) ] )
self . print_out ( question , option_str )
self . message ( question , option_str )
while True :
answer = self . input ( )
if answer is None or answer == ' ' :
@ -166,7 +158,7 @@ class InputUI(PrintUI):
return option_chars . index ( answer . lower ( ) )
except ValueError :
pass
self . print_out ( ' Incorrect option. ' , option_str )
self . message ( ' Incorrect option. ' , option_str )
def input_yn ( self , question = ' ' , default = ' y ' ) :
d = 0 if default in ( True , ' y ' , ' yes ' ) else 1