예제 #1
0
                    remove_escape_sequences(state.prompt) +
                    state.before_cursor + state.after_cursor)
                if to_erase > 0:
                    stdout.write(color.Fore.DEFAULT + color.Back.DEFAULT +
                                 ' ' * to_erase)
                    cursor_backward(to_erase)

                # Move cursor to the correct position
                set_cursor_attributes(50 if state.overwrite else 10, True)
                cursor_backward(len(state.after_cursor))

            # Prepare new input state
            state.step_line()

            # Read and process a keyboard event
            rec = read_input()
            select = auto_select or is_shift_pressed(rec)

            # Will be overriden if Shift-PgUp/Dn is pressed
            force_repaint = not is_control_only(rec)

            #print '\n\n', rec.KeyDown, rec.Char, rec.VirtualKeyCode, rec.ControlKeyState, '\n\n'
            if is_ctrl_pressed(
                    rec) and not is_alt_pressed(rec):  # Ctrl-Something
                if rec.Char == chr(4):  # Ctrl-D
                    if state.before_cursor + state.after_cursor == '':
                        internal_exit('\r\nBye!')
                    else:
                        state.handle(ActionCode.ACTION_DELETE)
                elif rec.Char == chr(31):  # Ctrl-_
                    state.handle(ActionCode.ACTION_UNDO_EMACS)
예제 #2
0
def list_and_switch():
    winstate_full_path = os.path.join(pycmd_data_dir, windows_state_path)
    with open(winstate_full_path, 'r') as f:
        winstate = f.readlines()
    winstate.reverse()

    first_line = True
    index = 0
    orig_index = -1
    index_map = []
    remove_hwnd_list = []
    columns = console.get_buffer_size()[0] - 3
    currHwnd = py_GetConsoleWindow()

    for line in winstate:
        orig_index += 1
        states = line.split(winstate_separator)
        if len(states) != 3:
            print("Warning: unsupported line for windows switch: ", line)
            continue

        hwnd  = int(states[0])
        if hwnd == currHwnd:
            continue
        if not py_IsWindow(hwnd):
            remove_hwnd_list.append(hwnd)
            continue

        curr_index_char = chr(ord('a') + index)
        index += 1
        index_map.append(orig_index)
        pwd = states[1].strip() + '> '
        cmd = states[2].strip()

        if len(pwd) > columns:
            pwd = pwd[0: column - 5] + '...> '
            cmd = ''
        else:
            left_columns = columns - len(pwd)
            if len(cmd) > left_columns:
                if left_columns >= 3:
                    cmd = cmd[0:left_columns - 3] + '...'

        if first_line:
            sys.stdout.write('\n\n')
            first_line = False

        if index % 2 == 0:
            color_str_cmd = color.Fore.RED + color.Fore.CLEAR_BRIGHT
            color_str_pwd = color.Fore.RED + color.Fore.SET_BRIGHT
        else:
            color_str_cmd = color.Fore.GREEN + color.Fore.CLEAR_BRIGHT
            color_str_pwd = color.Fore.GREEN + color.Fore.SET_BRIGHT
        sys.stdout.write(color_str_pwd + curr_index_char + ': ' + pwd + color_str_cmd + cmd + '\n')

    if index == 0:
        return
    sys.stdout.write(color.Fore.DEFAULT + '\n')
    message = ' Press a-z to switch to target PyCmd, space to ignore: '
    sys.stdout.write(message)

    rec = console.read_input()
    select_id = ord(rec.Char) - ord('a')
    #TODO: refresh current line instead of output new line?
    # Why 1 '\n' doesn't work? Know why, because cmd prompt is up for 1 line,
    # which occupies the message line scrolled by 1 line
    #sys.stdout.write('\n\n')
    sys.stdout.write('\r' + ' ' * len(message))
    if 0 <= select_id < index:
        to_line = winstate[index_map[select_id]]
        to_line_list = to_line.split(winstate_separator)
        to_hwnd = int(to_line_list[0])
        PyCmdUtils.SwitchToHwnd(to_hwnd)

        update_window_state(to_line_list[1], to_line_list[2], to_hwnd, remove_hwnd_list)
예제 #3
0
def list_and_switch():
    winstate_full_path = os.path.join(pycmd_data_dir, windows_state_path)
    with open(winstate_full_path, 'r') as f:
        winstate = f.readlines()
    winstate.reverse()

    first_line = True
    index = 0
    orig_index = -1
    index_map = []
    remove_hwnd_list = []
    columns = console.get_buffer_size()[0] - 3
    currHwnd = py_GetConsoleWindow()

    for line in winstate:
        orig_index += 1
        states = line.split(winstate_separator)
        if len(states) != 3:
            print("Warning: unsupported line for windows switch: ", line)
            continue

        hwnd = int(states[0])
        if hwnd == currHwnd:
            continue
        if not py_IsWindow(hwnd):
            remove_hwnd_list.append(hwnd)
            continue

        curr_index_char = chr(ord('a') + index)
        index += 1
        index_map.append(orig_index)
        pwd = states[1].strip() + '> '
        cmd = states[2].strip()

        if len(pwd) > columns:
            pwd = pwd[0:column - 5] + '...> '
            cmd = ''
        else:
            left_columns = columns - len(pwd)
            if len(cmd) > left_columns:
                if left_columns >= 3:
                    cmd = cmd[0:left_columns - 3] + '...'

        if first_line:
            sys.stdout.write('\n\n')
            first_line = False

        if index % 2 == 0:
            color_str_cmd = color.Fore.RED + color.Fore.CLEAR_BRIGHT
            color_str_pwd = color.Fore.RED + color.Fore.SET_BRIGHT
        else:
            color_str_cmd = color.Fore.GREEN + color.Fore.CLEAR_BRIGHT
            color_str_pwd = color.Fore.GREEN + color.Fore.SET_BRIGHT
        sys.stdout.write(color_str_pwd + curr_index_char + ': ' + pwd +
                         color_str_cmd + cmd + '\n')

    if index == 0:
        return
    sys.stdout.write(color.Fore.DEFAULT + '\n')
    message = ' Press a-z to switch to target PyCmd, space to ignore: '
    sys.stdout.write(message)

    rec = console.read_input()
    select_id = ord(rec.Char) - ord('a')
    #TODO: refresh current line instead of output new line?
    # Why 1 '\n' doesn't work? Know why, because cmd prompt is up for 1 line,
    # which occupies the message line scrolled by 1 line
    #sys.stdout.write('\n\n')
    sys.stdout.write('\r' + ' ' * len(message))
    if 0 <= select_id < index:
        to_line = winstate[index_map[select_id]]
        to_line_list = to_line.split(winstate_separator)
        to_hwnd = int(to_line_list[0])
        PyCmdUtils.SwitchToHwnd(to_hwnd)

        update_window_state(to_line_list[1], to_line_list[2], to_hwnd,
                            remove_hwnd_list)
예제 #4
0
파일: PyCmd.py 프로젝트: heyibaiyu/PyCmd
                # Erase remaining chars from old line
                to_erase = prev_total_len - len(remove_escape_sequences(state.prompt) + state.before_cursor + state.after_cursor)
                if to_erase > 0:
                    stdout.write(color.Fore.DEFAULT + color.Back.DEFAULT + ' ' * to_erase)
                    cursor_backward(to_erase)

                # Move cursor to the correct position
                set_cursor_visible(True)
                cursor_backward(len(state.after_cursor))

            # Prepare new input state
            state.step_line()

            # Read and process a keyboard event
            rec = read_input()
            select = auto_select or is_shift_pressed(rec)

            # Will be overriden if Shift-PgUp/Dn is pressed
            force_repaint = not is_control_only(rec)    

            #print '\n\n', rec.keyDown, rec.char, rec.virtualKeyCode, rec.controlKeyState, '\n\n'
            if is_ctrl_pressed(rec) and not is_alt_pressed(rec):  # Ctrl-Something
                if rec.Char == chr(4):                  # Ctrl-D
                    if state.before_cursor + state.after_cursor == '':
                        internal_exit('\r\nBye!')
                    else:
                        state.handle(ActionCode.ACTION_DELETE)
                elif rec.Char == chr(31):                   # Ctrl-_
                    state.handle(ActionCode.ACTION_UNDO_EMACS)
                    auto_select = False
예제 #5
0
    PyCmd = importlib.import_module('PyCmd')
    PyCmd.__dict__['__name__'] = '__main__'
    PyCmd.__dict__['__file__'] = os.path.join(scriptdir, 'PyCmd.py')
    apply_hijacks(PyCmd)
    return PyCmd

if __name__=='__main__':
    PyCmd = bootstrap()
    try:
        PyCmd.init()
        PyCmd.main()
    except Exception, e:
        report_file_name = (PyCmd.pycmd_data_dir
                            + '\\crash-'
                            + time.strftime('%Y%m%d_%H%M%S')
                            + '.log')
        print '\n'
        print '************************************'
        print 'PyCmd has encountered a fatal error!'
        print
        report_file = open(report_file_name, 'w')
        traceback.print_exc(file=report_file)
        report_file.close()
        traceback.print_exc()
        print
        print 'Crash report written to:\n  ' + report_file_name
        print
        print 'Press any key to exit... '
        print '************************************'
        read_input()
예제 #6
0
    # it was run as the main script.
    import PyCmd
    PyCmd.__dict__['__name__'] = '__main__'
    PyCmd.__dict__['__file__'] = os.path.join(scriptdir, 'PyCmd.py')
    apply_hijacks(PyCmd)
    return PyCmd


if __name__ == '__main__':
    PyCmd = bootstrap()
    try:
        PyCmd.init()
        PyCmd.main()
    except Exception, e:
        report_file_name = (PyCmd.pycmd_data_dir + '\\crash-' +
                            time.strftime('%Y%m%d_%H%M%S') + '.log')
        print '\n'
        print '************************************'
        print 'PyCmd has encountered a fatal error!'
        print
        report_file = open(report_file_name, 'w')
        traceback.print_exc(file=report_file)
        report_file.close()
        traceback.print_exc()
        print
        print 'Crash report written to:\n  ' + report_file_name
        print
        print 'Press any key to exit... '
        print '************************************'
        read_input()