Update in response to feedback

- one configuration option: ignore_paths
- use shlex to parse option to support whitespace and in-line comments
- change option type to Tuple[str, ...]
- remove ignored directories from dirnames to prevent scanning
This commit is contained in:
Suvayu Ali
2022-06-06 09:58:18 +02:00
parent eea652f1d0
commit 20b6a97159
6 changed files with 26 additions and 31 deletions

View File

@@ -7,6 +7,7 @@ from contextlib import suppress
from fnmatch import fnmatch
from functools import lru_cache
from hashlib import md5
from itertools import product
from kitty.guess_mime_type import guess_type
from typing import TYPE_CHECKING, Dict, List, Set, Optional, Iterator, Tuple, Union
@@ -38,8 +39,7 @@ class Segment:
class Collection:
file_ignores = ['']
ignore_paths = ['']
ignore_paths: Tuple[str, ...] = ()
def __init__(self) -> None:
self.changes: Dict[str, str] = {}
@@ -109,26 +109,29 @@ def resolve_remote_name(path: str, default: str) -> str:
return default
def walk(base: str, names: Set[str], pmap: Dict[str, str], ignore_paths: Tuple[str, ...]) -> None:
for dirpath, dirnames, filenames in os.walk(base):
ignored = [_dir for _dir, pat in product(dirnames, ignore_paths) if fnmatch(_dir, pat)]
for _dir in ignored:
dirnames.pop(dirnames.index(_dir))
for filename in filenames:
if any(fnmatch(filename, pat) for pat in ignore_paths if pat):
continue
path = os.path.abspath(os.path.join(dirpath, filename))
path_name_map[path] = name = os.path.relpath(path, base)
names.add(name)
pmap[name] = path
def collect_files(collection: Collection, left: str, right: str) -> None:
left_names: Set[str] = set()
right_names: Set[str] = set()
left_path_map: Dict[str, str] = {}
right_path_map: Dict[str, str] = {}
def walk(base: str, names: Set[str], pmap: Dict[str, str]) -> None:
for dirpath, dirnames, filenames in os.walk(base):
if any(fnmatch(dirpath, f"*/{pat}") for pat in collection.ignore_paths if pat):
continue
for filename in filenames:
if any(fnmatch(filename, f"{pat}") for pat in collection.file_ignores if pat):
continue
path = os.path.abspath(os.path.join(dirpath, filename))
path_name_map[path] = name = os.path.relpath(path, base)
names.add(name)
pmap[name] = path
walk(left, left_names, left_path_map, collection.ignore_paths)
walk(right, right_names, right_path_map, collection.ignore_paths)
walk(left, left_names, left_path_map)
walk(right, right_names, right_path_map)
common_names = left_names & right_names
changed_names = {n for n in common_names if data_for_path(left_path_map[n]) != data_for_path(right_path_map[n])}
for n in changed_names:

View File

@@ -655,7 +655,6 @@ def main(args: List[str]) -> None:
opts = init_config(cli_opts)
set_diff_command(opts.diff_cmd)
lines_for_path.replace_tab_by = opts.replace_tab_by
Collection.file_ignores = opts.file_ignores
Collection.ignore_paths = opts.ignore_paths
left, right = map(get_remote_file, (left, right))
if os.path.isdir(left) != os.path.isdir(right):

View File

@@ -48,14 +48,11 @@ opt('replace_tab_by', '\\x20\\x20\\x20\\x20',
long_text='The string to replace tabs with. Default is to use four spaces.'
)
opt('file_ignores', '',
option_type='pattern_list',
long_text='List of file patterns that are ignored when directories are compared'
)
opt('ignore_paths', '',
option_type='pattern_list',
long_text='List of directory patterns that are ignored when directories are compared'
long_text='''List of patterns that are matched against directory and file
names and ignored when directories are compared.
''',
)
egr() # }}}

View File

@@ -86,10 +86,7 @@ class Parser:
for k in parse_map(val):
ans['map'].append(k)
def file_ignores(self, val: str, ans: typing.Dict[str, typing.List[str]]):
ans['file_ignores'] = pattern_list(val)
def ignore_paths(self, val: str, ans: typing.Dict[str, typing.List[str]]):
def ignore_paths(self, val: str, ans: typing.Dict[str, typing.Any]):
ans['ignore_paths'] = pattern_list(val)

View File

@@ -14,7 +14,6 @@ option_names = ( # {{{
'added_margin_bg',
'background',
'diff_cmd',
'file_ignores',
'filler_bg',
'foreground',
'highlight_added_bg',
@@ -45,14 +44,13 @@ class Options:
added_margin_bg: Color = Color(205, 255, 216)
background: Color = Color(255, 255, 255)
diff_cmd: str = 'auto'
file_ignores: typing.List[str] = ['']
filler_bg: Color = Color(250, 251, 252)
foreground: Color = Color(0, 0, 0)
highlight_added_bg: Color = Color(172, 242, 189)
highlight_removed_bg: Color = Color(253, 184, 192)
hunk_bg: Color = Color(241, 248, 255)
hunk_margin_bg: Color = Color(219, 237, 255)
ignore_paths: typing.List[str] = ['']
ignore_paths: typing.Tuple[str, ...] = ()
margin_bg: Color = Color(250, 251, 252)
margin_fg: Color = Color(170, 170, 170)
margin_filler_bg: typing.Optional[kitty.fast_data_types.Color] = None

View File

@@ -3,6 +3,7 @@
# License: GPLv3 Copyright: 2021, Kovid Goyal <kovid at kovidgoyal.net>
import shlex
from typing import Any, Dict, Iterable, List, Tuple, Union
from kitty.conf.utils import (
@@ -58,8 +59,8 @@ def syntax_aliases(raw: str) -> Dict[str, str]:
return ans
def pattern_list(raw: str) -> List[str]:
return raw.split(' ')
def pattern_list(raw: str) -> Tuple[str, ...]:
return tuple(shlex.split(raw, comments=True))
def parse_map(val: str) -> Iterable[KittensKeyDefinition]: