-
Notifications
You must be signed in to change notification settings - Fork 366
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
feat: automatically hide hints after displayed once (#2807)
- Loading branch information
Showing
4 changed files
with
74 additions
and
53 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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 | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -11,6 +11,9 @@ mod test; | |
#[macro_use] | ||
mod output; | ||
|
||
#[macro_use] | ||
mod hint; | ||
|
||
#[macro_use] | ||
mod cmd; | ||
|
||
|
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters