Exemplo n.º 1
0
def print_patch(patch, lines_to_print, file_lines=None):
    if file_lines is None:
        file_lines = list(open(patch.path))

    size_of_old = patch.end_line_number - patch.start_line_number
    size_of_new = len(patch.new_lines) if patch.new_lines else 0
    size_of_diff = size_of_old + size_of_new
    size_of_context = max(0, lines_to_print - size_of_diff)
    size_of_up_context = int(size_of_context / 2)
    size_of_down_context = int(ceil(size_of_context / 2))
    start_context_line_number = patch.start_line_number - size_of_up_context
    end_context_line_number = patch.end_line_number + size_of_down_context

    def print_file_line(line_number):  # noqa
        # Why line_number is passed here?
        print('  %s' % file_lines[i], end='') if (
            0 <= i < len(file_lines)) else '~\n',

    for i in range(start_context_line_number, patch.start_line_number):
        print_file_line(i)
    for i in range(patch.start_line_number, patch.end_line_number):
        if patch.new_lines is not None:
            terminal.terminal_print('- %s' % file_lines[i], color='RED')
        else:
            terminal.terminal_print('* %s' % file_lines[i], color='YELLOW')
    if patch.new_lines is not None:
        for line in patch.new_lines:
            terminal.terminal_print('+ %s' % line, color='GREEN')
    for i in range(patch.end_line_number, end_context_line_number):
        print_file_line(i)
Exemplo n.º 2
0
def print_patch(patch, lines_to_print, file_lines=None):
    if file_lines is None:
        file_lines = list(open(patch.path))

    size_of_old = patch.end_line_number - patch.start_line_number
    size_of_new = len(patch.new_lines) if patch.new_lines else 0
    size_of_diff = size_of_old + size_of_new
    size_of_context = max(0, lines_to_print - size_of_diff)
    size_of_up_context = int(size_of_context / 2)
    size_of_down_context = int(ceil(size_of_context / 2))
    start_context_line_number = patch.start_line_number - size_of_up_context
    end_context_line_number = patch.end_line_number + size_of_down_context

    def print_file_line(line_number):  # noqa
        # Why line_number is passed here?
        print('  %s' %
              file_lines[i], end='') if (0 <= i < len(file_lines)) else '~\n',

    for i in range(start_context_line_number, patch.start_line_number):
        print_file_line(i)
    for i in range(patch.start_line_number, patch.end_line_number):
        if patch.new_lines is not None:
            terminal.terminal_print('- %s' % file_lines[i], color='RED')
        else:
            terminal.terminal_print('* %s' % file_lines[i], color='YELLOW')
    if patch.new_lines is not None:
        for line in patch.new_lines:
            terminal.terminal_print('+ %s' % line, color='GREEN')
    for i in range(patch.end_line_number, end_context_line_number):
        print_file_line(i)
Exemplo n.º 3
0
def _ask_about_patch(patch, editor, default_no):
    global yes_to_all

    default_action = 'n' if default_no else 'y'
    terminal.terminal_clear()
    terminal.terminal_print('%s\n' % patch.render_range(), color='WHITE')
    print()

    lines = list(open(patch.path))
    size = list(terminal.terminal_get_size())
    print_patch(patch, size[0] - 20, lines)

    print()

    if patch.new_lines is not None:
        if not yes_to_all:
            if default_no:
                print('Accept change (y = yes, n = no [default], e = edit, ' +
                      'A = yes to all, E = yes+edit, q = quit)? '),
            else:
                print('Accept change (y = yes [default], n = no, e = edit, ' +
                      'A = yes to all, E = yes+edit, q = quit)? '),
            p = _prompt('yneEAq', default=default_action)
        else:
            p = 'y'
    else:
        print('(e = edit [default], n = skip line, q = quit)? ', end=" ")
        p = _prompt('enq', default='e')

    if p in 'A':
        yes_to_all = True
        p = 'y'
    if p in 'yE':
        patch.apply_to(lines)
        _save(patch.path, lines)
    if p in 'eE':
        run_editor(patch.start_position, editor)
    if p in 'q':
        sys.exit(0)
Exemplo n.º 4
0
def _ask_about_patch(patch, editor, default_no):
    global yes_to_all

    default_action = 'n' if default_no else 'y'
    terminal.terminal_clear()
    terminal.terminal_print('%s\n' % patch.render_range(), color='WHITE')
    print()

    lines = list(open(patch.path))
    size = list(terminal.terminal_get_size())
    print_patch(patch, size[0] - 20, lines)

    print()

    if patch.new_lines is not None:
        if not yes_to_all:
            if default_no:
                print('Accept change (y = yes, n = no [default], e = edit, ' +
                      'A = yes to all, E = yes+edit, q = quit)? '),
            else:
                print('Accept change (y = yes [default], n = no, e = edit, ' +
                      'A = yes to all, E = yes+edit, q = quit)? '),
            p = _prompt('yneEAq', default=default_action)
        else:
            p = 'y'
    else:
        print('(e = edit [default], n = skip line, q = quit)? ', end=" ")
        p = _prompt('enq', default='e')

    if p in 'A':
        yes_to_all = True
        p = 'y'
    if p in 'yE':
        patch.apply_to(lines)
        _save(patch.path, lines)
    if p in 'eE':
        run_editor(patch.start_position, editor)
    if p in 'q':
        sys.exit(0)