Пример #1
0
    def begin_enter_command(self) -> None:
        self.stdscr.erase()
        # first check if they are trying to enter command mode
        # but already have a command...
        if self.flags.get_preset_command():
            self.helper_chrome.output(self.mode)
            (min_x, min_y, _, max_y) = self.get_chrome_boundaries()
            y_start = (max_y + min_y) // 2 - 3
            self.print_provided_command_warning(y_start, min_x)
            self.stdscr.refresh()
            self.get_key()
            self.mode = SELECT_MODE
            self.dirty_all()
            return

        self.mode = COMMAND_MODE
        self.helper_chrome.output(self.mode)
        logger.add_event("enter_command_mode")

        command = self.show_and_get_command()
        if len(command) == 0:
            # go back to selection mode and repaint
            self.mode = SELECT_MODE
            self.curses_api.noecho()
            self.dirty_all()
            logger.add_event("exit_command_mode")
            return
        line_objs = self.get_paths_to_use()
        output.exec_composed_command(command, line_objs)
        sys.exit(0)
Пример #2
0
def edit_files(line_objs: List[LineMatch]) -> None:
    logger.add_event("editing_num_files", len(line_objs))
    files_and_line_numbers = [
        (line_obj.get_path(), line_obj.get_line_num()) for line_obj in line_objs
    ]
    command = join_files_into_command(files_and_line_numbers)
    append_if_invalid(line_objs)
    append_to_file(command)
    append_exit()
Пример #3
0
 def check_resize(self) -> None:
     max_y, max_x = self.get_screen_dimensions()
     if max_y is not self.old_max_y or max_x is not self.old_max_x:
         # we resized so print all!
         self.print_all()
         self.reset_dirty()
         self.update_scroll_offset()
         self.stdscr.refresh()
         logger.add_event("resize")
     self.old_max_y, self.old_max_x = self.get_screen_dimensions()
Пример #4
0
def get_editor_and_path() -> Tuple[str, str]:
    editor_path = (
        os.environ.get("FPP_EDITOR")
        or os.environ.get("VISUAL")
        or os.environ.get("EDITOR")
    )
    if editor_path:
        editor = os.path.basename(editor_path)
        logger.add_event(f"using_editor_{editor}")
        return editor, editor_path
    return "vim", "vim"
Пример #5
0
def exec_composed_command(command: str, line_objs: List[LineMatch]) -> None:
    if not command:
        edit_files(line_objs)
        return

    logger.add_event("command_on_num_files", len(line_objs))
    command = compose_command(command, line_objs)
    append_alias_expansion()
    append_if_invalid(line_objs)
    append_friendly_command(command)
    append_exit()
Пример #6
0
 def __init__(self, printer: ColorPrinter, screen_control: "Controller",
              flags: ScreenFlags):
     self.printer = printer
     self.screen_control = screen_control
     self.flags = flags
     self.mode = SELECT_MODE
     self.width = 50
     self.sidebar_y = 0
     self.description_clear = True
     if self.get_is_sidebar_mode():
         logger.add_event("init_wide_mode")
     else:
         logger.add_event("init_narrow_mode")
Пример #7
0
    def __init__(
        self,
        flags: ScreenFlags,
        key_bindings: KeyBindings,
        stdscr: ScreenBase,
        line_objs: Dict[int, LineBase],
        curses_api: CursesApiBase,
    ):
        self.stdscr = stdscr
        self.curses_api = curses_api
        self.curses_api.use_default_colors()
        self.color_printer = ColorPrinter(self.stdscr, curses_api)
        self.flags = flags
        self.key_bindings = key_bindings

        self.line_objs = line_objs
        self.hover_index = 0
        self.scroll_offset = 0
        self.scroll_bar = ScrollBar(self.color_printer, line_objs, self)
        self.helper_chrome = HelperChrome(self.color_printer, self, flags)
        self.old_max_y, self.old_max_x = self.get_screen_dimensions()
        self.mode = SELECT_MODE

        # lets loop through and split
        self.line_matches: List[LineMatch] = []

        for line_obj in self.line_objs.values():
            line_obj.set_controller(self)
            if isinstance(line_obj, LineMatch):
                self.line_matches.append(line_obj)

        # begin tracking dirty state
        self.dirty = False
        self.dirty_indexes: List[int] = []

        if self.flags.args.all:
            self.toggle_select_all()

        self.num_lines = len(line_objs.keys())
        self.num_matches = len(self.line_matches)

        self.set_hover(self.hover_index, True)

        # the scroll offset might not start off
        # at 0 if our first real match is WAY
        # down the screen -- so lets init it to
        # a valid value after we have all our line objects
        self.update_scroll_offset()

        logger.add_event("init")
Пример #8
0
    def on_enter(self) -> None:
        line_objs = self.get_paths_to_use()
        if not line_objs:
            # nothing selected, assume we want hovered
            line_objs = self.get_hovered_paths()
        logger.add_event("selected_num_files", len(line_objs))

        # commands passed from the command line get used immediately
        preset_command = self.flags.get_preset_command()
        if len(preset_command) > 0:
            output.exec_composed_command(preset_command, line_objs)
        else:
            output.edit_files(line_objs)

        sys.exit(0)
Пример #9
0
    def __init__(
        self,
        printer: ColorPrinter,
        lines: Dict[int, LineBase],
        screen_control: "Controller",
    ):
        self.printer = printer
        self.screen_control = screen_control
        self.num_lines = len(lines)
        self.box_start_fraction = 0.0
        self.box_stop_fraction = 0.0
        self.calc_box_fractions()

        # see if we are activated
        self.activated = True
        max_y, _max_x = self.screen_control.get_screen_dimensions()
        if self.num_lines < max_y:
            self.activated = False
            logger.add_event("no_scrollbar")
        else:
            logger.add_event("needed_scrollbar")
Пример #10
0
def get_repo_path() -> str:
    proc = subprocess.Popen(
        ["git rev-parse --show-toplevel"],
        stdout=subprocess.PIPE,
        stderr=subprocess.PIPE,
        shell=True,
        universal_newlines=True,
    )

    stdout, stderr = proc.communicate()

    # If there was no error return the output
    if not stderr:
        logger.add_event("using_git")
        return stdout

    proc = subprocess.Popen(
        ["hg root"],
        stdout=subprocess.PIPE,
        stderr=subprocess.PIPE,
        shell=True,
        universal_newlines=True,
    )

    stdout, stderr = proc.communicate()

    # If there was no error return the output
    if not stderr:
        logger.add_event("using_hg")
        return stdout

    # Not a git or hg repo, go with current dir as a default
    logger.add_event("used_outside_repo")
    return "./"
Пример #11
0
def get_line_objs() -> Dict[int, LineBase]:
    file_path = state_files.get_pickle_file_path()
    try:
        line_objs: Dict[int, LineBase] = pickle.load(open(file_path, "rb"))
    except (OSError, KeyError, pickle.PickleError):
        output.append_error(LOAD_SELECTION_WARNING)
        output.append_exit()
        sys.exit(1)
    logger.add_event("total_num_files", len(line_objs))

    selection_path = state_files.get_selection_file_path()
    if os.path.isfile(selection_path):
        set_selections_from_pickle(selection_path, line_objs)

    matches = [
        line_obj for line_obj in line_objs.values() if isinstance(line_obj, LineMatch)
    ]
    if not matches:
        output.write_to_file('echo "No lines matched!";')
        output.append_exit()
        sys.exit(0)
    return line_objs