Skip to content

Commit

Permalink
implement delete (#1)
Browse files Browse the repository at this point in the history
* implement delete

* fix clippy
  • Loading branch information
jiacai2050 authored Feb 26, 2022
1 parent 049e0a3 commit 9f53337
Show file tree
Hide file tree
Showing 5 changed files with 89 additions and 4 deletions.
2 changes: 1 addition & 1 deletion Cargo.lock

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

2 changes: 1 addition & 1 deletion Cargo.toml
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
[package]
name = "eww-history-ext"
version = "0.1.0"
version = "0.2.0"
authors = ["Jiacai Liu <[email protected]>"]
edition = "2021"
publish = false
Expand Down
11 changes: 10 additions & 1 deletion eww-history-ext.el
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@
;; Copyright (C) 2022 Jiacai Liu

;; Author: Jiacai Liu <[email protected]>
;; Version: 0.1.0
;; Version: 0.2.0
;; Package-Requires: ((emacs "25.1"))
;; Keywords: eww, elfeed, history
;; URL: https://github.com/1History/eww-history-ext
Expand Down Expand Up @@ -62,6 +62,7 @@
(define-key map (kbd "w") 'eww-history-ext-copy-history-url)
(define-key map (kbd "t") 'eww-history-ext-copy-history-title)
(define-key map (kbd "s") 'eww-history-ext-search-by-keyword)
(define-key map (kbd "d") 'eww-history-ext-delete-history)
(define-key map (kbd "s-u") 'tabulated-list-revert)
map)
"Local keymap for eww-history-ext mode buffers.")
Expand Down Expand Up @@ -162,6 +163,14 @@
(kill-new title))
(user-error "There is no history at point")))

(defun eww-history-ext-delete-history ()
"Delete history at point."
(interactive)
(if-let ((entry (tabulated-list-delete-entry)))
(eww-history-ext-dyn--delete-history eww-history-ext-db
(string-to-number (car entry)))
(user-error "There is no history at point")))

(defun eww-history-ext-tabulated-list-revert (&optional revert)
(setq-local eww-history-ext-query-keyword eww-history-ext-default-query-keyword)
(setq-local eww-history-ext-query-limit eww-history-ext-default-query-limit))
Expand Down
73 changes: 72 additions & 1 deletion src/db.rs
Original file line number Diff line number Diff line change
Expand Up @@ -194,11 +194,50 @@ ORDER BY
self.query_inner(0, i64::MAX, Some(limit), keyword)
}

// Note: Update eww_history_visits/urls has no transaction now
pub fn delete_history(&self, id: i64) -> Result<()> {
let mut stat = self.conn.prepare(
r#"
DELETE FROM eww_history_visits
WHERE id = :id
RETURNING
url_id;
"#,
)?;

match stat.query_row(
named_params! {
":id":id,
},
|r| r.get(0),
) {
Err(e) if matches!(e, rusqlite::Error::QueryReturnedNoRows) => Ok(()),
Err(e) => Err(anyhow::Error::new(e)),
Ok::<i64, _>(url_id) => {
// visit_count can be zero in urls table
let _affected = self.conn.execute(
r#"
UPDATE
eww_history_urls
SET
visit_count = visit_count - 1
WHERE
id = :id
"#,
named_params! {
":id": url_id,
},
)?;
Ok(())
}
}
}

fn keyword_to_like(kw: Option<String>) -> String {
kw.map_or_else(
|| "1".to_string(),
|v| {
let v = v.replace("'", "");
let v = v.replace('\'', "");
format!("(url like '%{v}%' or title like '%{v}%')")
},
)
Expand All @@ -207,10 +246,42 @@ ORDER BY

#[cfg(test)]
mod tests {
use std::{thread, time::Duration};

use tempdir::TempDir;

use super::*;

#[test]
fn test_delete_history() {
let tmp_dir = TempDir::new("eww").unwrap();
let db_path = tmp_dir.path().join("history.db");
let db = Database::open(db_path).unwrap();
let cases = vec![
("https://www.baidu.com", "百度一下"),
("https://www.qq.com", "腾讯"),
("https://emacstalk.github.io", "EmacsTalk"),
("https://emacstalk.github.io", "EmacsTalk"),
];
for (url, title) in cases {
db.save_history(url.to_string(), title.to_string()).unwrap();
thread::sleep(Duration::from_millis(50));
}
let histories = db
.query_latest_histories(10, Some("emacs".to_string()))
.unwrap();
assert_eq!(2, histories.0.len());
assert_eq!(2, histories.0[0].visit_count);
assert_eq!(2, histories.0[1].visit_count);

db.delete_history(4).unwrap();
let histories = db
.query_latest_histories(10, Some("emacs".to_string()))
.unwrap();
assert_eq!(1, histories.0.len());
assert_eq!(1, histories.0[0].visit_count);
}

#[test]
fn test_get_url_id() {
let tmp_dir = TempDir::new("eww").unwrap();
Expand Down
5 changes: 5 additions & 0 deletions src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -24,6 +24,11 @@ fn save_history(db: &Database, url: String, title: String) -> Result<()> {
db.save_history(url, title)
}

#[defun]
fn delete_history(db: &Database, id: i64) -> Result<()> {
db.delete_history(id)
}

#[defun]
fn query_histories_by_range(
db: &Database,
Expand Down

0 comments on commit 9f53337

Please sign in to comment.