Vergessene Dateien
This commit is contained in:
1
.gitignore
vendored
Normal file
1
.gitignore
vendored
Normal file
@@ -0,0 +1 @@
|
|||||||
|
/target
|
||||||
@@ -0,0 +1,27 @@
|
|||||||
|
use anyhow::{anyhow, Result };
|
||||||
|
use directories::ProjectDirs;
|
||||||
|
|
||||||
|
pub struct Config {
|
||||||
|
pub database_file: String,
|
||||||
|
pub default_sheet: String,
|
||||||
|
}
|
||||||
|
|
||||||
|
impl Config {
|
||||||
|
pub fn build() -> Result<Config> {
|
||||||
|
let proj_dirs = ProjectDirs::from("de","schacht-analyse","timetracker")
|
||||||
|
.ok_or(anyhow!("Couldn't get project directories"))?;
|
||||||
|
|
||||||
|
let data_dir = proj_dirs.data_dir();
|
||||||
|
let mut db_file = data_dir.to_path_buf();
|
||||||
|
|
||||||
|
db_file.push("database.db");
|
||||||
|
|
||||||
|
if let Some(db_file_str) = db_file.to_str() {
|
||||||
|
return Ok(Config {
|
||||||
|
database_file: db_file_str.to_string(),
|
||||||
|
default_sheet: "default".to_string(),
|
||||||
|
});
|
||||||
|
}
|
||||||
|
Err(anyhow!("Couldn't get database file"))
|
||||||
|
}
|
||||||
|
}
|
||||||
@@ -0,0 +1,47 @@
|
|||||||
|
use std::path::PathBuf;
|
||||||
|
use anyhow::{anyhow, Context};
|
||||||
|
use anyhow::Result;
|
||||||
|
use std::str::FromStr;
|
||||||
|
use rusqlite::Connection;
|
||||||
|
|
||||||
|
use crate::config::Config;
|
||||||
|
|
||||||
|
pub fn connect_to_db(config: &Config) -> Result<Connection> {
|
||||||
|
if let Ok(conn) = Connection::open(&config.database_file) {
|
||||||
|
return Ok(conn);
|
||||||
|
}
|
||||||
|
|
||||||
|
Err(anyhow!(
|
||||||
|
"Cannot create connection to database"
|
||||||
|
))
|
||||||
|
}
|
||||||
|
|
||||||
|
pub fn create_tables(db: &Connection) -> Result<()> {
|
||||||
|
let query = "
|
||||||
|
CREATE TABLE IF NOT EXISTS entries (
|
||||||
|
id INTEGER NOT NULL PRIMARY KEY AUTOINCREMENT,
|
||||||
|
note VARCHAR(255) NOT NULL,
|
||||||
|
start TIMESTAMP NOT NULL,
|
||||||
|
end TIMESTAMP NOT NULL,
|
||||||
|
sheet VARCHAR(255) NOT NULL
|
||||||
|
)
|
||||||
|
";
|
||||||
|
db.execute(query, ())
|
||||||
|
.context("Failed to create entries table")?;
|
||||||
|
|
||||||
|
Ok(())
|
||||||
|
}
|
||||||
|
|
||||||
|
pub fn ensure_db_exists(config: &Config) -> Result<()> {
|
||||||
|
let mut db_path = PathBuf::from_str(&config.database_file).unwrap();
|
||||||
|
db_path.pop();
|
||||||
|
|
||||||
|
if!db_path.exists() {
|
||||||
|
std::fs::create_dir_all(&db_path).context(format!(
|
||||||
|
"Cannot create the data folder for Timetracker. The expected path was {:?}",
|
||||||
|
&db_path
|
||||||
|
))?;
|
||||||
|
|
||||||
|
}
|
||||||
|
Ok(())
|
||||||
|
}
|
||||||
66
src/state.rs
66
src/state.rs
@@ -0,0 +1,66 @@
|
|||||||
|
use rusqlite::Connection;
|
||||||
|
use anyhow::{anyhow, Context, Result };
|
||||||
|
use directories::ProjectDirs;
|
||||||
|
use std::fs;
|
||||||
|
|
||||||
|
use crate::config::Config;
|
||||||
|
use crate::database::connect_to_db;
|
||||||
|
|
||||||
|
pub struct State {
|
||||||
|
pub current_sheet: String,
|
||||||
|
pub last_sheet: String,
|
||||||
|
pub database: Connection,
|
||||||
|
}
|
||||||
|
|
||||||
|
impl State {
|
||||||
|
pub fn build(config: &Config) -> Result<State> {
|
||||||
|
let db = connect_to_db(config)?;
|
||||||
|
let proj_dirs = ProjectDirs::from("de", "schacht-analyse", "timetracker")
|
||||||
|
.ok_or(anyhow!("Could not determine project directories"))?;
|
||||||
|
|
||||||
|
let mut state = State {
|
||||||
|
current_sheet : "default".to_string(),
|
||||||
|
last_sheet: "default".to_string(),
|
||||||
|
database: db,
|
||||||
|
};
|
||||||
|
|
||||||
|
let data_dir = proj_dirs.data_local_dir();
|
||||||
|
let mut data_file = data_dir.to_path_buf();
|
||||||
|
data_file.push("data.txt");
|
||||||
|
|
||||||
|
let content_res = fs::read_to_string(&data_file);
|
||||||
|
|
||||||
|
match content_res {
|
||||||
|
Ok(content) => {
|
||||||
|
let mut lines = content.lines();
|
||||||
|
if let Some(s) = lines.next() {
|
||||||
|
state.current_sheet = s.to_string();
|
||||||
|
}
|
||||||
|
if let Some(s) = lines.next() {
|
||||||
|
state.last_sheet = s.to_string();
|
||||||
|
}
|
||||||
|
}
|
||||||
|
Err(_) => {
|
||||||
|
state
|
||||||
|
.update_file()
|
||||||
|
.context("Cannot write the default state file.")?;
|
||||||
|
}
|
||||||
|
}
|
||||||
|
Ok(state)
|
||||||
|
}
|
||||||
|
|
||||||
|
fn update_file(&self) -> Result<()> {
|
||||||
|
let proj_dirs = ProjectDirs::from("de", "schacht-analyse", "timetracker")
|
||||||
|
.ok_or(anyhow!("Could not determine project directories"))?;
|
||||||
|
|
||||||
|
let data_dir = proj_dirs.data_local_dir();
|
||||||
|
let mut data_file = data_dir.to_path_buf();
|
||||||
|
data_file.push("data.txt");
|
||||||
|
|
||||||
|
fs::write(&data_file,
|
||||||
|
format!("{}\n{}",self.current_sheet,self.last_sheet),)?;
|
||||||
|
Ok(())
|
||||||
|
|
||||||
|
}
|
||||||
|
|
||||||
|
}
|
||||||
19
src/style.rs
19
src/style.rs
@@ -0,0 +1,19 @@
|
|||||||
|
use colored::{ColoredString, Colorize};
|
||||||
|
|
||||||
|
pub enum Styles {
|
||||||
|
Error,
|
||||||
|
Title,
|
||||||
|
Message,
|
||||||
|
Primary,
|
||||||
|
Secondary,
|
||||||
|
}
|
||||||
|
|
||||||
|
pub fn style_string(label: &str, style: Styles) -> ColoredString {
|
||||||
|
match style {
|
||||||
|
Styles::Error => label.red().bold(),
|
||||||
|
Styles::Title => label.bold(),
|
||||||
|
Styles::Message => label.bold(),
|
||||||
|
Styles::Primary => label.green().bold(),
|
||||||
|
Styles::Secondary => label.cyan(),
|
||||||
|
}
|
||||||
|
}
|
||||||
Reference in New Issue
Block a user