Skip to content

Commit

Permalink
Refactor tips (#575)
Browse files Browse the repository at this point in the history
* refactor: Move all get_random_tip functionality to its own function

* fix: Only depend on rand when building the tips feature
  • Loading branch information
lj3954 authored Sep 21, 2024
1 parent f46d313 commit 1d5807a
Show file tree
Hide file tree
Showing 2 changed files with 12 additions and 13 deletions.
11 changes: 3 additions & 8 deletions tui/Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -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"] }
Expand All @@ -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]
Expand Down
14 changes: 9 additions & 5 deletions tui/src/state.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down Expand Up @@ -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]
}

0 comments on commit 1d5807a

Please sign in to comment.