Пример #1
0
    def display_result(self, y, result, is_current = False, is_marked = False):
        line, find_info, abs_idx = result

        if is_current:
            line_style = self.CANDIDATES_LINE_SELECTED
        elif is_marked:
            line_style = self.CANDIDATES_LINE_MARKED
        else:
            line_style = self.CANDIDATES_LINE_BASIC

        keyword_style = self.CANDIDATES_LINE_QUERY + line_style

        self.display_line(y, 0, line, style = line_style)

        if find_info is None:
            return
        for (subq, match_info) in find_info:
            for x_offset, subq_len in match_info:
                try:
                    x_offset_real = display.screen_len(line, beg = 0, end = x_offset)
                    self.display.add_string(line[x_offset:x_offset + subq_len],
                                            pos_y = y,
                                            pos_x = x_offset_real,
                                            style = keyword_style)
                except curses.error as e:
                    debug.log("addnstr", str(e) + " ({0})".format(y))
Пример #2
0
    def display_results(self):
        result_vertical_pos = self.RESULTS_OFFSET_V
        result_pos_direction = 1 if self.results_top_down else -1

        results_in_page = islice(enumerate(self.model.results),
                                 self.absolute_page_head,
                                 self.absolute_page_tail)

        try:
            for cand_nth, result in results_in_page:
                try:
                    self.display_result(
                        result_vertical_pos,
                        result,
                        is_current=cand_nth == self.model.index,
                        is_marked=self.model.get_is_marked(cand_nth))
                except curses.error as e:
                    debug.log("display_results", str(e))
                result_vertical_pos += result_pos_direction
        except Exception as e:
            # debug.log("display_results", str(e))
            debug.log(
                "display_results",
                six.text_type(" | ".join(
                    map(
                        lambda key: six.text_type(key) + ": " + six.text_type(
                            e.__getattribute__(key)), dir(e)))))
            exception_raw_string = str(e).decode(
                self.percol.encoding) if six.PY2 else str(e)
            self.display_error_message("Error at line " + str(cand_nth) +
                                       ": " + exception_raw_string)
Пример #3
0
    def display_result(self, y, result, is_current=False, is_marked=False):
        line, find_info, abs_idx = result

        if is_current:
            line_style = self.CANDIDATES_LINE_SELECTED
        elif is_marked:
            line_style = self.CANDIDATES_LINE_MARKED
        else:
            line_style = self.CANDIDATES_LINE_BASIC

        keyword_style = self.CANDIDATES_LINE_QUERY + line_style

        self.display_line(y, 0, line, style=line_style)

        if find_info is None:
            return
        for (subq, match_info) in find_info:
            for x_offset, subq_len in match_info:
                try:
                    x_offset_real = display.screen_len(line,
                                                       beg=0,
                                                       end=x_offset)
                    self.display.add_string(line[x_offset:x_offset + subq_len],
                                            pos_y=y,
                                            pos_x=x_offset_real,
                                            style=keyword_style)
                except curses.error as e:
                    debug.log("addnstr", str(e) + " ({0})".format(y))
Пример #4
0
    def display_results(self):
        result_vertical_pos = self.RESULTS_OFFSET_V
        result_pos_direction = 1 if self.results_top_down else -1

        results_in_page = islice(enumerate(self.model.results), self.absolute_page_head, self.absolute_page_tail)

        try:
            for cand_nth, result in results_in_page:
                try:
                    self.display_result(result_vertical_pos, result,
                                        is_current = cand_nth == self.model.index,
                                        is_marked = self.model.get_is_marked(cand_nth))
                except curses.error as e:
                    debug.log("display_results", str(e))
                result_vertical_pos += result_pos_direction
        except Exception as e:
            # debug.log("display_results", str(e))
            debug.log("display_results",
                      six.text_type(" | ".join(
                          map(lambda key: six.text_type(key) +
                              ": "
                              + six.text_type(e.__getattribute__(key)),
                              dir(e)
                          ))
                      ))
            exception_raw_string = str(e).decode(self.percol.encoding) if six.PY2 else str(e)
            self.display_error_message("Error at line " + str(cand_nth) + ": " + exception_raw_string)
Пример #5
0
def eval_string(percol, string_to_eval, encoding = 'utf-8'):
    try:
        import six
        if not isinstance(string_to_eval, six.text_type):
            string_to_eval = string_to_eval.decode(encoding)
        exec(string_to_eval, locals())
    except Exception as e:
        debug.log("Exception in eval_string", e)
Пример #6
0
def eval_string(percol, string_to_eval, encoding = 'utf-8'):
    try:
        import types
        if string_to_eval.__class__ != types.UnicodeType:
            string_to_eval = string_to_eval.decode(encoding)
        exec(string_to_eval, locals())
    except Exception as e:
        debug.log("Exception in eval_string", e)
Пример #7
0
def eval_string(percol, string_to_eval, encoding='utf-8'):
    try:
        import six
        if not isinstance(string_to_eval, six.text_type):
            string_to_eval = string_to_eval.decode(encoding)
        exec(string_to_eval, locals())
    except Exception as e:
        debug.log("Exception in eval_string", e)
Пример #8
0
def eval_string(percol, string_to_eval, encoding = 'utf-8'):
    try:
        import types
        if string_to_eval.__class__ != types.UnicodeType:
            string_to_eval = string_to_eval.decode(encoding)
        exec(string_to_eval, locals())
    except Exception as e:
        debug.log("Exception in eval_string", e)
Пример #9
0
 def handle_normal_key(self, ch):
     k = self.keyhandler.get_key_for(ch)
     if k in self.keymap:
         self.keymap[k](self)
         debug.log("here again %s, %s"%(ch,k))
     elif self.keyhandler.is_displayable_key(ch):
         self.model.insert_char(ch)
     return k
Пример #10
0
Файл: cli.py Проект: mooz/percol
def set_proper_locale(options):
    try:
        locale.setlocale(locale.LC_ALL, '')
    except Exception as e:
        debug.log("Exception in setlocale", e)
    output_encoding = locale.getpreferredencoding()
    if options.output_encoding:
        output_encoding = options.output_encoding
    return output_encoding
Пример #11
0
def set_proper_locale(options):
    try:
        locale.setlocale(locale.LC_ALL, '')
    except Exception as e:
        debug.log("Exception in setlocale", e)
    output_encoding = locale.getpreferredencoding()
    if options.output_encoding:
        output_encoding = options.output_encoding
    return output_encoding
Пример #12
0
def load_rc(percol, path=None, encoding=None):
    import os
    from percol import debug
    if path is None:
        path = os.path.expanduser("~/.percol.d/rc.py")
    try:
        with open(path, 'r') as rc:
            exec(rc.read().decode(encoding or 'utf-8'), locals())
    except Exception as e:
        debug.log("exception", e)
Пример #13
0
def load_rc(percol, path=None, encoding=None):
    import os
    from percol import debug
    if path is None:
        path = os.path.expanduser("~/.percol.d/rc.py")
    try:
        with open(path, 'r') as rc:
            exec(rc.read().decode(encoding or 'utf-8'), locals())
    except Exception as e:
        debug.log("exception", e)
Пример #14
0
 def get_selected_results_with_index(self):
     results = self.get_marked_results_with_index()
     if not results:
         try:
             index = self.index
             result = self.results[index] # EAFP (results may be a zero-length list)
             results.append((result[0], index, result[2]))
         except Exception as e:
             debug.log("get_selected_results_with_index", e)
     return results
Пример #15
0
 def get_selected_results_with_index(self):
     results = self.get_marked_results_with_index()
     if not results:
         try:
             index = self.index
             result = self.results[index] # EAFP (results may be a zero-length list)
             results.append((result[0], index, result[2]))
         except Exception as e:
             debug.log("get_selected_results_with_index", e)
     return results
Пример #16
0
def load_rc(percol, path = None, encoding = 'utf-8'):
    if path is None:
        path = os.path.expanduser("~/.percol.d/rc.py")
    if not os.path.exists(path):
        path = os.path.join(os.path.dirname(os.path.abspath(__file__)), 'rc.py')
    try:
        with open(path, 'r') as rc:
            exec(rc.read().decode(encoding), locals())
    except Exception as e:
        debug.log("Exception in rc file {0}".format(path), e)
Пример #17
0
 def stack_fname_prompt(self):
     win_y = self.RESULTS_DISPLAY_MAX + 1
     win_x = len(self.STACKLINE)
     
     curses.noecho()
     textwin,textbox = self.maketextbox(1,40, win_y,win_x,"")
     
     flag = False
     textbox.edit()
     text = textbox.gather()
     debug.log("Filename: %s"%text)
Пример #18
0
    def stack_fname_prompt(self):
        win_y = self.RESULTS_DISPLAY_MAX + 1
        win_x = len(self.STACKLINE)

        curses.noecho()
        textwin, textbox = self.maketextbox(1, 40, win_y, win_x, "")

        flag = False
        textbox.edit()
        text = textbox.gather()
        debug.log("view.py Filename: %s" % text)
Пример #19
0
def load_rc(percol, path=None, encoding='utf-8'):
    if path is None:
        path = os.path.expanduser("~/.percol.d/rc.py")
    if not os.path.exists(path):
        path = os.path.join(os.path.dirname(os.path.abspath(__file__)),
                            'rc.py')
    try:
        with open(path, 'r') as rc:
            exec(rc.read().decode(encoding), locals())
    except Exception as e:
        debug.log("Exception in rc file {0}".format(path), e)
Пример #20
0
    def execute_action(self):
        selected_actions = self.model_action.get_selected_results_with_index()

        if selected_actions and self.args_for_action:
            for name, _, act_idx in selected_actions:
                try:
                    action = self.actions[act_idx]
                    if action:
                        action.act([arg for arg, _, _ in self.args_for_action], self)
                except Exception as e:
                    debug.log("execute_action", e)
Пример #21
0
 def stack_selected_results_with_index_f(self,field=1,sep=' <> '):
     results = self.get_marked_results_with_index()
     if not results:
         try:
             index = self.index
             result = self.results[index] # EAFP (results may be a zero-length list)
             results.append((result[0], index, result[2]))
         except Exception as e:
             debug.log("stack_selected_results_with_index_f", e)
     if field is not None:
         results = [r[0].split(sep)[2] for r in results]
     self.stack += results
Пример #22
0
 def get_selected_results_with_index_f(self,field=None,sep=' <> '):
     results = self.get_marked_results_with_index()
     if not results:
         try:
             index = self.index
             result = self.results[index] # EAFP (results may be a zero-length list)
             results.append((result[0], index, result[2]))
         except Exception as e:
             debug.log("get_selected_results_with_index_f", e)
     if field is not None:
         results = [(r[0].split(sep)[field],r[1],r[2]) for r in results]
     return results
Пример #23
0
    def display_result(self, y, result, is_current=False, is_marked=False):
        line, find_info, abs_idx, exit_status = result
        if exit_status == 0:  # set separator colour depending on exit status
            bar_colour = self.exit0_color
        else:
            bar_colour = self.exitnot0_color

        if is_current:
            line_style = self.CANDIDATES_LINE_SELECTED
        elif is_marked:
            line_style = self.CANDIDATES_LINE_MARKED
        else:
            line_style = self.CANDIDATES_LINE_BASIC

        keyword_style = self.CANDIDATES_LINE_QUERY + line_style

        # self.fold_fields = [1,2]

        new_line = self.fold_line(line, self.FIELD_SEP, self.fold_fields)

        self.display_line(y, 0, new_line, style=line_style)

        spans = self.get_spans(line, self.FIELD_SEP)
        new_spans = self.get_spans(new_line, self.FIELD_SEP)

        if find_info is None:
            return
        for (subq, match_info) in find_info:
            new_match_info = self.fold_matches(spans, new_spans, subq,
                                               match_info, self.fold_fields,
                                               self.FOLDED)
            for x_offset, subq_len in new_match_info:
                # for x_offset, subq_len in match_info:
                try:
                    x_offset_real = display.screen_len(new_line,
                                                       beg=0,
                                                       end=x_offset)

                    self.display.add_string(new_line[x_offset:x_offset +
                                                     subq_len],
                                            pos_y=y,
                                            pos_x=x_offset_real,
                                            style=keyword_style)

                except curses.error as e:
                    debug.log("view.py, addnstr", str(e) + " ({0})".format(y))

                for n in new_spans:  # colour separator blue or red depending on exit status
                    self.display.add_string(
                        new_line[n[0] - len(self.FIELD_SEP):n[0]],
                        pos_y=y,
                        pos_x=n[0] - len(self.FIELD_SEP),
                        style=(bar_colour, "bold") + line_style)
Пример #24
0
 def execute_action(self):
     # selected_actions = self.model_action.get_selected_results_with_index_f(sep=self.model.finder.sep)
     selected_actions = self.model_action.get_selected_results_with_index()
     
     if selected_actions and self.args_for_action:
         for name, _, act_idx in selected_actions:
             # debug.log(f'init.py 122, {self.args_for_action[0][0]}')
             try:
                 action = self.actions[act_idx]
                 if action:
                     action.act([arg for arg, _, _ in self.args_for_action], self)
             except Exception as e:
                 debug.log("init.py, execute_action", e)
Пример #25
0
 def stack_selected_results_with_index_f(self, field=1, sep='║'):
     results = self.get_marked_results_with_index()
     if not results:
         try:
             index = self.index
             result = self.results[
                 index]  # EAFP (results may be a zero-length list)
             results.append((result[0], index, result[2]))
         except Exception as e:
             debug.log("model.py, stack_selected_results_with_index_f", e)
     if field is not None:
         results = [r[0].split(sep)[2] for r in results]
     self.stack += results
Пример #26
0
    def display_results(self):
        result_vertical_pos = self.RESULTS_OFFSET_V
        result_pos_direction = 1 if self.results_top_down else -1

        results_in_page = islice(enumerate(self.model.results), self.absolute_page_head, self.absolute_page_tail)

        for cand_nth, result in results_in_page:
            try:
                self.display_result(result_vertical_pos, result,
                                    is_current = cand_nth == self.model.index,
                                    is_marked = self.model.get_is_marked(cand_nth))
            except curses.error as e:
                debug.log("display_results", str(e))
            result_vertical_pos += result_pos_direction
Пример #27
0
 def get_selected_results_with_index(self):
     results = self.get_marked_results_with_index()
     if not results:
         try:
             index = self.index
             result = self.results[
                 index]  # EAFP (results may be a zero-length list)
             results.append((result[0], index, result[2]))
         except Exception as e:
             debug.log(
                 f"model.py 102, get_selected_results_with_index, {self.finder.host}",
                 e)
     # debug.log(results)
     return results
Пример #28
0
    def display_results(self):
        result_vertical_pos = self.RESULTS_OFFSET_V
        result_pos_direction = 1 if self.results_top_down else -1

        results_in_page = islice(enumerate(self.model.results),
                                 self.absolute_page_head,
                                 self.absolute_page_tail)

        for cand_nth, result in results_in_page:
            try:
                self.display_result(
                    result_vertical_pos,
                    result,
                    is_current=cand_nth == self.model.index,
                    is_marked=self.model.get_is_marked(cand_nth))
            except curses.error as e:
                debug.log("display_results", str(e))
            result_vertical_pos += result_pos_direction
Пример #29
0
    def display_result(self, y, result, is_current = False, is_marked = False):
        line, find_info, abs_idx = result

        if is_current:
            line_style = self.CANDIDATES_LINE_SELECTED
        elif is_marked:
            line_style = self.CANDIDATES_LINE_MARKED
        else:
            line_style = self.CANDIDATES_LINE_BASIC

        keyword_style = self.CANDIDATES_LINE_QUERY + line_style

        # self.fold_fields = [1,2]

        new_line = self.fold_line(line,self.FIELD_SEP,self.fold_fields)

        self.display_line(y, 0, new_line, style = line_style)

        spans = self.get_spans(line, self.FIELD_SEP)
        new_spans = self.get_spans(new_line, self.FIELD_SEP)

        if find_info is None:
            return
        for (subq, match_info) in find_info:
            new_match_info = self.fold_matches(spans, new_spans, subq, match_info, self.fold_fields, self.FOLDED)
            for x_offset, subq_len in new_match_info:
            # for x_offset, subq_len in match_info:
                try:
                    x_offset_real = display.screen_len(new_line, beg = 0, end = x_offset)

                    self.display.add_string(new_line[x_offset:x_offset + subq_len],
                                            pos_y = y,
                                            pos_x = x_offset_real,
                                            style = keyword_style)
                    
                    # debug.log((line,x_offset,x_offset+subq_len,y,x_offset_real))
                    # debug.log(line[x_offset:x_offset + subq_len])
                    # x_offset_real = display.screen_len(line, beg = 0, end = x_offset)
                    # self.display.add_string(line[x_offset:x_offset + subq_len],
                    #                         pos_y = y,
                    #                         pos_x = x_offset_real,
                    #                         style = keyword_style)
                except curses.error as e:
                    debug.log("addnstr", str(e) + " ({0})".format(y))
Пример #30
0
    def find(self, query, collection = None):
        query_is_empty = query == ""

        # Arrange queries
        if self.case_insensitive:
            query = query.lower()
        # Split query when split_query is True
        if self.split_query:
            queries = [self.transform_query(sub_query)
                       for sub_query in query.split(self.split_str)]
        else:
            queries = [self.transform_query(query)]

        if collection is None:
            collection = self.collection

        found_commands = []

        for idx, line in enumerate(collection):
            if query_is_empty:
                res = self.dummy_res
            else:
                if self.case_insensitive:
                    line_to_match = line.lower()
                else:
                    line_to_match = line
                res = self.find_queries(queries, line_to_match)
                # When invert_match is enabled (via "-v" option),
                # select non matching line
                if self.invert_match:
                    res = None if res else self.dummy_res

            if res:
                command = line.split(self.sep)[-1].strip()
                # debug.log(len(found_commands))
                if not command in found_commands and self.recent_commands:
                    debug.log(command)
                    found_commands.append(command)
                    yield line, res, idx
                elif not self.recent_commands:
                    yield line, res, idx
Пример #31
0
    def get_selected_results_with_index_f(self, sep, field=None):
        results = self.get_marked_results_with_index()
        # debug.log(f'model.py 116, sep:{sep}, r:{results}')

        if not results:
            try:
                index = self.index
                result = self.results[
                    index]  # EAFP (results may be a zero-length list)
                results.append((result[0], index, result[2]))
                # debug.log(f'model.py 123, {results}')
            except Exception as e:
                debug.log("model.py 124, get_selected_results_with_index_f", e)

        if field is not None:
            # debug.log(f'model.py 127, {results}')
            if field == 1:  # put quotes around directories
                newresults = []
                for r in results:
                    path = self.quote_path(r[0].split(sep)[1])
                    newresults.append((path, r[1], r[2]))
                    # debug.log("model.py, Modified path %s"%path)
                results = newresults
            elif field == -1:  #chdir and run command
                newresults = []
                for r in results:
                    path = self.quote_path(r[0].split(sep)[1])
                    command = r[0].split(sep)[2]
                    newresults.append((path + '; ' + command, r[1], r[2]))
                    # debug.log("model.py, Modified path %s"%path)
                results = newresults
            else:
                results = [(r[0].split(sep)[field], r[1], r[2])
                           for r in results]
                # debug.log(f'model.py 145, r:{results}')

        return results