Skip to content

Commit

Permalink
Merge pull request #1296 from SierraSoftworks/feat/scratch-ref
Browse files Browse the repository at this point in the history
feat: Add support for resolving scratchpads relative to the current week
  • Loading branch information
notheotherben authored Dec 5, 2024
2 parents ae8f76c + 98c759f commit 31800bd
Show file tree
Hide file tree
Showing 3 changed files with 58 additions and 2 deletions.
3 changes: 3 additions & 0 deletions src/commands/scratch.rs
Original file line number Diff line number Diff line change
Expand Up @@ -72,6 +72,9 @@ impl CommandRunnable for ScratchCommand {
skip(self, core, completer, _matches)
)]
async fn complete(&self, core: &Core, completer: &Completer, _matches: &ArgMatches) {
let time = chrono::Local::now();
completer.offer(time.format("%Yw%V").to_string());

completer.offer_many(core.config().get_apps().map(|a| a.get_name()));

if let Ok(pads) = core.resolver().get_scratchpads() {
Expand Down
3 changes: 2 additions & 1 deletion src/completion/completer.rs
Original file line number Diff line number Diff line change
Expand Up @@ -18,7 +18,8 @@ impl Completer {
}
}

pub fn offer(&self, completion: &str) {
pub fn offer<S: AsRef<str>>(&self, completion: S) {
let completion = completion.as_ref();
if !matches(completion, &self.filter) {
return;
}
Expand Down
54 changes: 53 additions & 1 deletion src/core/resolver.rs
Original file line number Diff line number Diff line change
Expand Up @@ -68,6 +68,22 @@ impl Resolver for TrueResolver {

#[tracing::instrument(err, skip(self, name))]
fn get_scratchpad(&self, name: &str) -> Result<Scratchpad, Error> {
if name.starts_with('^') || name.starts_with('~') {
let delta = name[1..].parse::<u64>().map_err(|err| {
errors::user(
&format!(
"Could not parse the offset expression '{}' into a valid week offset: {}.",
&name, err,
),
"Please provide a valid number of weeks to go back in time.",
)
})?;

let time = Local::now() - chrono::Duration::days(delta as i64 * 7);

return self.get_scratchpad(&time.format("%Yw%V").to_string());
}

Ok(Scratchpad::new(
name,
self.config.get_scratch_directory().join(name),
Expand Down Expand Up @@ -216,7 +232,7 @@ impl TrueResolver {
repo_from_svc_and_path(&self.config, Some(svc_name), relative_path.strip_prefix(svc).unwrap_or(relative_path), false)
},
Err(e) => Err(errors::system_with_internal(
"We were unable to determine the repository's fully qualified name.",
"We were unable to determine the repository's fully qualified name.",
&format!("Make sure that you are currently within a repository contained within your development directory ('{}').", dev_dir.display()),
e))
}
Expand Down Expand Up @@ -375,6 +391,42 @@ mod tests {
);
}

#[test]
fn get_scratchpad_offset() {
let resolver = get_resolver();

let example = resolver.get_scratchpad("^0").unwrap();
assert_eq!(
example.get_path(),
get_dev_dir()
.join("scratch")
.join(Local::now().format("%Yw%V").to_string())
);

let example = resolver.get_scratchpad("^1").unwrap();
assert_eq!(
example.get_path(),
get_dev_dir().join("scratch").join(
(Local::now() - chrono::Duration::days(7))
.format("%Yw%V")
.to_string()
)
);

let example = resolver.get_scratchpad("^5").unwrap();
assert_eq!(
example.get_path(),
get_dev_dir().join("scratch").join(
(Local::now() - chrono::Duration::days(7 * 5))
.format("%Yw%V")
.to_string()
)
);

assert!(resolver.get_scratchpad("^not-a-number").is_err());
assert!(resolver.get_scratchpad("^-1").is_err());
}

#[test]
fn get_current_scratchpad() {
let resolver = get_resolver();
Expand Down

0 comments on commit 31800bd

Please sign in to comment.