8
8
logger = logging .getLogger (__name__ )
9
9
10
10
11
- def register_tabcomplete () -> None :
11
+ def register_tabcomplete () -> None : # pragma: no cover
12
12
"""Register tab completion for readline."""
13
13
14
14
# set up tab completion
@@ -26,7 +26,7 @@ def register_tabcomplete() -> None:
26
26
readline .parse_and_bind ("tab: complete" )
27
27
28
28
29
- def _completer (text : str , state : int ) -> str | None :
29
+ def _completer (text : str , state : int ) -> str | None : # pragma: no cover
30
30
"""
31
31
Tab completion for readline.
32
32
@@ -38,41 +38,40 @@ def _completer(text: str, state: int) -> str | None:
38
38
return _matches (text )[state ]
39
39
40
40
41
+ def _process_completion (p : Path ) -> str :
42
+ # Strip cwd from path
43
+ p = Path (str (p ).replace (str (Path .cwd ()) + "/" , "" ))
44
+
45
+ # If path is a directory, add trailing slash
46
+ if p .exists () and p .is_dir ():
47
+ return str (p ) + "/"
48
+ else :
49
+ return str (p )
50
+
51
+
41
52
@lru_cache (maxsize = 1 )
42
53
def _matches (text : str ) -> list [str ]:
43
54
"""Returns a list of matches for text to complete."""
44
55
45
56
# if text starts with /, complete with commands or files as absolute paths
46
57
if text .startswith ("/" ):
47
58
# if no text, list all commands
48
- all_commands = [f"{ CMDFIX } { cmd } " for cmd in COMMANDS if cmd != "help" ]
59
+ all_commands = [f"{ CMDFIX } { cmd } " for cmd in COMMANDS ]
49
60
if not text [1 :]:
50
61
return all_commands
51
62
# else, filter commands with text
52
63
else :
53
- matching_files = [str (p ) for p in Path ("/" ).glob (text [1 :] + "*" )]
64
+ matching_files = [
65
+ _process_completion (p ) for p in Path ("/" ).glob (text [1 :] + "*" )
66
+ ]
54
67
return [
55
68
cmd for cmd in all_commands if cmd .startswith (text )
56
69
] + matching_files
57
70
58
- # if text starts with ., complete with current dir
59
- elif text .startswith ("." ):
60
- if not text [1 :]:
61
- return [str (Path .cwd ())]
62
- else :
63
- all_files = [str (p ) for p in Path .cwd ().glob ("*" )]
64
- return [f for f in all_files if f .startswith (text )]
65
-
66
71
# if text starts with ../, complete with parent dir
67
72
elif text .startswith (".." ):
68
- if not text [2 :]:
69
- return [str (Path .cwd ().parent )]
70
- else :
71
- return [str (p ) for p in Path .cwd ().parent .glob (text [2 :] + "*" )]
73
+ return [_process_completion (p ) for p in Path (".." ).glob (text [3 :] + "*" )]
72
74
73
75
# else, complete with files in current dir
74
76
else :
75
- if not text :
76
- return [str (Path .cwd ())]
77
- else :
78
- return [str (p ) for p in Path .cwd ().glob (text + "*" )]
77
+ return [_process_completion (p ) for p in Path .cwd ().glob (text + "*" )]
0 commit comments