From 8d62e382f4118cc9364b185bb28c6e72fea9efe8 Mon Sep 17 00:00:00 2001 From: Dev Vishnu Date: Tue, 26 Aug 2025 23:50:19 +0530 Subject: [PATCH] Add compress_clipboard utility for clipboard compression/decompression --- .../scripts/utilities/compress_clipboard.py | 113 ++++++++++++++++++ 1 file changed, 113 insertions(+) create mode 100644 backend/scripts/utilities/compress_clipboard.py diff --git a/backend/scripts/utilities/compress_clipboard.py b/backend/scripts/utilities/compress_clipboard.py new file mode 100644 index 00000000..5f338d66 --- /dev/null +++ b/backend/scripts/utilities/compress_clipboard.py @@ -0,0 +1,113 @@ +import zipfile +import pyperclip +import os +import sys +import time + + +class Colors: + HEADER = '\033[95m' + OKBLUE = '\033[94m' + OKCYAN = '\033[96m' + OKGREEN = '\033[92m' + WARNING = '\033[93m' + FAIL = '\033[91m' + ENDC = '\033[0m' + BOLD = '\033[1m' + +class FileUtility: + + @staticmethod + def compress_files(files, output_zip=None): + + valid_files = [] + for f in files: + if os.path.exists(f): + valid_files.append(f) + else: + print(f"{Colors.WARNING}⚠️ Path not found: {f}{Colors.ENDC}") + + if not valid_files: + raise FileNotFoundError("No valid files or folders provided to compress.") + + + if output_zip is None: + if len(valid_files) == 1: + name = os.path.basename(valid_files[0]) + output_zip = os.path.splitext(name)[0] + ".zip" + else: + output_zip = "archive.zip" + + + all_files = [] + for path in valid_files: + if os.path.isfile(path): + all_files.append((path, os.path.basename(path))) + elif os.path.isdir(path): + for root, _, files_in_dir in os.walk(path): + for file in files_in_dir: + full_path = os.path.join(root, file) + arcname = os.path.relpath(full_path, os.path.dirname(path)) + all_files.append((full_path, arcname)) + + total = len(all_files) + print(f"{Colors.OKBLUE}📦 Compressing {total} item(s) into '{output_zip}'{Colors.ENDC}") + + + with zipfile.ZipFile(output_zip, 'w', zipfile.ZIP_DEFLATED) as zipf: + for idx, (full_path, arcname) in enumerate(all_files, 1): + zipf.write(full_path, arcname=arcname) + progress = int((idx / total) * 30) + bar = '🟩' * progress + '⬜' * (30 - progress) + print(f"\r[{bar}] {idx}/{total} files", end="") + time.sleep(0.01) + + print(f"\n{Colors.OKGREEN}✅ Compression completed!{Colors.ENDC}") + return output_zip + + @staticmethod + def copy_to_clipboard(text): + pyperclip.copy(text) + print(f"{Colors.OKCYAN}📋 Copied to clipboard: '{text}'{Colors.ENDC}") + + +if __name__ == "__main__": + if len(sys.argv) < 2: + print(f"{Colors.BOLD}Usage: python compress_clipboard.py [args]{Colors.ENDC}") + print("Commands:") + print(" compress [file/folder2 ...] [output_zip] - Compress files/folders") + print(" copy - Copy text to clipboard") + sys.exit(1) + + command = sys.argv[1] + + try: + if command == "compress": + if len(sys.argv) < 3: + print("Usage: compress [file/folder2 ...] [output_zip]") + sys.exit(1) + + *paths, last_arg = sys.argv[2:] + output_zip = None + if len(paths) >= 1 and last_arg.lower().endswith(".zip"): + output_zip = last_arg + else: + paths.append(last_arg) + + zip_file = FileUtility.compress_files(paths, output_zip) + FileUtility.copy_to_clipboard(zip_file) + + elif command == "copy": + if len(sys.argv) < 3: + print("Usage: copy ") + sys.exit(1) + + text = " ".join(sys.argv[2:]) + FileUtility.copy_to_clipboard(text) + + else: + print(f"{Colors.FAIL}❌ Unknown command: {command}{Colors.ENDC}") + + except Exception as e: + print(f"{Colors.FAIL}⚠️ Error: {e}{Colors.ENDC}") +