Skip to content

Commit

Permalink
Added file rename command
Browse files Browse the repository at this point in the history
  • Loading branch information
Joshua Barretto committed Apr 26, 2024
1 parent e3596b7 commit 2b9c40b
Show file tree
Hide file tree
Showing 2 changed files with 39 additions and 0 deletions.
9 changes: 9 additions & 0 deletions lib/howl/buffer.moon
Original file line number Diff line number Diff line change
Expand Up @@ -248,6 +248,15 @@ class Buffer extends PropertyObject
@_associate_with_file file
@save!

rename: (file) =>
old_file = @file
@_associate_with_file file
@save!
if @file != old_file
status, err = pcall old_file\delete
if not status
log.warn "Failed to delete old alias #{old_file} for #{@file}: #{err}"

as_one_undo: (f) => @_buffer\as_one_undo f

undo: =>
Expand Down
30 changes: 30 additions & 0 deletions lib/howl/commands/file_commands.moon
Original file line number Diff line number Diff line change
Expand Up @@ -148,6 +148,36 @@ command.register
log.info ("%s: %d lines, %d bytes written")\format buffer.file.basename,
#buffer.lines, #buffer

command.register
name: 'rename',
description: 'Rename the current buffer\'s file'
input: (opts) ->
parent = app.editor.buffer.file and app.editor.buffer.file.parent
file = interact.select_file prompt: ':rename ', text: opts.text, allow_new: true, path: parent and parent.path
return unless file

if file.exists
unless interact.yes_or_no prompt: "File '#{file}' already exists, overwrite? "
log.info "Not overwriting; buffer not renamed"
return

unless auto_mkdir file.parent
log.info "Parent directory doesn't exist; buffer not renamed"
return

return file

handler: (file) ->
buffer = app.editor.buffer
if not buffer.file
command.run 'save-as'
return

buffer\rename file
buffer.mode = mode.for_file file
log.info ("%s: %d lines, %d bytes written")\format buffer.file.basename,
#buffer.lines, #buffer

command.register
name: 'buffer-close',
description: 'Close the current buffer'
Expand Down

0 comments on commit 2b9c40b

Please sign in to comment.