Class: Clacky::Tools::TrashManager
- Defined in:
- lib/clacky/tools/trash_manager.rb
Instance Method Summary collapse
- #empty_trash(trash_dir, days_old, project_root) ⇒ Object
- #execute(action:, file_path: nil, days_old: 7, working_dir: nil) ⇒ Object
- #format_bytes(bytes) ⇒ Object
- #format_call(args) ⇒ Object
- #format_result(result) ⇒ Object
- #format_time(time_str) ⇒ Object
- #get_deleted_files(trash_dir, project_root) ⇒ Object
- #list_deleted_files(trash_dir, project_root) ⇒ Object
- #restore_file(trash_dir, file_path, project_root) ⇒ Object
- #show_help ⇒ Object
- #show_trash_status(trash_dir, project_root) ⇒ Object
Methods inherited from Base
#category, #description, #name, #parameters, #to_function_definition
Instance Method Details
#empty_trash(trash_dir, days_old, project_root) ⇒ Object
195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 |
# File 'lib/clacky/tools/trash_manager.rb', line 195 def empty_trash(trash_dir, days_old, project_root) deleted_files = get_deleted_files(trash_dir, project_root) cutoff_time = Time.now - (days_old * 24 * 60 * 60) old_files = deleted_files.select do |file| Time.parse(file[:deleted_at]) < cutoff_time end if old_files.empty? return { action: 'empty', success: true, deleted_count: 0, message: "šļø No files older than #{days_old} days found in trash" } end deleted_count = 0 freed_size = 0 old_files.each do |file| begin File.delete(file[:trash_file]) if File.exist?(file[:trash_file]) File.delete("#{file[:trash_file]}.metadata.json") if File.exist?("#{file[:trash_file]}.metadata.json") deleted_count += 1 freed_size += file[:file_size] || 0 rescue StandardError => e # Continue processing other files, but log the error end end { action: 'empty', success: true, deleted_count: deleted_count, freed_size: freed_size, days_old: days_old, message: "šļø Permanently deleted #{deleted_count} files older than #{days_old} days\nš¾ Freed up #{format_bytes(freed_size)} of disk space" } end |
#execute(action:, file_path: nil, days_old: 7, working_dir: nil) ⇒ Object
34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 |
# File 'lib/clacky/tools/trash_manager.rb', line 34 def execute(action:, file_path: nil, days_old: 7, working_dir: nil) project_root = working_dir || Dir.pwd # Use global trash directory organized by project trash_directory = Clacky::TrashDirectory.new(project_root) trash_dir = trash_directory.trash_dir unless Dir.exist?(trash_dir) return { action: action, success: false, message: "No trash directory found. No files have been safely deleted yet." } end case action.downcase when 'list' list_deleted_files(trash_dir, project_root) when 'restore' return { action: action, success: false, message: "file_path is required for restore action" } unless file_path restore_file(trash_dir, file_path, project_root) when 'status' show_trash_status(trash_dir, project_root) when 'empty' empty_trash(trash_dir, days_old, project_root) when 'help' show_help else { action: action, success: false, message: "Unknown action: #{action}" } end end |
#format_bytes(bytes) ⇒ Object
300 301 302 303 304 305 306 307 308 309 310 311 312 313 314 315 316 317 |
# File 'lib/clacky/tools/trash_manager.rb', line 300 def format_bytes(bytes) return "0 B" if bytes.zero? units = %w[B KB MB GB] unit_index = 0 size = bytes.to_f while size >= 1024 && unit_index < units.length - 1 size /= 1024.0 unit_index += 1 end if unit_index == 0 "#{size.to_i} #{units[unit_index]}" else "#{size.round(2)} #{units[unit_index]}" end end |
#format_call(args) ⇒ Object
334 335 336 337 |
# File 'lib/clacky/tools/trash_manager.rb', line 334 def format_call(args) action = args[:action] || args['action'] || 'unknown' "TrashManager(#{action})" end |
#format_result(result) ⇒ Object
339 340 341 342 343 344 345 346 347 348 349 350 351 352 353 354 355 356 357 358 359 360 361 362 363 364 365 366 367 368 |
# File 'lib/clacky/tools/trash_manager.rb', line 339 def format_result(result) action = result[:action] || 'unknown' success = result[:success] case action when 'list' count = result[:count] || 0 "š Listed #{count} deleted files" when 'restore' if success "ā»ļø File restored successfully" else "ā Restore failed" end when 'status' count = result[:count] || 0 "š Trash: #{count} files" when 'empty' if success deleted_count = result[:deleted_count] || 0 "šļø Emptied #{deleted_count} files" else "ā Empty failed" end when 'help' "ā Help displayed" else success ? "[OK] #{action} completed" : "[Error] #{action} failed" end end |
#format_time(time_str) ⇒ Object
319 320 321 322 323 324 325 326 327 328 329 330 331 332 |
# File 'lib/clacky/tools/trash_manager.rb', line 319 def format_time(time_str) time = Time.parse(time_str) if time.to_date == Date.today time.strftime("%H:%M") elsif time.to_date == Date.today - 1 "yesterday #{time.strftime('%H:%M')}" elsif time.year == Date.today.year time.strftime("%m/%d %H:%M") else time.strftime("%Y/%m/%d") end rescue time_str end |
#get_deleted_files(trash_dir, project_root) ⇒ Object
273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298 |
# File 'lib/clacky/tools/trash_manager.rb', line 273 def get_deleted_files(trash_dir, project_root) deleted_files = [] Dir.glob(File.join(trash_dir, "*.metadata.json")).each do || begin = JSON.parse(File.read()) trash_file = .sub('.metadata.json', '') # Only include existing trash files if File.exist?(trash_file) deleted_files << { original_path: ['original_path'], deleted_at: ['deleted_at'], trash_file: trash_file, file_size: ['file_size'], file_type: ['file_type'], file_mode: ['file_mode'] } end rescue StandardError # Skip corrupted metadata files end end deleted_files.sort_by { |f| f[:deleted_at] }.reverse end |
#list_deleted_files(trash_dir, project_root) ⇒ Object
66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 |
# File 'lib/clacky/tools/trash_manager.rb', line 66 def list_deleted_files(trash_dir, project_root) deleted_files = get_deleted_files(trash_dir, project_root) if deleted_files.empty? return { action: 'list', success: true, count: 0, message: "šļø Trash is empty" } end file_list = deleted_files.map.with_index(1) do |file, index| size_info = file[:file_size] ? " (#{format_bytes(file[:file_size])})" : "" "#{index}. #{file[:original_path]}#{size_info}\n Deleted: #{format_time(file[:deleted_at])}" end { action: 'list', success: true, count: deleted_files.size, files: deleted_files, message: "šļø Deleted Files:\n\n#{file_list.join("\n\n")}\n\nš” Use trash_manager with action='restore' and file_path='<path>' to restore a file" } end |
#restore_file(trash_dir, file_path, project_root) ⇒ Object
92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 |
# File 'lib/clacky/tools/trash_manager.rb', line 92 def restore_file(trash_dir, file_path, project_root) deleted_files = get_deleted_files(trash_dir, project_root) = File.(file_path, project_root) target_file = deleted_files.find { |f| f[:original_path] == } unless target_file similar_files = deleted_files.select { |f| File.basename(f[:original_path]) == File.basename(file_path) } if similar_files.any? suggestions = similar_files.map { |f| f[:original_path] }.join("\n - ") return { action: 'restore', success: false, message: "File not found in trash: #{file_path}\n\nDid you mean one of these?\n - #{suggestions}" } else return { action: 'restore', success: false, message: "File not found in trash: #{file_path}\n\nUse trash_manager with action='list' to see available files." } end end if File.exist?() return { action: 'restore', success: false, message: "Cannot restore: file already exists at #{file_path}" } end begin # Ensure target directory exists FileUtils.mkdir_p(File.dirname()) # Restore file FileUtils.mv(target_file[:trash_file], ) File.delete("#{target_file[:trash_file]}.metadata.json") { action: 'restore', success: true, restored_file: , message: "ā Successfully restored: #{file_path}" } rescue StandardError => e { action: 'restore', success: false, message: "ā Failed to restore file: #{e.}" } end end |
#show_help ⇒ Object
236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 |
# File 'lib/clacky/tools/trash_manager.rb', line 236 def show_help help_text = <<~HELP šļø Trash Manager Help The SafeShell tool automatically moves deleted files to a trash directory instead of permanently deleting them. This tool helps you manage those files. Available actions: š list - Show all deleted files Example: trash_manager(action="list") ā»ļø restore - Restore a deleted file to its original location Example: trash_manager(action="restore", file_path="path/to/file.txt") š status - Show trash summary with statistics Example: trash_manager(action="status") šļø empty - Permanently delete files older than N days (default: 7) Example: trash_manager(action="empty", days_old=7) ā help - Show this help message š” Tips: - Use 'list' to see what files are in trash - Use 'restore' to get back accidentally deleted files - Use 'empty' periodically to free up disk space - All deletions by SafeShell are logged in ~/.clacky/safety_logs/ HELP { action: 'help', success: true, message: help_text } end |
#show_trash_status(trash_dir, project_root) ⇒ Object
148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 |
# File 'lib/clacky/tools/trash_manager.rb', line 148 def show_trash_status(trash_dir, project_root) deleted_files = get_deleted_files(trash_dir, project_root) total_size = deleted_files.sum { |f| f[:file_size] || 0 } if deleted_files.empty? return { action: 'status', success: true, count: 0, total_size: 0, message: "šļø Trash is empty" } end # Group by file type by_type = deleted_files.group_by { |f| f[:file_type] || 'no extension' } type_summary = by_type.map do |ext, files| size = files.sum { |f| f[:file_size] || 0 } " #{ext}: #{files.size} files (#{format_bytes(size)})" end.join("\n") recent_files = deleted_files.first(3).map do |file| " - #{File.basename(file[:original_path])} (#{format_time(file[:deleted_at])})" end.join("\n") = [] << "šļø Trash Status:" << " Files: #{deleted_files.count}" << " Total size: #{format_bytes(total_size)}" << " Location: #{trash_dir}" << "" << "š By file type:" << type_summary << "" << "š Recently deleted:" << recent_files { action: 'status', success: true, count: deleted_files.size, total_size: total_size, by_type: by_type.transform_values(&:size), message: .join("\n") } end |