|
| 1 | +import os |
| 2 | +import sys |
| 3 | +import random |
| 4 | +import shutil |
| 5 | + |
| 6 | +TODO_FILE = '/tmp/todo.tmp' # Tasks stored in this temporary file |
| 7 | +#WIDTH = 80 # Terminal width |
| 8 | +WIDTH, _ = shutil.get_terminal_size() # Get the actual terminal width |
| 9 | +BACKGROUND_COLORS = [ |
| 10 | + "\033[41m", # Red |
| 11 | + "\033[42m", # Green |
| 12 | + "\033[43m", # Yellow |
| 13 | + "\033[44m", # Blue |
| 14 | + "\033[45m", # Magenta |
| 15 | + "\033[46m", # Cyan |
| 16 | + "\033[47m", # White |
| 17 | + "\033[49m", # Default |
| 18 | +] |
| 19 | + |
| 20 | +# Function to load tasks from the TODO file |
| 21 | +def load_tasks(): |
| 22 | + tasks_by_date = {} |
| 23 | + task_idx = 0 |
| 24 | + |
| 25 | + if not os.path.exists(TODO_FILE): |
| 26 | + return tasks_by_date |
| 27 | + |
| 28 | + with open(TODO_FILE, 'r') as file: |
| 29 | + for line in file: |
| 30 | + line = line.strip() |
| 31 | + if not line: |
| 32 | + continue |
| 33 | + |
| 34 | + try: |
| 35 | + task_color, task_description, task_date = line.split(';') |
| 36 | + task_color = int(task_color) |
| 37 | + except ValueError: |
| 38 | + continue # Skip lines that don't have the correct format |
| 39 | + |
| 40 | + if task_date not in tasks_by_date: |
| 41 | + tasks_by_date[task_date] = [] |
| 42 | + tasks_by_date[task_date].append((task_idx, task_color, task_description)) |
| 43 | + task_idx += 1 |
| 44 | + |
| 45 | + return tasks_by_date |
| 46 | + |
| 47 | +# Function to save tasks to the TODO file |
| 48 | +def save_tasks(tasks_by_date): |
| 49 | + with open(TODO_FILE, "w") as file: |
| 50 | + for date, task_list in tasks_by_date.items(): |
| 51 | + for task in task_list: |
| 52 | + task_idx, color, description = task |
| 53 | + # Save task with a tuple: task_idx, color, description, and date |
| 54 | + file.write(f"{color};{description};{date}\n") |
| 55 | + |
| 56 | +# Function to get background color from color index |
| 57 | +def get_background_color(task_color): |
| 58 | + return BACKGROUND_COLORS[task_color % len(BACKGROUND_COLORS)] |
| 59 | + |
| 60 | +# Function to print tasks with padding |
| 61 | +def print_tasks(index=None): |
| 62 | + tasks_by_date = load_tasks() |
| 63 | + if not tasks_by_date: |
| 64 | + return |
| 65 | + |
| 66 | + sorted_dates = sorted(tasks_by_date.keys(), reverse=True) |
| 67 | + |
| 68 | + if index is not None and index < len(sorted_dates): |
| 69 | + date_to_show = sorted_dates[index] |
| 70 | + else: |
| 71 | + date_to_show = sorted_dates[0] |
| 72 | + |
| 73 | + print_header(date_to_show) |
| 74 | + for task_idx, task_color, task_description in tasks_by_date[date_to_show]: |
| 75 | + print_task(task_idx, task_color, task_description) |
| 76 | + |
| 77 | + if index is None: |
| 78 | + for date in sorted_dates[1:]: |
| 79 | + print_header(date) |
| 80 | + for task_idx, task_color, task_description in tasks_by_date[date]: |
| 81 | + print_task(task_idx, task_color, task_description) |
| 82 | + |
| 83 | +# Function to print the header for a date |
| 84 | +def print_header(date): |
| 85 | + # Calculate leading spaces to center the header |
| 86 | + leading_spaces = (WIDTH - len(date)) // 2 |
| 87 | + padded_header = f"{' ' * leading_spaces}{date}" |
| 88 | + # Define color codes |
| 89 | + white_on_black = "\033[1;37;40m" # White text on black background |
| 90 | + reset = "\033[0m" # Reset color |
| 91 | + # Ensure the header fills the terminal width by padding the right side |
| 92 | + padded_header = padded_header.ljust(WIDTH) |
| 93 | +# print(f"\033[1;30;47m{padded_header}\033[0m") # Black text on gray background, bold if possible |
| 94 | + # Clear the terminal screen (Linux/Unix) |
| 95 | + os.system('clear') |
| 96 | + #print() |
| 97 | + print(f"{white_on_black}{padded_header}{reset}") |
| 98 | + |
| 99 | +# Function to print a task with background color and padding to terminal width |
| 100 | +def print_task(task_idx, task_color, task_description): |
| 101 | + text_color = "\033[30m" # Black text |
| 102 | + bg_color = get_background_color(task_color) |
| 103 | + # Ensure the task description fills the terminal width |
| 104 | + padded_description = task_description.ljust(WIDTH - len(str(task_idx)) - 5) |
| 105 | + # Print task index and description with background color |
| 106 | + print(f"{bg_color}{text_color}{task_idx:3} - {padded_description}\033[0m") |
| 107 | + |
| 108 | +# Function to remove a task by index |
| 109 | +def remove_task(index): |
| 110 | + tasks_by_date = load_tasks() |
| 111 | + all_tasks = [(task_idx, task_color, task_description, task_date) |
| 112 | + for task_date, task_list in tasks_by_date.items() |
| 113 | + for task_idx, task_color, task_description in task_list] |
| 114 | + |
| 115 | + if 0 <= index < len(all_tasks): |
| 116 | + del all_tasks[index] |
| 117 | + print(f"Task {index} removed.") |
| 118 | + save_tasks(all_tasks) |
| 119 | + else: |
| 120 | + print(f"Invalid task index: {index}") |
| 121 | + |
| 122 | +# Function to remove all tasks |
| 123 | +def remove_all_tasks(): |
| 124 | + if os.path.exists(TODO_FILE): |
| 125 | + os.remove(TODO_FILE) |
| 126 | + print("All tasks have been removed.") |
| 127 | + else: |
| 128 | + print("No tasks found.") |
| 129 | + |
| 130 | +# Function to add a task with a description |
| 131 | +def add_task(description, color=None): |
| 132 | + if not description: |
| 133 | + description = input("Enter task description: ") |
| 134 | + |
| 135 | + color = int(input("Color index (0-7): ")) |
| 136 | + if not 0 <= color <= 7: |
| 137 | + color = random.randint(0, len(BACKGROUND_COLORS) - 1) |
| 138 | + |
| 139 | + task_date = "2024-11-01" # Placeholder for the date (you can use dynamic dates here) |
| 140 | + |
| 141 | + # Load existing tasks |
| 142 | + tasks_by_date = load_tasks() |
| 143 | + |
| 144 | + # Add task to the appropriate date group |
| 145 | + if task_date not in tasks_by_date: |
| 146 | + tasks_by_date[task_date] = [] |
| 147 | + |
| 148 | + task_idx = len(tasks_by_date[task_date]) # Use the next available task index |
| 149 | + tasks_by_date[task_date].append((task_idx, color, description)) # Append task to the date group |
| 150 | + |
| 151 | + # Save tasks back to the file |
| 152 | + save_tasks(tasks_by_date) |
| 153 | + print(f"Task added: {description}") |
| 154 | + |
| 155 | +# Function to edit a task description by index |
| 156 | +def edit_task(index): |
| 157 | + tasks_by_date = load_tasks() |
| 158 | + all_tasks = [(task_idx, task_color, task_description, task_date) |
| 159 | + for task_date, task_list in tasks_by_date.items() |
| 160 | + for task_idx, task_color, task_description in task_list] |
| 161 | + |
| 162 | + if 0 <= index < len(all_tasks): |
| 163 | + # Display current description |
| 164 | + _, task_color, current_description, task_date = all_tasks[index] |
| 165 | + print(f"Current description: {current_description}") |
| 166 | + |
| 167 | + # Get new description from user |
| 168 | + new_description = input("Enter new task description: ") |
| 169 | + |
| 170 | + # Update the task in tasks_by_date |
| 171 | + for i, (task_idx, color, description) in enumerate(tasks_by_date[task_date]): |
| 172 | + if task_idx == all_tasks[index][0]: # Find by task index |
| 173 | + tasks_by_date[task_date][i] = (task_idx, task_color, new_description) |
| 174 | + break |
| 175 | + |
| 176 | + save_tasks(tasks_by_date) |
| 177 | + print(f"Task {index} description updated.") |
| 178 | + else: |
| 179 | + print(f"Invalid task index: {index}") |
| 180 | + |
| 181 | +# Function to show the help message |
| 182 | +def show_help(): |
| 183 | + print(""" |
| 184 | +Usage: |
| 185 | + task.py Print every task grouped by date |
| 186 | + task.py <int> Print only the nth date group where nth is the int |
| 187 | + task.py remove <int> Remove the task with the specified index |
| 188 | + task.py remove all Remove all tasks |
| 189 | + task.py add <description> Add a new task with description |
| 190 | + task.py color <int> Change the color of the task at the specified index |
| 191 | +""") |
| 192 | + |
| 193 | +# Main program |
| 194 | +if __name__ == "__main__": |
| 195 | + if len(sys.argv) == 1: |
| 196 | + print_tasks() # Print all tasks if no arguments |
| 197 | + elif sys.argv[1] in ["-h", "--help"]: |
| 198 | + show_help() # Show help |
| 199 | + elif sys.argv[1] == "remove": |
| 200 | + if len(sys.argv) == 3 and sys.argv[2] == "all": |
| 201 | + remove_all_tasks() # Remove all tasks |
| 202 | + elif len(sys.argv) == 3: |
| 203 | + try: |
| 204 | + index = int(sys.argv[2]) |
| 205 | + remove_task(index) # Remove a specific task by index |
| 206 | + except ValueError: |
| 207 | + print("Please provide a valid task index.") |
| 208 | + else: |
| 209 | + print("Usage: task.py remove <int> or task.py remove all") |
| 210 | + elif sys.argv[1] == "edit": |
| 211 | + if len(sys.argv) == 3: |
| 212 | + try: |
| 213 | + index = int(sys.argv[2]) |
| 214 | + edit_task(index) # Edit the task by index |
| 215 | + except ValueError: |
| 216 | + print("Please provide a valid task index.") |
| 217 | + else: |
| 218 | + print("Usage: task.py edit <int>") |
| 219 | + elif sys.argv[1] == "add": |
| 220 | + description = " ".join(sys.argv[2:]) if len(sys.argv) > 2 else None |
| 221 | + add_task(description) # Add a new task |
| 222 | + else: |
| 223 | + print("Unknown command. Use -h or --help for usage.") |
0 commit comments