Skip to content

Commit

Permalink
feat: automatically hide hints after displayed once (#2807)
Browse files Browse the repository at this point in the history
  • Loading branch information
jdx authored Oct 25, 2024
1 parent 197e4ac commit dc76b03
Show file tree
Hide file tree
Showing 4 changed files with 74 additions and 53 deletions.
11 changes: 11 additions & 0 deletions src/file.rs
Original file line number Diff line number Diff line change
Expand Up @@ -198,6 +198,17 @@ pub fn replace_path<P: AsRef<Path>>(path: P) -> PathBuf {
}
}

pub fn touch_file(file: &Path) -> Result<()> {
if !file.exists() {
create(file)?;
return Ok(());
}
trace!("touch_file {}", file.display());
let now = FileTime::now();
set_file_times(file, now, now)
.wrap_err_with(|| format!("failed to touch file: {}", display_path(file)))
}

pub fn touch_dir(dir: &Path) -> Result<()> {
trace!("touch {}", dir.display());
let now = FileTime::now();
Expand Down
59 changes: 59 additions & 0 deletions src/hint.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,59 @@
use crate::config::SETTINGS;
use crate::dirs;
use once_cell::sync::Lazy;
use std::collections::HashSet;
use std::path::PathBuf;
use std::sync::Mutex;

#[macro_export]
macro_rules! hint {
($id:expr, $message:expr, $example_cmd:expr) => {{
if $crate::hint::should_display_hint($id) {
let _ = $crate::file::touch_file(&$crate::hint::HINTS_DIR.join($id));
let prefix = console::style("hint")
.dim()
.yellow()
.for_stderr()
.to_string();
let message = format!($message);
let cmd = console::style($example_cmd).bold().for_stderr();
info!("{prefix} {message} {cmd}");
}
}};
}

pub static HINTS_DIR: Lazy<PathBuf> = Lazy::new(|| dirs::STATE.join("hints"));

pub static DISPLAYED_HINTS: Lazy<Mutex<HashSet<String>>> = Lazy::new(|| {
let mut hints = HashSet::new();

for file in xx::file::ls(&*HINTS_DIR).unwrap_or_default() {
if let Some(file_name) = file.file_name().map(|f| f.to_string_lossy()) {
if file_name.starts_with(".") {
continue;
}
hints.insert(file_name.to_string());
}
}

Mutex::new(hints)
});

pub fn should_display_hint(id: &str) -> bool {
if cfg!(test) || !console::user_attended() || !console::user_attended_stderr() {
return false;
}
if SETTINGS
.disable_hints
.iter()
.any(|hint| hint == id || hint == "*")
{
return false;
}
let displayed_hints = &mut DISPLAYED_HINTS.lock().unwrap();
if displayed_hints.contains(id) {
return false;
}
displayed_hints.insert(id.to_string());
true
}
3 changes: 3 additions & 0 deletions src/main.rs
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,9 @@ mod test;
#[macro_use]
mod output;

#[macro_use]
mod hint;

#[macro_use]
mod cmd;

Expand Down
54 changes: 1 addition & 53 deletions src/output.rs
Original file line number Diff line number Diff line change
@@ -1,9 +1,5 @@
use crate::config::SETTINGS;
#[cfg(feature = "timings")]
use crate::ui::style;
use once_cell::sync::Lazy;
use std::collections::HashSet;
use std::sync::Mutex;

#[cfg(test)]
#[macro_export]
Expand Down Expand Up @@ -91,54 +87,6 @@ macro_rules! debug {
}};
}

pub fn should_display_hint(id: &str) -> bool {
if cfg!(test) {
return false;
}
if SETTINGS
.disable_hints
.iter()
.any(|hint| hint == id || hint == "*")
{
return false;
}
if !console::user_attended() {
return false;
}
static DISPLAYED_HINTS: Lazy<Mutex<HashSet<String>>> = Lazy::new(Default::default);
let displayed_hints = &mut DISPLAYED_HINTS.lock().unwrap();
if displayed_hints.contains(id) {
return false;
}
displayed_hints.insert(id.to_string());
true
}

#[macro_export]
macro_rules! hint {
($id:expr, $message:expr, $example_cmd:expr) => {{
if $crate::output::should_display_hint($id) {
let prefix = console::style("hint")
.dim()
.yellow()
.for_stderr()
.to_string();
let cmd = console::style($example_cmd).bold().for_stderr();
let disable_single = console::style(format!("mise settings add disable_hints {}", $id))
.bold()
.for_stderr();
let disable_all = console::style("mise settings set disable_hints \"*\"")
.bold()
.for_stderr();
info!("{} {} {}", prefix, format!($message), cmd);
info!(
"{} disable this hint with {} or all with {}",
prefix, disable_single, disable_all
);
}
}};
}

#[cfg(not(test))]
#[macro_export]
macro_rules! info {
Expand All @@ -149,7 +97,7 @@ macro_rules! info {

#[cfg(feature = "timings")]
pub fn get_time_diff(module: &str, extra: &str) -> String {
static PREV: Mutex<Option<std::time::Instant>> = Mutex::new(None);
static PREV: std::sync::Mutex<Option<std::time::Instant>> = std::sync::Mutex::new(None);
let now = std::time::Instant::now();
if PREV.lock().unwrap().is_none() {
*PREV.lock().unwrap() = Some(std::time::Instant::now());
Expand Down

0 comments on commit dc76b03

Please sign in to comment.