Implemented find with ripgrep/grep

This commit is contained in:
Lionel Dricot 2022-03-25 13:37:23 +01:00
parent 05da20801c
commit 4acebf6fba
3 changed files with 27 additions and 20 deletions

View File

@ -1,6 +1,8 @@
# Offpunk History
## 1.3 - Unreleased
- New behaviour for "find" (or "/") which is to grep through current page (ripgrep used if detected)
- Default width set to 80 as many gopherhole and gemini capsule have it hardcoded
- Streaming URL without valid content-length are now closed after 5Mo of download (thanks to Eoin Carney for reporting the issue)
- Fixed a crash when the cache is already a dir inside a dir.

View File

@ -69,7 +69,7 @@ else beyond the Python standard library and the "less" pager. However, it will
import" a few other libraries if they are available to offer an improved
experience or some other features. Python libraries requests, bs4 and readability are required for http/html support. Images are displayed if chafa or timg are presents (python-pil is needed for chafa version before 1.10). When displaying only a picture (not inline), rendering will be pixel perfect in compatible terminals (such as Kitty) if chafa is at least version 1.8 or if timg is used.
To avoid using unstable or too recent libraries, the rule of thumb is that a library should be packaged in Debian/Ubuntu. Keep in mind that Offpunk is mainly tested will all libraries installed. If you encounter a crash without one optional dependencies, please report it.
To avoid using unstable or too recent libraries, the rule of thumb is that a library should be packaged in Debian/Ubuntu. Keep in mind that Offpunk is mainly tested will all libraries installed. If you encounter a crash without one optional dependencies, please report it. Patches and contributions to remove dependencies or support alternatives are highly appreciated.
Run command `version` in offpunk to see if you are missing some dependencies.
@ -81,12 +81,13 @@ Run command `version` in offpunk to see if you are missing some dependencies.
provide a better and slightly more secure experience when using the default
TOFU certificate validation mode and is highly recommended (apt-get install python3-cryptography).
* [Python magic](https://github.com/ahupp/python-magic/) is useful to determine the MIME type of cached object. If not present, the file extension will be used but some capsules provide wrong extension or no extension at all. Python-magic is highly recommended. (apt-get install python3-magic)
* [Python editor](https://github.com/fmoo/python-editor) is used to edit your lists with "list edit". (apt-get install python3-editor)
* [Python editor](https://github.com/fmoo/python-editor) is used to edit your lists with "list edit". Removing this dependency is on the roadmap (apt-get install python3-editor)
* [Xsel](http://www.vergenet.net/~conrad/software/xsel/) allows to `go` to the URL copied in the clipboard without having to paste it (both X and traditional clipboards are supported). Also needed to use the `copy` command. (apt-get install xsel)
* [Chafa](https://hpjansson.org/chafa/) allows to display pictures in your console. Install it and browse to an HTML page with picture to see the magic.
* [Timg](https://github.com/hzeller/timg) is a slower alternative to chafa for inline images. But it has better rendering when displaying only the image. Install both to get the best of both world but if you need to choose one, choose Chafa.
* [Python-pil](http://python-pillow.github.io/) is required to only display the first frame of animated gif with chafa if chafa version is lower than 1.10.
* [Python-setproctitle](https://github.com/dvarrazzo/py-setproctitle) will change the process name from "python" to "offpunk". Useful to kill it without killing every python service.
* [RipGrep](https://github.com/BurntSushi/ripgrep) is used, if found, to add colours to your in-page searches ("find" or "/").
## Features

View File

@ -218,6 +218,9 @@ _MAX_REDIRECTS = 5
_MAX_CACHE_SIZE = 10
_MAX_CACHE_AGE_SECS = 180
_GREP = "grep"
if shutil.which("rg"):
_GREP = "rg"
less_version = 0
if not shutil.which("less"):
print("Please install the pager \"less\" to run Offpunk.")
@ -244,24 +247,30 @@ else:
# -R : interpret ANSI colors correctly
# -f : suppress warning for some contents
# -M : long prompt (to have info about where you are in the file)
# -w : hilite the new first line after a page skip (space)
# -W : hilite the new first line after a page skip (space)
# -i : ignore case in search
# -S : do not wrap long lines. Wrapping is done by offpunk, longlines
# are there on purpose (surch in asciiart)
#--incsearch : incremental search starting rev581
if less_version >= 581:
less_base = "less --incsearch --save-marks -~ -XRfMwiS"
less_base = "less --incsearch --save-marks -~ -XRfMWiS"
else:
less_base = "less --save-marks -XRfMwiS"
less_base = "less --save-marks -XRfMWiS"
_DEFAULT_LESS = less_base + " \"+''\" %s"
_DEFAULT_CAT = less_base + " -EF %s"
def less_cmd(file, histfile=None,cat=False):
def less_cmd(file, histfile=None,cat=False,grep=None):
if histfile:
prefix = "LESSHISTFILE=%s "%histfile
else:
prefix = ""
if cat:
cmd_str = prefix + _DEFAULT_CAT % file
elif grep:
grep_cmd = _GREP
#case insensitive for lowercase search
if grep.islower():
grep_cmd += " -i"
cmd_str = prefix + _DEFAULT_CAT % file + "|" + grep_cmd + " %s"%grep
else:
cmd_str = prefix + _DEFAULT_LESS % file
subprocess.call(cmd_str,shell=True)
@ -613,7 +622,7 @@ class AbstractRenderer():
title_r.close_color("red")
return title_r.get_final()
def display(self,mode="readable",window_title="",window_info=None):
def display(self,mode="readable",window_title="",window_info=None,grep=None):
wtitle = self._window_title(window_title,info=window_info)
body = wtitle + "\n" + self.get_body(mode=mode)
if not body:
@ -630,7 +639,7 @@ class AbstractRenderer():
self.less_histfile[mode] = tmpf.name
else:
firsttime = False
less_cmd(self.temp_file[mode], histfile=self.less_histfile[mode],cat=firsttime)
less_cmd(self.temp_file[mode], histfile=self.less_histfile[mode],cat=firsttime,grep=grep)
return True
def get_temp_file(self,mode="readable"):
@ -987,7 +996,7 @@ class ImageRenderer(AbstractRenderer):
for l in lines:
new_img += spaces*" " + l + "\n"
return new_img, []
def display(self,mode=None,window_title=None,window_info=None):
def display(self,mode=None,window_title=None,window_info=None,grep=None):
if window_title:
print(self._window_title(window_title,info=window_info))
terminal_image(self.body)
@ -1528,7 +1537,7 @@ class GeminiItem():
if not self.renderer.is_valid():
self.renderer = None
def display(self,mode=None):
def display(self,mode=None,grep=None):
if not self.renderer:
self._set_renderer()
if self.renderer and self.renderer.is_valid():
@ -1545,7 +1554,7 @@ class GeminiItem():
else:
str_last = "last accessed on %s" %time.ctime(self.cache_last_modified())
title += " (%s links)"%nbr
return self.renderer.display(mode=mode,window_title=title,window_info=str_last)
return self.renderer.display(mode=mode,window_title=title,window_info=str_last,grep=grep)
else:
return False
@ -3109,6 +3118,7 @@ Marks are temporary until shutdown (not saved to disk)."""
output += " - python-readability : " + has(_HAS_READABILITY)
output += " - python-setproctitle : " + has(_HAS_SETPROCTITLE)
output += " - xdg-open : " + has(_HAS_XDGOPEN)
output += " - ripgrep : " + has(shutil.which("rg"))
output += " - xsel : " + has(_HAS_XSEL)
output += " - timg : " + has(_HAS_TIMG)
if _NEW_CHAFA:
@ -3151,16 +3161,10 @@ Use 'ls -l' to see URLs."""
"""Display history."""
self.list_show("history")
@needs_gi
def do_find(self, searchterm):
"""Find in the list of links (case insensitive)."""
results = [
gi for gi in self.lookup if searchterm.lower() in gi.name.lower()]
if results:
self.lookup = results
self._show_lookup()
self.page_index = 0
else:
print("No results found.")
"""Find in current page by displaying only relevant lines (grep)."""
self.gi.display(grep=searchterm)
def emptyline(self):
"""Page through index ten lines at a time."""