Skip to content

Commit b297d45

Browse files
Define minimum length per filter (#124)
* A minimum length can now be defined per filter * Move the default minimum length to a variable * Added per filter min length to docs * Global min_length can overwrite filter specific min_length * Adhere to formatting spec * Removed default min_length from tmux plugin * Added default value for min_length in FilterDef * Determine min_length value during creation of the filer
1 parent a50d382 commit b297d45

File tree

4 files changed

+36
-17
lines changed

4 files changed

+36
-17
lines changed

HELP.md

+1-1
Original file line numberDiff line numberDiff line change
@@ -5,7 +5,7 @@ You can give feedback or star extrakto at https://github.com/laktak/extrakto
55
Extrakto uses fzf. You only need to type a few keys to find your selection with a fuzzy match.
66

77
- Press *ctrl-f* to change to the next filter mode (*filter_key*)
8-
- *word*, the default filter allows you to select words (min length=5)
8+
- *word*, the default filter allows you to select words (default min length=5)
99
- *all*, runs all filters and allows you select quotes, url, paths, etc. \
1010
You can define your own filters as well.
1111
- *line*, select full lines

extrakto.conf

+8-7
Original file line numberDiff line numberDiff line change
@@ -8,13 +8,14 @@
88

99
# define a section per filter
1010
# each filter must have at least a regex containing one or more capture groups
11-
# regex: a python regex expression
12-
# enabled: is filter active (default True)
13-
# in_all: is included in --all (default True)
14-
# lstrip: characters to strip from left result
15-
# rstrip: characters to strip from right result
16-
# exclude: exclude result if matching
17-
# alt2-9: alternate result (see url)
11+
# regex: a python regex expression
12+
# enabled: is filter active (default True)
13+
# in_all: is included in --all (default True)
14+
# lstrip: characters to strip from left result
15+
# rstrip: characters to strip from right result
16+
# exclude: exclude result if matching
17+
# alt2-9: alternate result (see url)
18+
# min_length: minimum length of the result (default 5)
1819

1920
[word]
2021
# "words" consist of anything but the following characters:

extrakto.py

+25-7
Original file line numberDiff line numberDiff line change
@@ -23,13 +23,15 @@
2323
# and whitespace ( \t\n\r)
2424
RE_WORD = "[^][(){}=$\u2500-\u27BF\uE000-\uF8FF \\t\\n\\r]+"
2525

26+
MIN_LENGTH_DEFAULT = 5
27+
2628

2729
class ExtraktoException(Exception):
2830
pass
2931

3032

3133
class Extrakto:
32-
def __init__(self, *, min_length=5, alt=False, prefix_name=False):
34+
def __init__(self, *, min_length=None, alt=False, prefix_name=False):
3335
conf = ConfigParser(interpolation=None)
3436
default_conf = os.path.join(SCRIPT_DIR, "extrakto.conf")
3537
user_conf = os.path.join(
@@ -71,6 +73,12 @@ def __init__(self, *, min_length=5, alt=False, prefix_name=False):
7173
lstrip=sect.get("lstrip", ""),
7274
rstrip=sect.get("rstrip", ""),
7375
alt=alt,
76+
# prefer global min_length, fallback to filter specific
77+
min_length=(
78+
self.min_length
79+
if self.min_length is not None
80+
else sect.getint("min_length", MIN_LENGTH_DEFAULT)
81+
),
7482
)
7583

7684
def __getitem__(self, key):
@@ -86,14 +94,26 @@ def keys(self):
8694

8795

8896
class FilterDef:
89-
def __init__(self, extrakto, name, *, regex, exclude, lstrip, rstrip, alt):
97+
def __init__(
98+
self,
99+
extrakto,
100+
name,
101+
*,
102+
regex,
103+
exclude,
104+
lstrip,
105+
rstrip,
106+
alt,
107+
min_length=MIN_LENGTH_DEFAULT,
108+
):
90109
self.extrakto = extrakto
91110
self.name = name
92111
self.regex = regex
93112
self.exclude = exclude
94113
self.lstrip = lstrip
95114
self.rstrip = rstrip
96115
self.alt = alt
116+
self.min_length = min_length
97117

98118
def filter(self, text):
99119
res = list()
@@ -111,7 +131,7 @@ def filter(self, text):
111131
if self.rstrip:
112132
item = item.rstrip(self.rstrip)
113133

114-
if len(item) >= self.extrakto.min_length:
134+
if len(item) >= self.min_length:
115135
if not self.exclude or not re.search(self.exclude, item, re.I):
116136
if self.extrakto.alt:
117137
for i, altre in enumerate(self.alt):
@@ -122,7 +142,7 @@ def filter(self, text):
122142
return res
123143

124144

125-
def get_lines(text, *, min_length=5, prefix_name=False):
145+
def get_lines(text, *, min_length=MIN_LENGTH_DEFAULT, prefix_name=False):
126146
lines = []
127147

128148
for raw_line in text.splitlines():
@@ -209,9 +229,7 @@ def main(parser):
209229

210230
parser.add_argument("-r", "--reverse", action="store_true", help="reverse output")
211231

212-
parser.add_argument(
213-
"-m", "--min-length", default=5, help="minimum token length", type=int
214-
)
232+
parser.add_argument("-m", "--min-length", help="minimum token length", type=int)
215233

216234
parser.add_argument(
217235
"--warn-empty", action="store_true", help="warn if result is empty"

extrakto_plugin.py

+2-2
Original file line numberDiff line numberDiff line change
@@ -78,12 +78,12 @@ def get_cap(mode, data):
7878
run_list = []
7979

8080
if mode == "all":
81-
extrakto = Extrakto(min_length=5, alt=True, prefix_name=True)
81+
extrakto = Extrakto(alt=True, prefix_name=True)
8282
run_list = extrakto.all()
8383
elif mode == "line":
8484
res += get_lines(data)
8585
else:
86-
extrakto = Extrakto(min_length=5)
86+
extrakto = Extrakto()
8787
run_list = [mode]
8888

8989
for name in run_list:

0 commit comments

Comments
 (0)