From 4643429c335062295092157ece3f0b46b052cda6 Mon Sep 17 00:00:00 2001 From: Anatoli Babenia Date: Sat, 13 Nov 2021 17:18:27 +0300 Subject: [PATCH 1/2] Simplify handling of query with multiple spaces A followup to #312 --- lib/cheat_wrapper.py | 3 +-- lib/cheat_wrapper_test.py | 3 +++ tests/README.md | 7 +++++++ tests/run-tests.sh | 0 4 files changed, 11 insertions(+), 2 deletions(-) create mode 100644 tests/README.md mode change 100644 => 100755 tests/run-tests.sh diff --git a/lib/cheat_wrapper.py b/lib/cheat_wrapper.py index 606f351..602342c 100644 --- a/lib/cheat_wrapper.py +++ b/lib/cheat_wrapper.py @@ -26,7 +26,7 @@ def _add_section_name(query): if '/' in query: return query if ' ' in query: - delim = " " + return re.sub(r' +', '/', query, count=1) elif '+' in query: delim = "+" @@ -53,7 +53,6 @@ def cheat_wrapper(query, request_options=None, output_format='ansi'): Additional request options specified in `request_options`. """ - def _rewrite_aliases(word): if word == ':bash.completion': return ':bash_completion' diff --git a/lib/cheat_wrapper_test.py b/lib/cheat_wrapper_test.py index 5533edc..9d85be5 100644 --- a/lib/cheat_wrapper_test.py +++ b/lib/cheat_wrapper_test.py @@ -19,6 +19,9 @@ split = """ python copy file python/copy file +python file +python/file + g++ -O1 g++/-O1 """ diff --git a/tests/README.md b/tests/README.md new file mode 100644 index 0000000..fe1e2db --- /dev/null +++ b/tests/README.md @@ -0,0 +1,7 @@ +To run unit tests. + + python3 -m pytest -v ../lib/ + +To run input/output tests. + + ./run-tests.sh diff --git a/tests/run-tests.sh b/tests/run-tests.sh old mode 100644 new mode 100755 From 908bc52237ffe6fc2b4b736b588e329d85b31514 Mon Sep 17 00:00:00 2001 From: Anatoli Babenia Date: Sat, 13 Nov 2021 18:19:45 +0300 Subject: [PATCH 2/2] `g++` workaround could still strip ++ if more ++ were encountered This replaces cycle logic in #312 with regex. --- lib/cheat_wrapper.py | 21 +++------------------ lib/cheat_wrapper_test.py | 4 ++++ 2 files changed, 7 insertions(+), 18 deletions(-) diff --git a/lib/cheat_wrapper.py b/lib/cheat_wrapper.py index 602342c..fdcaff6 100644 --- a/lib/cheat_wrapper.py +++ b/lib/cheat_wrapper.py @@ -27,24 +27,9 @@ def _add_section_name(query): return query if ' ' in query: return re.sub(r' +', '/', query, count=1) - elif '+' in query: - delim = "+" - - index = 0 - length = len(query) - while index != length: - - index = query.index(delim, index) + 1 - - try: - comparison = query.index(delim, index) - except ValueError: - comparison = -1 - - if (index != comparison and index != length): - return "%s/%s" % (query[:index-1], query[index:]) - - return query + if '+' in query: + # replace only single + to avoid catching g++ and friends + return re.sub(r'([^\+])\+([^\+])', r'\1/\2', query, count=1) def cheat_wrapper(query, request_options=None, output_format='ansi'): """ diff --git a/lib/cheat_wrapper_test.py b/lib/cheat_wrapper_test.py index 9d85be5..72449ab 100644 --- a/lib/cheat_wrapper_test.py +++ b/lib/cheat_wrapper_test.py @@ -13,6 +13,7 @@ btrfs~volume python/copy+file python/rosetta/:list emacs:go-mode/:list +g++g++ """ split = """ @@ -22,6 +23,9 @@ python/copy file python file python/file +python+file +python/file + g++ -O1 g++/-O1 """