Skip to content

Commit

Permalink
Dev: Refactor: Introduce custom sort order enable/disable functions
Browse files Browse the repository at this point in the history
Added `enable_custom_sort_order` and `disable_custom_sort_order`
functions to encapsulate the logic for modifying `rl_sort_completion_matches`.
  • Loading branch information
liangxin1300 committed Feb 8, 2025
1 parent 1cc1ca6 commit 4191083
Show file tree
Hide file tree
Showing 2 changed files with 14 additions and 7 deletions.
17 changes: 13 additions & 4 deletions crmsh/command.py
Original file line number Diff line number Diff line change
Expand Up @@ -535,10 +535,7 @@ def complete(self, context, args):
'''
ret = []
if self.completer is not None:
if sort_completion_inst is not None and sort_completion_inst.value == 0:
# Restore the original value of rl_sort_completion_matches
# before calling the completer again
sort_completion_inst.value = orig_sort_completion_value
disable_custom_sort_order()
specs = inspect.getfullargspec(self.completer)
if 'context' in specs.args:
ret = self.completer([self.name] + args, context)
Expand Down Expand Up @@ -566,6 +563,18 @@ def _check_args(fn, expected):
": Expected method with signature " + repr(expected))


def enable_custom_sort_order():
if sort_completion_inst is not None:
sort_completion_inst.value = 0


def disable_custom_sort_order():
if sort_completion_inst is not None and sort_completion_inst.value == 0:
# Restore the original value of rl_sort_completion_matches
# before calling the completer again
sort_completion_inst.value = orig_sort_completion_value


readline_path = ctypes.util.find_library('readline')
sort_completion_inst = None
if readline_path:
Expand Down
4 changes: 1 addition & 3 deletions crmsh/ui_configure.py
Original file line number Diff line number Diff line change
Expand Up @@ -251,9 +251,7 @@ def _prim_params_completer(agent, args):
return []
elif '=' in completing:
return []
if command.sort_completion_inst is not None:
# Set the value to 0 to use the costum sort order
command.sort_completion_inst.value = 0
command.enable_custom_sort_order()
return utils.filter_keys(agent.params(), args)


Expand Down

0 comments on commit 4191083

Please sign in to comment.