From 94c656ad40a7aae570e5a5fb61ad32632acc6d46 Mon Sep 17 00:00:00 2001 From: Benedikt Peetz Date: Fri, 23 Aug 2024 13:11:09 +0200 Subject: feat(treewide): Use a configuration file This allows use to avoid duplication of default values in the codebase and obviously also facilitates changing these without having to re-compile. --- src/config/default.rs | 100 ++++++++++++++++++++++++++++++++++++++++++++++++++ 1 file changed, 100 insertions(+) create mode 100644 src/config/default.rs (limited to 'src/config/default.rs') diff --git a/src/config/default.rs b/src/config/default.rs new file mode 100644 index 0000000..131c289 --- /dev/null +++ b/src/config/default.rs @@ -0,0 +1,100 @@ +// yt - A fully featured command line YouTube client +// +// Copyright (C) 2024 Benedikt Peetz +// SPDX-License-Identifier: GPL-3.0-or-later +// +// This file is part of Yt. +// +// You should have received a copy of the License along with this program. +// If not, see . + +use std::path::PathBuf; + +use anyhow::{Context, Result}; + +fn get_runtime_path(name: &'static str) -> Result { + let xdg_dirs = xdg::BaseDirectories::with_prefix(PREFIX)?; + xdg_dirs + .place_runtime_file(name) + .with_context(|| format!("Failed to place runtime file: '{}'", name)) +} +fn get_data_path(name: &'static str) -> Result { + let xdg_dirs = xdg::BaseDirectories::with_prefix(PREFIX)?; + xdg_dirs + .place_data_file(name) + .with_context(|| format!("Failed to place data file: '{}'", name)) +} +fn get_config_path(name: &'static str) -> Result { + let xdg_dirs = xdg::BaseDirectories::with_prefix(PREFIX)?; + xdg_dirs + .place_config_file(name) + .with_context(|| format!("Failed to place config file: '{}'", name)) +} + +pub(super) fn create_path(path: PathBuf) -> Result { + if !path.exists() { + std::fs::create_dir_all(&path) + .with_context(|| format!("Failed to create the '{}' directory", path.display()))? + } + + Ok(path) +} + +pub const PREFIX: &str = "yt"; + +pub mod select { + pub fn playback_speed() -> f64 { + 2.7 + } + pub fn subtitle_langs() -> &'static str { + "" + } +} + +pub mod watch { + pub fn local_comments_length() -> i64 { + 1000 + } +} + +pub mod update { + pub fn max_backlog() -> i64 { + 20 + } +} + +pub mod paths { + use std::{env::temp_dir, path::PathBuf}; + + use anyhow::Result; + + use super::{create_path, get_config_path, get_data_path, get_runtime_path, PREFIX}; + + // We download to the temp dir to avoid taxing the disk + pub fn download_dir() -> Result { + let temp_dir = temp_dir(); + + create_path(temp_dir.join(PREFIX)) + } + pub fn mpv_config_path() -> Result { + get_config_path("mpv.conf") + } + pub fn mpv_input_path() -> Result { + get_config_path("mpv.input.conf") + } + pub fn database_path() -> Result { + get_data_path("videos.sqlite") + } + pub fn config_path() -> Result { + get_config_path("config.toml") + } + pub fn last_selection_path() -> Result { + get_runtime_path("selected.yts") + } +} + +pub mod download { + pub fn max_cache_size() -> &'static str { + "3 GiB" + } +} -- cgit 1.4.1