Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Implement rename subcommand #16

Draft
wants to merge 1 commit into
base: master
Choose a base branch
from
Draft
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
52 changes: 52 additions & 0 deletions src/handler.rs
Original file line number Diff line number Diff line change
Expand Up @@ -305,4 +305,56 @@ impl Handler {

Ok(())
}

/// Rename a note
///
/// Note:
/// this will run through each Zettelkasten note
/// and ensure that the links are updated to accomodate
/// this name change.
pub fn rename(&self, note: &str, name: &str) -> Result<(), Error> {
let candidates = self.directory.find(note)?;

// if there's only one candidate note, rename the note, change all
// references to the note and return
if candidates.len() == 1 {
let candidate = candidates.first().unwrap();

let id = &candidate.id.to_string();

let new_id = &candidate.id.update(name);

for note in self.directory.notes()? {
if note.has_link(id) {
note.remove_link(id)?;
note.add_link(new_id)?;
}
}

// rename the file on disk
candidate.rename(&self.directory.path, NoteId::new(name, &candidate.id.ext))?;

return Ok(());
}

// rename each selected note with `name`, and change each
// reference to the note
for note in Search::new(candidates).run()? {
let id = &note.id.to_string();

let new_id = &note.id.update(name);

for note in self.directory.notes()? {
if note.has_link(id) {
note.remove_link(id)?;
note.add_link(new_id)?;
}
}

// rename the file on disk
note.rename(&self.directory.path, NoteId::new(name, &note.id.ext))?;
}

Ok(())
}
}
23 changes: 23 additions & 0 deletions src/note.rs
Original file line number Diff line number Diff line change
Expand Up @@ -211,6 +211,14 @@ impl Note {

Ok(Note::new(self.path.to_owned()))
}

/// Attempts to rename the current notes file on disk
pub fn rename(&self, path: &Path, id: NoteId) -> Result<Note, Error> {
// need path/to/id.md -> path/to/new-id.md
let new_path = &path.join(id.to_string());
fs::rename(&self.path, new_path).context(error::Io)?;
Ok(Note::new(new_path.to_owned()))
}
}

#[cfg(test)]
Expand Down Expand Up @@ -277,4 +285,19 @@ mod tests {
assert!(!a.has_tag("software"));
});
}

#[test]
fn test_rename() {
in_temp_dir!({
let a = create(&NoteId::new("a", "md")).unwrap();

let b = a
.rename(&env::current_dir().unwrap(), NoteId::new("b", "md"))
.unwrap();

assert_eq!(b.id.prefix, a.id.prefix);
assert_eq!(b.id.name, "b");
assert_eq!(b.id.ext, a.id.ext);
});
}
}
20 changes: 20 additions & 0 deletions src/note_id.rs
Original file line number Diff line number Diff line change
Expand Up @@ -48,6 +48,11 @@ impl NoteId {
ext: ext.to_owned(),
})
}

/// Replaces the current `name` with `name` and returns the result.
pub fn update(&self, name: &str) -> String {
format!("{}-{}.{}", self.prefix, name, self.ext)
}
}

#[cfg(test)]
Expand Down Expand Up @@ -75,4 +80,19 @@ mod tests {
assert_eq!(id.ext, ext);
}
}

#[test]
fn test_update() {
let cases = vec![
("123-a.md", "b", "123-b.md"),
("93048-name.md", "new-name", "93048-new-name.md"),
];

for case in cases {
let (curr, new, want) = case;
let id = NoteId::parse(curr).unwrap();
let have = id.update(new);
assert_eq!(have, want);
}
}
}
5 changes: 5 additions & 0 deletions src/opt.rs
Original file line number Diff line number Diff line change
Expand Up @@ -46,6 +46,10 @@ pub enum Opt {
#[structopt(name = "explore", alias = "e")]
/// Explore note links
Explore { name: String },

#[structopt(name = "rename", alias = "re")]
/// Rename a note
Rename { note: String, name: String },
}

impl Opt {
Expand All @@ -69,6 +73,7 @@ impl Opt {
Opt::Tag { name, tag } => handler.tag(&name, &tag)?,
Opt::RemoveTag { name, tag } => handler.remove_tag(&name, &tag)?,
Opt::Explore { name } => handler.explore(&name)?,
Opt::Rename { note, name } => handler.rename(&note, &name)?,
}

Ok(())
Expand Down