From 1355b074b853819eeefd4be14c7935540c575356 Mon Sep 17 00:00:00 2001 From: Dror Harari Date: Thu, 6 Jan 2022 03:30:19 +0200 Subject: [PATCH 1/2] Add semicalc feature to the Calc plugin Add semicalc (multi-part calculation) feature to the Calc plugin. For example, writing "10+20+30;*1.3" is equivalent to "(10+20+30)*1.3" Every semicolon is replaced with the preceding part of the expression, wrapped in parentheses. --- Calc/calc.py | 14 +++++++++++++- README.md | 3 ++- 2 files changed, 15 insertions(+), 2 deletions(-) diff --git a/Calc/calc.py b/Calc/calc.py index 37f2925..e62a900 100644 --- a/Calc/calc.py +++ b/Calc/calc.py @@ -413,6 +413,18 @@ def on_catalog(self): args_hint=kp.ItemArgsHint.REQUIRED, hit_hint=kp.ItemHitHint.NOARGS)]) + def semicalc(self, expression): + while True: + semipos = expression.find(";") + if semipos < 0: + return expression + elif semipos == 0: + expression = expression[1:] + elif len(expression) > semipos and (expression[semipos+1].isalnum() or expression[semipos+1] in "(){}[]"): + return f"error: {expression}" + else: + expression = f"({expression[0:semipos]}){expression[semipos+1:]}" + def on_suggest(self, user_input, items_chain): if items_chain and items_chain[0].category() == self.ITEMCAT_VAR: suggestions = [] @@ -447,7 +459,7 @@ def on_suggest(self, user_input, items_chain): suggestions = [] try: - results = self._eval(expression) + results = self._eval(self.semicalc(expression)) if not isinstance(results, (tuple, list)): results = (results,) for res in results: diff --git a/README.md b/README.md index 407d3d7..d462b76 100644 --- a/README.md +++ b/README.md @@ -46,4 +46,5 @@ license, which you can find in the `LICENSE` file located in this directory. - Patch to add `Copy...` actions for the `RegBrowser` package * [@DrorHarari](https://github.com/DrorHarari): - Patch to add string reverse and camel-like case changes to the `String` package - - Patch to add variable support to the 'Calc' package \ No newline at end of file + - Patch to add variable support to the 'Calc' package + - Patch to add multi-part calculation to the 'Calc' package (10+20+30;*1.3) From 06b59aaa97ea16569727dcc0273e1a4902e35f53 Mon Sep 17 00:00:00 2001 From: Dror Harari Date: Sat, 19 Feb 2022 02:40:37 +0200 Subject: [PATCH 2/2] Update calc.py Ignore tailing ; for calculations (previously, this would have shown no result) --- Calc/calc.py | 2 ++ 1 file changed, 2 insertions(+) diff --git a/Calc/calc.py b/Calc/calc.py index e62a900..dc60c6c 100644 --- a/Calc/calc.py +++ b/Calc/calc.py @@ -420,6 +420,8 @@ def semicalc(self, expression): return expression elif semipos == 0: expression = expression[1:] + elif semipos + 1 == len(expression): + expression = expression[0:-1] elif len(expression) > semipos and (expression[semipos+1].isalnum() or expression[semipos+1] in "(){}[]"): return f"error: {expression}" else: