srsudar / eg

Useful examples at the command line.
MIT License
1.8k stars 98 forks source link

[substitutions] section is being ignored #82

Closed sbienkow closed 4 years ago

sbienkow commented 4 years ago

Substitution from README doesn't work: ~/.egrc

[substitution]
remove-indents = ['^    ', '', True]

Applying following patch:

diff --git a/eg/substitute.py b/eg/substitute.py
index b19f457..8eb006e 100644
--- a/eg/substitute.py
+++ b/eg/substitute.py
@@ -1,6 +1,6 @@
 import re
-
-
+import sys
+print('Imported Substitution', file=sys.stderr)
 class Substitution:
     """
     A substitution to be performed on a piece of text.
@@ -19,6 +19,7 @@ class Substitution:
         self.pattern = pattern
         self.repl = replacement
         self.is_multiline = is_multiline
+        print(self,file=sys.stderr)

     def apply_and_get_result(self, string):
         """
@@ -31,6 +32,12 @@ class Substitution:
             compiled_pattern = re.compile(self.pattern)

         result = re.sub(compiled_pattern, self.repl, string)
+        print(f'Did sub change anything? {result != string}',file=sys.stderr)
+        if result != string:
+            with open(f'orig.{hash(self.repl)}', 'w') as f:
+                f.write(string)
+            with open(f'subbed.{hash(self.repl)}', 'w') as f:
+                f.write(result)
         return result

     def __eq__(self, other):

If eg-config.squeeze = false causes following output:

Imported Substitution

while eg-config.squeeze = true causes:

Imported Substitution
<eg.substitute.Substitution object at 0x7f55c8029730>
<eg.substitute.Substitution object at 0x7f55c7bb63d0>
<eg.substitute.Substitution object at 0x7f55c7bb61c0>
Did sub change anything? True
Did sub change anything? True
Did sub change anything? False

[substitutions] section is completely ignored. Adding and removing entries from it doesn't cause above output to change. This is also confirmed by diffing orig.* and subbed.* files - only change is occasional newline removed (it looks like squeeze is done with Substitution).

Tested on latest git version with ./eg_exec.py ls >/dev/null.