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 7, 2025
1 parent dfaaad3 commit 59fabde
Show file tree
Hide file tree
Showing 2 changed files with 16 additions and 10 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()

Check warning on line 538 in crmsh/command.py

View check run for this annotation

Codecov / codecov/patch

crmsh/command.py#L538

Added line #L538 was not covered by tests
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

Check warning on line 568 in crmsh/command.py

View check run for this annotation

Codecov / codecov/patch

crmsh/command.py#L567-L568

Added lines #L567 - L568 were not covered by tests


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

Check warning on line 572 in crmsh/command.py

View check run for this annotation

Codecov / codecov/patch

crmsh/command.py#L572

Added line #L572 was not covered by tests
# Restore the original value of rl_sort_completion_matches
# before calling the completer again
sort_completion_inst.value = orig_sort_completion_value

Check warning on line 575 in crmsh/command.py

View check run for this annotation

Codecov / codecov/patch

crmsh/command.py#L575

Added line #L575 was not covered by tests


readline_path = ctypes.util.find_library('readline')
sort_completion_inst = None
if readline_path:
Expand Down
9 changes: 3 additions & 6 deletions crmsh/ui_configure.py
Original file line number Diff line number Diff line change
Expand Up @@ -205,9 +205,8 @@ def schema_completer(args):
# Sort files using packaging.version
complete_results.sort(key=lambda x: version.parse(version_pattern.match(x).group(1)))

Check warning on line 206 in crmsh/ui_configure.py

View check run for this annotation

Codecov / codecov/patch

crmsh/ui_configure.py#L206

Added line #L206 was not covered by tests

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()

Check warning on line 208 in crmsh/ui_configure.py

View check run for this annotation

Codecov / codecov/patch

crmsh/ui_configure.py#L208

Added line #L208 was not covered by tests

return complete_results

Check warning on line 210 in crmsh/ui_configure.py

View check run for this annotation

Codecov / codecov/patch

crmsh/ui_configure.py#L210

Added line #L210 was not covered by tests


Expand Down Expand Up @@ -251,9 +250,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()

Check warning on line 253 in crmsh/ui_configure.py

View check run for this annotation

Codecov / codecov/patch

crmsh/ui_configure.py#L253

Added line #L253 was not covered by tests
return utils.filter_keys(agent.params(), args)


Expand Down

0 comments on commit 59fabde

Please sign in to comment.