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

View File

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