Refactor tips (#575)

* refactor: Move all get_random_tip functionality to its own function

* fix: Only depend on rand when building the tips feature
This commit is contained in:
Liam 2024-09-20 19:12:26 -05:00 committed by GitHub
parent f46d3137b7
commit 1d5807ad51
No known key found for this signature in database
GPG Key ID: B5690EEEBB952194
2 changed files with 12 additions and 13 deletions

View File

@ -7,17 +7,12 @@ edition = "2021"
license.workspace = true
repository = "https://github.com/ChrisTitusTech/linutil/tree/main/tui"
version.workspace = true
include = [
"src/*.rs",
"Cargo.toml",
"build.rs",
"cool_tips.txt",
]
include = ["src/*.rs", "Cargo.toml", "build.rs", "cool_tips.txt"]
build = "build.rs"
[features]
default = ["tips"]
tips = []
tips = ["rand"]
[dependencies]
clap = { version = "4.5.16", features = ["derive"] }
@ -28,7 +23,7 @@ portable-pty = "0.8.1"
ratatui = "0.28.1"
tui-term = "0.1.12"
unicode-width = "0.1.13"
rand = "0.8.5"
rand = { version = "0.8.5", optional = true }
linutil_core = { path = "../core", version = "24.9.19" }
[build-dependencies]

View File

@ -74,7 +74,7 @@ impl AppState {
selected_commands: Vec::new(),
drawable: false,
#[cfg(feature = "tips")]
tip: get_random_line(include_str!("../cool_tips.txt").lines().collect()),
tip: get_random_tip(),
};
state.update_items();
state
@ -543,12 +543,16 @@ impl AppState {
}
#[cfg(feature = "tips")]
fn get_random_line(lines: Vec<&str>) -> &str {
if lines.is_empty() {
const TIPS: &str = include_str!("../cool_tips.txt");
#[cfg(feature = "tips")]
fn get_random_tip() -> &'static str {
let tips: Vec<&str> = TIPS.lines().collect();
if tips.is_empty() {
return "";
}
let mut rng = rand::thread_rng();
let random_index = rng.gen_range(0..lines.len());
lines[random_index]
let random_index = rng.gen_range(0..tips.len());
tips[random_index]
}