tiempo-rs/src/commands/sheet.rs

132 lines
3.8 KiB
Rust
Raw Normal View History

2021-07-01 23:44:38 -05:00
use std::convert::TryFrom;
2021-08-25 14:30:27 -05:00
use std::io::{BufRead, Write};
2021-07-01 23:44:38 -05:00
use clap::ArgMatches;
use crate::database::Database;
2021-07-14 12:37:08 -05:00
use crate::error::{Error, Result};
2021-08-25 14:30:27 -05:00
use crate::commands::{Command, Facts};
use crate::commands::list::ListCommand;
2021-08-25 14:30:27 -05:00
use crate::io::Streams;
2021-07-01 23:44:38 -05:00
#[derive(Default)]
pub struct Args {
2021-07-28 20:51:33 -05:00
pub sheet: Option<String>,
2021-07-01 23:44:38 -05:00
}
impl<'a> TryFrom<&'a ArgMatches<'a>> for Args {
2021-07-14 12:37:08 -05:00
type Error = Error;
2021-07-01 23:44:38 -05:00
2021-07-14 12:37:08 -05:00
fn try_from(matches: &'a ArgMatches) -> Result<Self> {
2021-07-01 23:44:38 -05:00
Ok(Args {
sheet: matches.value_of("sheet").map(|s| s.into()),
})
}
}
pub struct SheetCommand {}
impl<'a> Command<'a> for SheetCommand {
type Args = Args;
2021-08-25 14:30:27 -05:00
fn handle<D, I, O, E>(args: Args, streams: &mut Streams<D, I, O, E>, facts: &Facts) -> Result<()>
2021-07-01 23:44:38 -05:00
where
D: Database,
2021-08-25 14:30:27 -05:00
I: BufRead,
2021-07-01 23:44:38 -05:00
O: Write,
E: Write,
{
2021-07-23 20:37:21 -05:00
if let Some(sheet) = args.sheet {
2021-08-25 14:30:27 -05:00
let current_sheet = streams.db.current_sheet()?.unwrap_or_else(|| "default".into());
2021-07-23 20:37:21 -05:00
2021-07-14 12:37:08 -05:00
// sheet given, switch to it
let move_to = if sheet == "-" {
2021-08-25 14:30:27 -05:00
if let Some(move_to) = streams.db.last_sheet()? {
move_to
2021-07-23 20:37:21 -05:00
} else {
2021-08-25 14:30:27 -05:00
writeln!(streams.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)?;
2021-07-23 20:37:21 -05:00
return Ok(());
2021-07-23 20:37:21 -05:00
}
2021-08-02 19:10:34 -05:00
} else if sheet == current_sheet {
2021-08-25 14:30:27 -05:00
writeln!(streams.out, "Already on sheet '{}'", sheet)?;
2021-07-23 20:37:21 -05:00
2021-08-02 19:10:34 -05:00
return Ok(());
} else {
sheet
};
2021-07-23 20:37:21 -05:00
2021-08-25 14:30:27 -05:00
streams.db.set_last_sheet(&current_sheet)?;
streams.db.set_current_sheet(&move_to)?;
2021-08-25 14:30:27 -05:00
writeln!(streams.out, "Switching to sheet '{}'", move_to)?;
Ok(())
2021-07-14 12:37:08 -05:00
} else {
// call list
2021-08-25 14:30:27 -05:00
ListCommand::handle(Default::default(), streams, facts)
2021-07-14 12:37:08 -05:00
}
2021-07-01 23:44:38 -05:00
}
}
#[cfg(test)]
mod tests {
2021-07-23 20:37:21 -05:00
use pretty_assertions::assert_eq;
2021-08-03 20:56:40 -05:00
use crate::test_utils::Ps;
2021-07-23 20:37:21 -05:00
use super::*;
2021-07-01 23:44:38 -05:00
#[test]
fn switch_to_sheet() {
2021-07-23 20:37:21 -05:00
let args = Args {
sheet: Some("new_sheet".into()),
};
2021-08-25 14:30:27 -05:00
let mut streams = Streams::fake(b"");
let facts = Facts::new();
2021-07-23 20:37:21 -05:00
2021-08-25 14:30:27 -05:00
SheetCommand::handle(args, &mut streams, &facts).unwrap();
2021-07-23 20:37:21 -05:00
2021-08-25 14:30:27 -05:00
assert_eq!(streams.db.current_sheet().unwrap().unwrap(), "new_sheet");
assert_eq!(Ps(&String::from_utf8_lossy(&streams.out)), Ps("Switching to sheet 'new_sheet'\n"));
assert_eq!(Ps(&String::from_utf8_lossy(&streams.err)), Ps(""));
2021-07-01 23:44:38 -05:00
}
#[test]
fn switch_to_sheet_already_in() {
2021-07-23 20:37:21 -05:00
let args = Args {
sheet: Some("foo".into()),
};
2021-08-25 14:30:27 -05:00
let mut streams = Streams::fake(b"");
let facts = Facts::new();
2021-07-23 20:37:21 -05:00
2021-08-25 14:30:27 -05:00
streams.db.set_current_sheet("foo").unwrap();
2021-07-23 20:37:21 -05:00
2021-08-25 14:30:27 -05:00
SheetCommand::handle(args, &mut streams, &facts).unwrap();
2021-07-23 20:37:21 -05:00
2021-08-25 14:30:27 -05:00
assert_eq!(streams.db.current_sheet().unwrap().unwrap(), "foo");
assert_eq!(Ps(&String::from_utf8_lossy(&streams.out)), Ps("Already on sheet 'foo'\n"));
assert_eq!(Ps(&String::from_utf8_lossy(&streams.err)), Ps(""));
2021-07-23 20:37:21 -05:00
}
#[test]
fn switch_to_last_sheet() {
let args = Args {
sheet: Some("-".into()),
};
2021-08-25 14:30:27 -05:00
let mut streams = Streams::fake(b"");
let facts = Facts::new();
2021-07-23 20:37:21 -05:00
2021-08-25 14:30:27 -05:00
streams.db.set_current_sheet("foo").unwrap();
streams.db.set_last_sheet("var").unwrap();
2021-07-23 20:37:21 -05:00
2021-08-25 14:30:27 -05:00
SheetCommand::handle(args, &mut streams, &facts).unwrap();
2021-07-23 20:37:21 -05:00
2021-08-25 14:30:27 -05:00
assert_eq!(streams.db.current_sheet().unwrap().unwrap(), "var");
assert_eq!(streams.db.last_sheet().unwrap().unwrap(), "foo");
assert_eq!(Ps(&String::from_utf8_lossy(&streams.out)), Ps("Switching to sheet 'var'\n"));
assert_eq!(Ps(&String::from_utf8_lossy(&streams.err)), Ps(""));
2021-07-01 23:44:38 -05:00
}
}