reorganize clone semantics, implement recently closed tabs history

This commit is contained in:
yggverse 2025-01-12 04:02:41 +02:00
parent 3682b5bf3f
commit ba68019614
17 changed files with 176 additions and 52 deletions

View file

@ -56,7 +56,7 @@ pub fn select(
WHERE `profile_id` = ? AND `request` LIKE ?",
)?;
let result = stmt.query_map((profile_id, request.unwrap_or("%")), |row| {
let result = stmt.query_map((profile_id, request.unwrap_or("%".to_string())), |row| {
Ok(Table {
id: row.get(0)?,
profile_id: row.get(1)?,

View file

@ -0,0 +1,24 @@
mod closed;
use closed::Closed;
/// Reduce disk usage by cache Bookmarks index in memory
pub struct Memory {
pub closed: Closed,
}
impl Default for Memory {
fn default() -> Self {
Self::new()
}
}
impl Memory {
// Constructors
/// Create new `Self`
pub fn new() -> Self {
Self {
closed: Closed::new(),
}
}
}

View file

@ -0,0 +1,53 @@
use gtk::glib::GString;
use itertools::Itertools;
use std::{cell::RefCell, collections::HashMap};
/// Reduce disk usage by cache Bookmarks index in memory
pub struct Closed {
index: RefCell<HashMap<GString, i64>>,
}
impl Default for Closed {
fn default() -> Self {
Self::new()
}
}
impl Closed {
// Constructors
/// Create new `Self`
pub fn new() -> Self {
Self {
index: RefCell::new(HashMap::new()),
}
}
// Actions
/// Add new record
/// * replace with new one if the record already exist
pub fn add(&self, request: GString, unix_timestamp: i64) {
self.index.borrow_mut().insert(request, unix_timestamp);
}
/// Get recent requests vector sorted by `ID` 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, &a.1))
.take(limit)
{
recent.push(request.clone())
}
recent
}
/// Get records total
pub fn total(&self) -> usize {
self.index.borrow().len()
}
}