diff options
| author | Jae-Heon Ji <32578710+jaeheonji@users.noreply.github.com> | 2021-12-10 02:53:46 +0900 |
|---|---|---|
| committer | GitHub <noreply@github.com> | 2021-12-09 18:53:46 +0100 |
| commit | d79060f69a99e73215533c1acbdff79af99af8f9 (patch) | |
| tree | 626603f9756db04a41218cb3da9b02e6069bab1f /default-plugins/status-bar/src/tip/utils.rs | |
| parent | chore: update example layout (#926) (diff) | |
| download | sl0ppij-d79060f69a99e73215533c1acbdff79af99af8f9.tar.gz sl0ppij-d79060f69a99e73215533c1acbdff79af99af8f9.tar.xz sl0ppij-d79060f69a99e73215533c1acbdff79af99af8f9.zip | |
feat(status-bar): add multiple tips (#848)
* feat(status-bar): add draft for multiple tips
* feat: add TIPS_MAP
* Simplified 'tip' function.
* chore: update file structure
* feat(status-bar): update method of Tip rendering
* feat(status-bar): change type of tip in State
* refactor(status-bar): related to random tip selection
* feat(status-bar): add simple local cache for testing
* feat(status-bar): add cache system for tip data
* Add mpadir to wasm for plugin to access zellij temp folder.
* refactor(status-bar): update cache and utils
* fix(status-bar): update file read error
* refactor(status-bar): update macros
* chore(status-bar): delete test data
* chore(status-bar): update missing fixes
* feat(status-bar): add detailed error
* style: make clippy
Diffstat (limited to 'default-plugins/status-bar/src/tip/utils.rs')
| -rw-r--r-- | default-plugins/status-bar/src/tip/utils.rs | 68 |
1 files changed, 68 insertions, 0 deletions
diff --git a/default-plugins/status-bar/src/tip/utils.rs b/default-plugins/status-bar/src/tip/utils.rs new file mode 100644 index 00000000..3dd4c5a8 --- /dev/null +++ b/default-plugins/status-bar/src/tip/utils.rs @@ -0,0 +1,68 @@ +use std::path::PathBuf; + +use rand::prelude::{IteratorRandom, SliceRandom}; + +use zellij_tile::prelude::get_zellij_version; + +use super::cache::LocalCache; +use super::consts::{DEFAULT_CACHE_FILE_PATH, MAX_CACHE_HITS}; +use super::data::TIPS; + +macro_rules! get_name_and_caching { + ($cache:expr) => {{ + let name = get_random_tip_name(); + $cache.caching(name.clone()).unwrap(); + return name; + }}; + ($cache:expr, $from:expr) => {{ + let name = $from.choose(&mut rand::thread_rng()).unwrap().to_string(); + $cache.caching(name.clone()).unwrap(); + return name; + }}; +} + +pub fn get_random_tip_name() -> String { + TIPS.keys() + .choose(&mut rand::thread_rng()) + .unwrap() + .to_string() +} + +pub fn get_cached_tip_name() -> String { + let mut local_cache = LocalCache::new(PathBuf::from(DEFAULT_CACHE_FILE_PATH)).unwrap(); + + let zellij_version = get_zellij_version(); + if zellij_version.ne(local_cache.get_version()) { + local_cache.set_version(zellij_version); + local_cache.clear().unwrap(); + } + + if local_cache.is_empty() { + get_name_and_caching!(local_cache); + } + + let usable_tips = local_cache + .get_cached_data() + .iter() + .filter(|(_, &v)| v < MAX_CACHE_HITS) + .map(|(k, _)| k.to_string()) + .collect::<Vec<String>>(); + + if usable_tips.is_empty() { + let cached_set = local_cache.get_cached_data_set(); + let diff = TIPS + .keys() + .cloned() + .filter(|k| !cached_set.contains(&k.to_string())) + .collect::<Vec<&str>>(); + + if !diff.is_empty() { + get_name_and_caching!(local_cache, diff); + } else { + local_cache.clear().unwrap(); + get_name_and_caching!(local_cache); + } + } else { + get_name_and_caching!(local_cache, usable_tips); + } +} |
