mirror of
https://github.com/YGGverse/Yoda.git
synced 2026-04-02 17:45:28 +00:00
implement recently visited memory index, add new global menu section
This commit is contained in:
parent
0618283d84
commit
38594c0aa8
5 changed files with 113 additions and 20 deletions
|
|
@ -1,8 +1,12 @@
|
|||
mod request;
|
||||
mod tab;
|
||||
|
||||
use request::Request;
|
||||
use tab::Tab;
|
||||
|
||||
/// Reduce disk usage by cache Bookmarks index in memory
|
||||
pub struct Memory {
|
||||
pub request: Request,
|
||||
pub tab: Tab,
|
||||
}
|
||||
|
||||
|
|
@ -17,6 +21,9 @@ impl Memory {
|
|||
|
||||
/// Create new `Self`
|
||||
pub fn new() -> Self {
|
||||
Self { tab: Tab::new() }
|
||||
Self {
|
||||
request: Request::new(),
|
||||
tab: Tab::new(),
|
||||
}
|
||||
}
|
||||
}
|
||||
|
|
|
|||
60
src/profile/history/memory/request.rs
Normal file
60
src/profile/history/memory/request.rs
Normal file
|
|
@ -0,0 +1,60 @@
|
|||
use gtk::glib::GString;
|
||||
use itertools::Itertools;
|
||||
use std::{cell::RefCell, collections::HashMap};
|
||||
|
||||
pub struct Value {
|
||||
pub unix_timestamp: i64,
|
||||
}
|
||||
|
||||
/// Recent request history
|
||||
pub struct Request {
|
||||
index: RefCell<HashMap<GString, Value>>,
|
||||
}
|
||||
|
||||
impl Default for Request {
|
||||
fn default() -> Self {
|
||||
Self::new()
|
||||
}
|
||||
}
|
||||
|
||||
impl Request {
|
||||
// Constructors
|
||||
|
||||
/// Create new `Self`
|
||||
pub fn new() -> Self {
|
||||
Self {
|
||||
index: RefCell::new(HashMap::new()),
|
||||
}
|
||||
}
|
||||
|
||||
// Actions
|
||||
|
||||
/// Add new record with `request` as key and `unix_timestamp` as value
|
||||
/// * replace with new value if `request` already exists
|
||||
pub fn set(&self, request: GString, unix_timestamp: i64) {
|
||||
self.index
|
||||
.borrow_mut()
|
||||
.insert(request, Value { unix_timestamp });
|
||||
}
|
||||
|
||||
/// Get recent requests vector
|
||||
/// * sorted by `unix_timestamp` DESC
|
||||
pub fn recent(&self, limit: usize) -> Vec<GString> {
|
||||
let mut recent: Vec<GString> = Vec::new();
|
||||
for (request, _) in self
|
||||
.index
|
||||
.borrow()
|
||||
.iter()
|
||||
.sorted_by(|a, b| Ord::cmp(&b.1.unix_timestamp, &a.1.unix_timestamp))
|
||||
.take(limit)
|
||||
{
|
||||
recent.push(request.clone())
|
||||
}
|
||||
recent
|
||||
}
|
||||
|
||||
/// Get records total
|
||||
pub fn total(&self) -> usize {
|
||||
self.index.borrow().len()
|
||||
}
|
||||
}
|
||||
Loading…
Add table
Add a link
Reference in a new issue