From 1d5807ad5130670bf446aea9b6accf443cd5d153 Mon Sep 17 00:00:00 2001 From: Liam <33645555+lj3954@users.noreply.github.com> Date: Fri, 20 Sep 2024 19:12:26 -0500 Subject: [PATCH] Refactor tips (#575) * refactor: Move all get_random_tip functionality to its own function * fix: Only depend on rand when building the tips feature --- tui/Cargo.toml | 11 +++-------- tui/src/state.rs | 14 +++++++++----- 2 files changed, 12 insertions(+), 13 deletions(-) diff --git a/tui/Cargo.toml b/tui/Cargo.toml index b6e57edcd..681d645d8 100644 --- a/tui/Cargo.toml +++ b/tui/Cargo.toml @@ -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] diff --git a/tui/src/state.rs b/tui/src/state.rs index 2a8297470..9bfb982f4 100644 --- a/tui/src/state.rs +++ b/tui/src/state.rs @@ -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] }