def print_multi_line(content, force_single_line, sort_key):
    """
    'sort_key' 参数只在 dict 模式时有效
    'sort_key' parameter only available in 'dict' mode
    """

    global last_output_lines
    global overflow_flag
    global is_atty

    if not is_atty:
        if isinstance(content, list):
            for line in content:
                print(line)
        elif isinstance(content, dict):
            for k, v in sorted(content.items(), key=sort_key):
                print("{}: {}".format(k, v))
        else:
            raise TypeError("Excepting types: list, dict. Got: {}".format(type(content)))
        return

    columns, rows = get_terminal_size()
    lines = lines_of_content(content, columns)
    if force_single_line is False and lines > rows:
        overflow_flag = True
    elif force_single_line is True and len(content) > rows:
        overflow_flag = True

    # 确保初始输出位置是位于最左处的
    # to make sure the cursor is at the left most
    print("\b" * columns, end="")

    if isinstance(content, list):
        for line in content:
            _line = preprocess(line)
            print_line(_line, columns, force_single_line)
    elif isinstance(content, dict):
        for k, v in sorted(content.items(), key=sort_key):
            _k, _v = map(preprocess, (k, v))
            print_line("{}: {}".format(_k, _v), columns, force_single_line)
    else:
        raise TypeError("Excepting types: list, dict. Got: {}".format(type(content)))

    # 输出额外的空行来清除上一次输出的剩余内容
    # do extra blank lines to wipe the remaining of last output
    print(" " * columns * (last_output_lines - lines), end="")

    # 回到初始输出位置
    # back to the origin pos
    print(magic_char * (max(last_output_lines, lines)-1), end="")
    sys.stdout.flush()
    last_output_lines = lines
def print_multi_line ( content , force_single_line , sort_key ) : global last_output_lines global overflow_flag global is_atty if not is_atty : if isinstance ( content , list ) : for line in content : print ( line ) elif isinstance ( content , dict ) : for k , v in sorted ( content . items ( ) , key = sort_key ) : print ( "{}: {}" . format ( k , v ) ) else : raise TypeError ( "Excepting types: list, dict. Got: {}" . format ( type ( content ) ) ) return columns , rows = get_terminal_size ( ) lines = lines_of_content ( content , columns ) if force_single_line is False and lines > rows : overflow_flag = True elif force_single_line is True and len ( content ) > rows : overflow_flag = True print ( "\b" * columns , end = "" ) if isinstance ( content , list ) : for line in content : _line = preprocess ( line ) print_line ( _line , columns , force_single_line ) elif isinstance ( content , dict ) : for k , v in sorted ( content . items ( ) , key = sort_key ) : _k , _v = map ( preprocess , ( k , v ) ) print_line ( "{}: {}" . format ( _k , _v ) , columns , force_single_line ) else : raise TypeError ( "Excepting types: list, dict. Got: {}" . format ( type ( content ) ) ) print ( " " * columns * ( last_output_lines - lines ) , end = "" ) print ( magic_char * ( max ( last_output_lines , lines ) - 1 ) , end = "" ) sys . stdout . flush ( ) last_output_lines = lines
'sort_key' 参数只在 dict 模式时有效
    'sort_key' parameter only available in 'dict' mode