summaryrefslogtreecommitdiff
path: root/src/exercise.rs
diff options
context:
space:
mode:
authormo8it <mo8it@proton.me>2024-04-14 02:41:19 +0200
committermo8it <mo8it@proton.me>2024-04-14 02:41:19 +0200
commitbee62c89de09fdd9823cba81e07f0f8528fe8ef9 (patch)
treebca32899b02862936905ed84624ebd3af2b0e691 /src/exercise.rs
parent5c0073a9485c4226e58b657cb49628919a28a942 (diff)
Add terminal links
Diffstat (limited to 'src/exercise.rs')
-rw-r--r--src/exercise.rs34
1 files changed, 31 insertions, 3 deletions
diff --git a/src/exercise.rs b/src/exercise.rs
index c5ece5f..2ec8d97 100644
--- a/src/exercise.rs
+++ b/src/exercise.rs
@@ -1,7 +1,8 @@
use anyhow::{Context, Result};
+use crossterm::style::{style, StyledContent, Stylize};
use std::{
fmt::{self, Display, Formatter},
- path::Path,
+ fs,
process::{Command, Output},
};
@@ -10,11 +11,32 @@ use crate::{
info_file::Mode,
};
+pub struct TerminalFileLink<'a> {
+ path: &'a str,
+}
+
+impl<'a> Display for TerminalFileLink<'a> {
+ fn fmt(&self, f: &mut Formatter<'_>) -> fmt::Result {
+ if let Ok(Some(canonical_path)) = fs::canonicalize(self.path)
+ .as_deref()
+ .map(|path| path.to_str())
+ {
+ write!(
+ f,
+ "\x1b]8;;file://{}\x1b\\{}\x1b]8;;\x1b\\",
+ canonical_path, self.path,
+ )
+ } else {
+ write!(f, "{}", self.path,)
+ }
+ }
+}
+
pub struct Exercise {
// Exercise's unique name
pub name: &'static str,
// Exercise's path
- pub path: &'static Path,
+ pub path: &'static str,
// The mode of the exercise
pub mode: Mode,
// The hint text associated with the exercise
@@ -60,10 +82,16 @@ impl Exercise {
.write_exercise_to_disk(self.path, WriteStrategy::Overwrite)
.with_context(|| format!("Failed to reset the exercise {self}"))
}
+
+ pub fn terminal_link(&self) -> StyledContent<TerminalFileLink<'_>> {
+ style(TerminalFileLink { path: self.path })
+ .underlined()
+ .blue()
+ }
}
impl Display for Exercise {
fn fmt(&self, f: &mut Formatter) -> fmt::Result {
- Display::fmt(&self.path.display(), f)
+ self.path.fmt(f)
}
}