- commit
- cbf25115ab61bb7042b8c1f2bcf255ce0bc9d7ca
- parent
- 14fadda95c58a20736ff44425481daa9d86d1123
- Author
- Tobias Bengfort <tobias.bengfort@posteo.de>
- Date
- 2023-05-29 15:50
implement optimize
Diffstat
M | python/pad/__init__.py | 39 | +++++++++++++++++++++++++++++---------- |
1 files changed, 29 insertions, 10 deletions
diff --git a/python/pad/__init__.py b/python/pad/__init__.py
@@ -38,6 +38,7 @@ class Pad: 38 38 self.local_changes = [] 39 39 self.staged_changes = [] 40 40 self.old = self.get_text() -1 41 self.last_event_id = 0 41 42 42 43 self.name = os.path.basename(buffer.name).removesuffix('.pad') 43 44 self.url = BASE_URL + self.name @@ -106,6 +107,19 @@ class Pad: 106 107 mod = bool(self.local_changes or self.staged_changes) 107 108 self.buffer.options['modified'] = mod 108 109 -1 110 def optimize(self): -1 111 text = self.get_text() -1 112 my_changes = [*self.staged_changes, *self.local_changes] -1 113 -1 114 for change in reversed(my_changes): -1 115 text = diff.unapply(text, change) -1 116 -1 117 change = diff.diff('', text, 3) -1 118 data = [self.id, 'changes', [change]] -1 119 session.put(self.url, json=data, headers={ -1 120 'Last-Event-ID': self.last_event_id -1 121 }) -1 122 109 123 110 124 def get_buffer(): 111 125 return int(vim.eval('expand("<abuf>")'), 10) @@ -135,13 +149,18 @@ def on_close(): 135 149 136 150 137 151 def on_channel(i, msg):138 -1 if not msg.startswith('data: '):139 -1 return140 -1 data = json.loads(msg.split(': ', 1)[1])141 -1 if data[1] == 'changes':142 -1 pad = pads[i]143 -1 if data[0] == pad.id:144 -1 pad.staged_changes = []145 -1 else:146 -1 pad.apply_changes(data[2])147 -1 pad.reset_modified()-1 152 pad = pads[i] -1 153 -1 154 if msg.startswith('data: '): -1 155 data = json.loads(msg.split(': ', 1)[1]) -1 156 if data[1] == 'changes': -1 157 if data[0] == pad.id: -1 158 pad.staged_changes = [] -1 159 else: -1 160 pad.apply_changes(data[2]) -1 161 pad.reset_modified() -1 162 -1 163 if (random.random() < 0.05): -1 164 pad.optimize() -1 165 elif msg.startswith('id: '): -1 166 pad.last_event_id = msg.split(': ', 1)[1]