1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
|
use crate::data::LayoutInfo;
use crate::input::options::Options;
use crate::pane_size::Size;
use crate::{
home::{find_default_config_dir, get_theme_dir},
input::{config::Config, layout::Layout, theme::Themes},
setup::get_default_themes,
};
use serde::{Deserialize, Serialize};
use std::path::PathBuf;
#[derive(Debug, Clone, Default, Serialize, Deserialize, PartialEq)]
pub struct CliAssets {
pub config_file_path: Option<PathBuf>,
pub config_dir: Option<PathBuf>,
pub should_ignore_config: bool,
pub configuration_options: Option<Options>, // merged from everywhere: there are the source of truth
pub layout: Option<LayoutInfo>,
pub terminal_window_size: Size,
pub data_dir: Option<PathBuf>,
pub is_debug: bool,
pub max_panes: Option<usize>,
pub force_run_layout_commands: bool,
pub cwd: Option<PathBuf>,
}
impl CliAssets {
pub fn load_config_and_layout(&self) -> (Config, Layout) {
let config = {
if self.should_ignore_config {
Config::from_default_assets().unwrap_or_else(|_| Default::default())
} else if let Some(ref path) = self.config_file_path {
let default_config =
Config::from_default_assets().unwrap_or_else(|_| Default::default());
Config::from_path(path, Some(default_config.clone()))
.unwrap_or_else(|_| default_config)
} else {
Config::from_default_assets().unwrap_or_else(|_| Default::default())
}
};
let (mut layout, mut config_with_merged_layout_opts) = {
let layout_dir = self
.configuration_options
.as_ref()
.and_then(|e| e.layout_dir.clone())
.or_else(|| config.options.layout_dir.clone())
.or_else(|| {
self.config_dir
.clone()
.or_else(find_default_config_dir)
.map(|dir| dir.join("layouts"))
});
self.layout.as_ref().and_then(|layout_info| {
Layout::from_layout_info_with_config(&layout_dir, layout_info, Some(config.clone()))
.ok()
})
}
.map(|(layout, config)| (layout, config))
.unwrap_or_else(|| (Layout::default_layout_asset(), config));
if self.force_run_layout_commands {
layout.recursively_add_start_suspended(Some(false));
}
config_with_merged_layout_opts.themes = config_with_merged_layout_opts
.themes
.merge(get_default_themes());
let user_theme_dir = self
.configuration_options
.as_ref()
.and_then(|o| o.theme_dir.clone())
.or_else(|| {
config_with_merged_layout_opts
.options
.theme_dir
.clone()
.or_else(|| {
get_theme_dir(self.config_dir.clone().or_else(find_default_config_dir))
})
.filter(|dir| dir.exists())
});
if let Some(themes) = user_theme_dir.and_then(|u| Themes::from_dir(u).ok()) {
config_with_merged_layout_opts.themes =
config_with_merged_layout_opts.themes.merge(themes);
}
(config_with_merged_layout_opts, layout)
}
}
|