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
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
|
use std::convert::From;
use serde_derive::Deserialize;
use tui::layout::Constraint;
use crate::config::option::{DisplayMode, DisplayOption, LineNumberStyle};
use super::sort_raw::SortOptionRaw;
pub const fn default_column_ratio() -> (usize, usize, usize) {
(1, 3, 4)
}
fn default_mode() -> String {
"default".to_string()
}
const fn default_true() -> bool {
true
}
const fn default_scroll_offset() -> usize {
4
}
#[derive(Clone, Debug, Deserialize)]
pub struct DisplayOptionRaw {
#[serde(default = "default_mode")]
pub mode: String,
#[serde(default)]
pub automatically_count_files: bool,
#[serde(default = "default_true")]
pub collapse_preview: bool,
#[serde(default)]
pub column_ratio: Option<Vec<usize>>,
#[serde(default = "default_scroll_offset")]
pub scroll_offset: usize,
#[serde(default = "default_true")]
pub show_borders: bool,
#[serde(default)]
pub show_hidden: bool,
#[serde(default)]
pub show_icons: bool,
#[serde(default = "default_true")]
pub tilde_in_titlebar: bool,
#[serde(default, rename = "sort")]
pub sort_options: SortOptionRaw,
#[serde(default)]
pub line_number_style: String,
}
impl std::default::Default for DisplayOptionRaw {
fn default() -> Self {
Self {
mode: default_mode(),
automatically_count_files: false,
collapse_preview: true,
column_ratio: None,
scroll_offset: 4,
show_borders: true,
show_hidden: false,
show_icons: false,
sort_options: SortOptionRaw::default(),
tilde_in_titlebar: true,
line_number_style: "none".to_string(),
}
}
}
impl From<DisplayOptionRaw> for DisplayOption {
fn from(raw: DisplayOptionRaw) -> Self {
let mode = match raw.mode.as_str() {
"vsplit" => DisplayMode::VSplit,
_ => DisplayMode::Default,
};
let column_ratio = match raw.column_ratio {
Some(s) if s.len() == 3 => (s[0], s[1], s[2]),
Some(s) if s.len() == 2 => (0, s[0], s[1]),
_ => default_column_ratio(),
};
let total = (column_ratio.0 + column_ratio.1 + column_ratio.2) as u32;
let default_layout = [
Constraint::Ratio(column_ratio.0 as u32, total),
Constraint::Ratio(column_ratio.1 as u32, total),
Constraint::Ratio(column_ratio.2 as u32, total),
];
let no_preview_layout = [
Constraint::Ratio(column_ratio.0 as u32, total),
Constraint::Ratio(column_ratio.1 as u32 + column_ratio.2 as u32, total),
Constraint::Ratio(0, total),
];
let _line_nums = match raw.line_number_style.as_ref() {
"absolute" => LineNumberStyle::Absolute,
"relative" => LineNumberStyle::Relative,
_ => LineNumberStyle::None,
};
Self {
_mode: mode,
_automatically_count_files: raw.automatically_count_files,
_collapse_preview: raw.collapse_preview,
_scroll_offset: raw.scroll_offset,
_show_borders: raw.show_borders,
_show_hidden: raw.show_hidden,
_show_icons: raw.show_icons,
_sort_options: raw.sort_options.into(),
_tilde_in_titlebar: raw.tilde_in_titlebar,
_line_nums,
column_ratio,
default_layout,
no_preview_layout,
}
}
}
|