Skip to content

Instantly share code, notes, and snippets.

@LaptopDev
Created April 1, 2025 18:37
Show Gist options
  • Save LaptopDev/eed57090e83665abba4e59604983c8a8 to your computer and use it in GitHub Desktop.
Save LaptopDev/eed57090e83665abba4e59604983c8a8 to your computer and use it in GitHub Desktop.
a python script to stitch together uncopyable ncurses buffer dumps
#This was made for stitching uncopyable ncurses buffers textdumped to files
#Expects correct ordering of input, i.e.: pane1, pane2, pane3, etc.
#Accepts UTF-8 plain text files There is no restriction on file extension, but
# dumpfile names must regex match by 'pane\d+'
#If a proceeding pane contains completely new content it will be appended in full and in order,
# ortherwise it will append only non-overlapping tail of the next pane, effectively deduplicating overlaps.
#!/usr/bin/env python3
import os
import re
import sys
def load_dumps(dir_path):
files = sorted(
[f for f in os.listdir(dir_path) if re.match(r'pane\d+', f)],
key=lambda x: int(re.search(r'\d+', x).group())
)
return [open(os.path.join(dir_path, f), encoding='utf-8').read() for f in files]
def smart_overlap(a_lines, b_lines, min_match=3):
best = (0, 0) # (match_len, b_start_index)
for b_start in range(len(b_lines)):
match_len = 0
a_index = max(0, len(a_lines) - (len(b_lines) - b_start))
while (a_index + match_len < len(a_lines) and
b_start + match_len < len(b_lines) and
a_lines[a_index + match_len] == b_lines[b_start + match_len]):
match_len += 1
# Ensure we're not matching all of b_lines (only overlap with continuation)
if match_len >= min_match and b_start + match_len < len(b_lines):
if match_len > best[0]:
best = (match_len, b_start)
# Return index to start appending from
return best[1] + best[0] if best[0] > 0 else 0
def smart_stitch(dumps):
lines = dumps[0].splitlines()
for dump in dumps[1:]:
new_lines = dump.splitlines()
cutoff = smart_overlap(lines, new_lines)
lines += new_lines[cutoff:]
return '\n'.join(lines) + '\n'
def main():
if len(sys.argv) != 2:
print("Usage: stitch.py /path/to/dumps")
sys.exit(1)
dir_path = sys.argv[1]
dumps = load_dumps(dir_path)
stitched = smart_stitch(dumps)
print(stitched)
if __name__ == '__main__':
main()
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment