keep updated the last_checkout_id when moving between sheets

This commit is contained in:
Abraham Toriz 2021-07-27 12:20:58 -05:00
parent 39bc7abf0d
commit 9832945005
No known key found for this signature in database
GPG Key ID: D5B4A746DB5DD42A
3 changed files with 23 additions and 15 deletions

View File

@ -43,8 +43,8 @@ impl<'a> Command<'a> for ResumeCommand {
if let Some(entry_id) = entry_id {
if let Some(entry) = db.entry_by_id(entry_id)? {
writeln!(out, "Resuming \"{}\" from entry #{}", entry.note.as_ref().unwrap_or(&"".to_owned()), entry.id)?;
r#in::InCommand::handle(r#in::Args {
at: args.at,
note: entry.note,

View File

@ -40,31 +40,35 @@ impl<'a> Command<'a> for SheetCommand {
let current_sheet = db.current_sheet()?.unwrap_or("default".into());
// sheet given, switch to it
if sheet == "-" {
let move_to = if sheet == "-" {
if let Some(move_to) = db.last_sheet()? {
db.set_last_sheet(&current_sheet)?;
db.set_current_sheet(&move_to)?;
writeln!(out, "Switching to sheet '{}'", move_to)?;
Ok(())
move_to
} else {
writeln!(out, "No previous sheet to move to. Staying on '{}'.\nHint: remember that giving - (a dash) as argument to t sheet switches to the last active sheet", current_sheet)?;
writeln!(out, "No previous sheet to move to. Staying on '{}'.
Hint: remember that giving - (a dash) as argument to t sheet switches to the last active sheet", current_sheet)?;
Ok(())
return Ok(());
}
} else {
if sheet == current_sheet {
writeln!(out, "Already on sheet '{}'", sheet)?;
return Ok(());
} else {
db.set_last_sheet(&current_sheet)?;
db.set_current_sheet(&sheet)?;
writeln!(out, "Switching to sheet '{}'", sheet)?;
sheet
}
};
Ok(())
db.set_last_sheet(&current_sheet)?;
db.set_current_sheet(&move_to)?;
if let Some(entry) = db.last_checkout_of_sheet(&move_to)? {
db.set_last_checkout_id(entry.id)?;
}
writeln!(out, "Switching to sheet '{}'", move_to)?;
Ok(())
} else {
// call list
ListCommand::handle(Default::default(), db, out, err, config, now)

View File

@ -165,6 +165,10 @@ pub trait Database {
Ok(self.entry_query("select * from entries where end is null and sheet=?1", &[&sheet])?.into_iter().next())
}
fn last_checkout_of_sheet(&self, sheet: &str) -> Result<Option<Entry>> {
Ok(self.entry_query("select * from entries where end is not null and sheet=?1 order by end desc limit 1", &[&sheet])?.into_iter().next())
}
// Meta queries
fn current_sheet(&self) -> Result<Option<String>> {
let results = self.meta_query("select * from meta where key='current_sheet'", &[])?;