src/global/functions/paths.rs
97 lines · 3712 bytes
6 symbols in this file
1use std::path::PathBuf;
2
3/// Returns the configuration directory for youtube-tui.
4///
5/// If `YOUTUBETUI_CONFIG_HOME` is set, it is used directly as the config directory.
6/// Otherwise falls back to the platform default with a `youtube-tui` subdirectory:
7/// - Linux: `~/.config/youtube-tui/`
8/// - Windows: `%APPDATA%\youtube-tui\`
9/// - macOS: `~/Library/Application Support/youtube-tui/`
10pub fn config_dir() -> PathBuf {
11 std::env::var("YOUTUBETUI_CONFIG_HOME")
12 .map(PathBuf::from)
13 .unwrap_or_else(|_| {
14 dirs::config_dir()
15 .expect("no config dir, set YOUTUBETUI_CONFIG_HOME to override")
16 .join("youtube-tui")
17 })
18}
19
20/// Returns the data directory for youtube-tui.
21///
22/// If `YOUTUBETUI_DATA_HOME` is set, it is used directly as the data directory.
23/// Otherwise falls back to the platform default with a `youtube-tui` subdirectory:
24/// - Linux: `~/.local/share/youtube-tui/`
25/// - Windows: `%LOCALAPPDATA%\youtube-tui\`
26/// - macOS: `~/Library/Application Support/youtube-tui/`
27pub fn data_dir() -> PathBuf {
28 std::env::var("YOUTUBETUI_DATA_HOME")
29 .map(PathBuf::from)
30 .unwrap_or_else(|_| {
31 dirs::data_local_dir()
32 .expect("no data dir, set YOUTUBETUI_DATA_HOME to override")
33 .join("youtube-tui")
34 })
35}
36
37/// Returns the cache directory for youtube-tui.
38///
39/// If `YOUTUBETUI_CACHE_HOME` is set, it is used directly as the cache directory.
40/// Otherwise falls back to the platform default with a `youtube-tui` subdirectory:
41/// - Linux: `~/.cache/youtube-tui/`
42/// - Windows: `%LOCALAPPDATA%\youtube-tui\cache\`
43/// - macOS: `~/Library/Caches/youtube-tui/`
44pub fn cache_dir() -> PathBuf {
45 std::env::var("YOUTUBETUI_CACHE_HOME")
46 .map(PathBuf::from)
47 .unwrap_or_else(|_| {
48 dirs::cache_dir()
49 .expect("no cache dir, set YOUTUBETUI_CACHE_HOME to override")
50 .join("youtube-tui")
51 })
52}
53
54/// Returns the storage directory for rustypipe.
55///
56/// Uses the parent of `YOUTUBETUI_DATA_HOME` (sibling to the youtube-tui data dir) if set,
57/// otherwise falls back to the platform data dir with a `rustypipe` subdirectory.
58pub fn rustypipe_dir() -> PathBuf {
59 std::env::var("YOUTUBETUI_DATA_HOME")
60 .map(|p| {
61 let base = PathBuf::from(p);
62 let parent = base.parent().map(|p| p.to_path_buf()).unwrap_or(base);
63 parent.join("rustypipe")
64 })
65 .unwrap_or_else(|_| {
66 dirs::data_local_dir()
67 .expect("no data dir, set YOUTUBETUI_DATA_HOME to override")
68 .join("rustypipe")
69 })
70}
71
72/// Returns the default save path string for the `save-path` env variable.
73pub fn default_save_path() -> String {
74 let p = data_dir().join("saved");
75 let mut s = p.to_string_lossy().to_string();
76 if !s.ends_with(std::path::MAIN_SEPARATOR) {
77 s.push(std::path::MAIN_SEPARATOR);
78 }
79 s
80}
81
82/// Returns the default download path string for the `download-path` env variable.
83///
84/// If `YOUTUBETUI_DOWNLOAD_HOME` is set, it is used directly as the download directory.
85/// Otherwise falls back to the platform downloads directory.
86pub fn default_download_path() -> String {
87 let downloads = std::env::var("YOUTUBETUI_DOWNLOAD_HOME")
88 .map(PathBuf::from)
89 .unwrap_or_else(|_| {
90 dirs::download_dir().expect("no download dir, set YOUTUBETUI_DOWNLOAD_HOME to override")
91 });
92 let mut s = downloads.to_string_lossy().to_string();
93 if !s.ends_with(std::path::MAIN_SEPARATOR) {
94 s.push(std::path::MAIN_SEPARATOR);
95 }
96 format!("{s}%(title)s-%(id)s.%(ext)s")
97}