Splitting kana set
This commit is contained in:
1 parent
36e5693564
commit
c74a0eb311
25 files changed
+389
-354
No files matched your search
@@ -1,20 +1,21 @@
|
||||
use crate::dictionary::app_data_dir;
|
||||
use crate::fs::*;
|
||||
use crate::lang::{Id, WordOpenMode};
|
||||
use chrono::{DateTime, Utc};
|
||||
use std::fs;
|
||||
use std::fs::{File, OpenOptions};
|
||||
use std::io::Write;
|
||||
use std::io::{BufRead, BufReader};
|
||||
use std::path::PathBuf;
|
||||
use crate::notification::NotificationData;
|
||||
use crate::notification::NotificationData::PermissionDenied;
|
||||
|
||||
pub fn get_history_of_set(id: Id) -> Vec<HistoryItem> {
|
||||
let app_dir = history_dir();
|
||||
pub fn get_history_of_set(id: Id) -> Result<Vec<HistoryItem>, NotificationData> {
|
||||
let app_dir = history_dir()?;
|
||||
let head = app_dir.clone().join(format!("set_{}_history.csv", id));
|
||||
let mut lines = Vec::new();
|
||||
|
||||
append_lines_if_exists(head, &mut lines);
|
||||
lines.reverse();
|
||||
parse_history_items(lines)
|
||||
Ok(parse_history_items(lines))
|
||||
}
|
||||
|
||||
fn append_lines_if_exists(path: PathBuf, lines: &mut Vec<String>) {
|
||||
@@ -56,15 +57,15 @@ fn parse_history_items(strings: Vec<String>) -> Vec<HistoryItem> {
|
||||
items
|
||||
}
|
||||
|
||||
pub fn push_note(set_id: Id, item: HistoryItem) {
|
||||
let app_dir = history_dir();
|
||||
pub fn push_note(set_id: Id, item: HistoryItem) -> Result<(), NotificationData> {
|
||||
let app_dir = history_dir()?;
|
||||
let path = app_dir.clone().join(format!("set_{}_history.csv", set_id));
|
||||
|
||||
let mut file = OpenOptions::new()
|
||||
.create(true)
|
||||
.append(true)
|
||||
.open(path)
|
||||
.unwrap();
|
||||
.map_err(|_| PermissionDenied)?;
|
||||
let line_str = format!(
|
||||
"{};{};{};{};{}",
|
||||
item.timestamp.timestamp(),
|
||||
@@ -78,15 +79,8 @@ pub fn push_note(set_id: Id, item: HistoryItem) {
|
||||
item.before,
|
||||
item.after
|
||||
);
|
||||
writeln!(&mut file, "{}", line_str).unwrap();
|
||||
}
|
||||
|
||||
pub fn history_dir() -> PathBuf {
|
||||
let directory = app_data_dir().join("history");
|
||||
if !directory.exists() {
|
||||
fs::create_dir(directory.clone()).unwrap();
|
||||
}
|
||||
directory
|
||||
writeln!(&mut file, "{}", line_str).map_err(|_| PermissionDenied)?;
|
||||
Ok(())
|
||||
}
|
||||
|
||||
#[derive(Clone)]
|
||||
|
||||
Reference in new issue
Block a user