#!/usr/bin/env python3 import argparse import os import sys from datetime import datetime CLIPKEEPER_DIR = os.path.expanduser('~/.clipkeeper') CLIPKEEPER_FILE = os.path.join(CLIPKEEPER_DIR, 'clips.txt') def create_clipkeeper_dir(): if not os.path.exists(CLIPKEEPER_DIR): os.makedirs(CLIPKEEPER_DIR) def save_clip(clip): create_clipkeeper_dir() with open(CLIPKEEPER_FILE, 'a') as f: f.write(f'{datetime.now().strftime("%Y-%m-%d %H:%M:%S")}\n{clip}\n\n') def get_clips(): try: with open(CLIPKEEPER_FILE, 'r') as f: return f.read().split('\n\n') except FileNotFoundError: return [] def list_clips(clips): for i, clip in enumerate(clips): print(f'\033[94mClip {i+1}:\033[0m') print(clip) def recall_clip(clips, index): try: clip = clips[index - 1] print(f'\033[92mRecalled Clip:\033[0m') print(clip) except IndexError: print('\033[91mInvalid clip index.\033[0m') def delete_clip(clips, index): try: del clips[index - 1] with open(CLIPKEEPER_FILE, 'w') as f: for clip in clips: f.write(clip + '\n\n') print('\033[92mClip deleted successfully.\033[0m') except IndexError: print('\033[91mInvalid clip index.\033[0m') def main(): parser = argparse.ArgumentParser(description='Clipkeeper: A terminal-based clipboard history manager.') subparsers = parser.add_subparsers(dest='command') save_parser = subparsers.add_parser('save', help='Save a clip') save_parser.add_argument('clip', help='The clip to save') list_parser = subparsers.add_parser('list', help='List all clips') recall_parser = subparsers.add_parser('recall', help='Recall a clip by index') recall_parser.add_argument('index', type=int, help='The index of the clip to recall') delete_parser = subparsers.add_parser('delete', help='Delete a clip by index') delete_parser.add_argument('index', type=int, help='The index of the clip to delete') if len(sys.argv) == 1: parser.print_help() sys.exit(1) args = parser.parse_args() if args.command == 'save': save_clip(args.clip) print('\033[92mClip saved successfully.\033[0m') elif args.command == 'list': clips = get_clips() if clips: list_clips(clips) else: print('\033[91mNo clips found.\033[0m') elif args.command == 'recall': clips = get_clips() recall_clip(clips, args.index) elif args.command == 'delete': clips = get_clips() delete_clip(clips, args.index) if __name__ == '__main__': main()