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
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
//! State persistence — clipboard, pins, sessions
//!
//! Stores state in `~/.local/share/fab/` via the directories crate.
use anyhow::{bail, Context, Result};
use directories::ProjectDirs;
use serde::{Deserialize, Serialize};
use std::fs;
use std::path::PathBuf;
/// Get the fab data directory (~/.local/share/fab/)
pub fn get_data_dir() -> Result<PathBuf> {
let proj_dirs =
ProjectDirs::from("com", "fab", "fab").context("Failed to determine data directory")?;
let data_dir = proj_dirs.data_dir().to_path_buf();
// Never follow a symlink supplied at the cache root. The directory holds
// local state and cache files, so redirecting it elsewhere would weaken
// both the permission guarantee and the expected data boundary.
if let Ok(metadata) = fs::symlink_metadata(&data_dir) {
if metadata.file_type().is_symlink() {
bail!("data directory is a symlink: {:?}", data_dir);
}
if !metadata.is_dir() {
bail!("data directory is not a directory: {:?}", data_dir);
}
}
// Ensure directory exists
if !data_dir.exists() {
fs::create_dir_all(&data_dir)
.context(format!("Failed to create data directory: {:?}", data_dir))?;
}
// The data dir holds per-path banner caches (listings, git status) and
// the IPC socket — restrict it to this user. Apply on every call so an
// existing install with looser permissions is also tightened (the
// `directories` crate can return a path that pre-existed with mode
// 0755 from an earlier version of fab or from a manual mkdir).
#[cfg(unix)]
{
use std::os::unix::fs::PermissionsExt;
if let Err(e) = std::fs::set_permissions(&data_dir, std::fs::Permissions::from_mode(0o700))
{
// Only warn — a permission failure is not fatal for the rest of
// the call (the data dir is still usable, just less hardened).
tracing::warn!("Failed to set 0700 permissions on data dir: {}", e);
}
}
Ok(data_dir)
}
// === Config ===
#[derive(Debug, Clone, Serialize, Deserialize)]
#[serde(default)]
pub struct Config {
// Display preferences
pub icons: bool,
pub colors: bool,
pub compact: bool,
pub verbose: bool,
pub max_display_items: usize,
// Permission display
pub permission: String, // "rwx", "octal", "disable"
// Size display
pub size: String, // "default", "short", "bytes"
// Date display
pub date: String, // "date", "relative"
// File classification
pub classify: bool, // append */=>@|
// Symlink display
pub no_symlink: bool,
// Total size
pub total_size: bool,
// Columns to show
pub columns: Vec<String>,
// Columns to hide
pub hide_columns: Vec<String>,
// Sorting
pub sort: String,
pub reverse: bool,
pub group_dirs: String, // "none", "first", "last"
// Display options
pub hyperlink: bool, // terminal hyperlinks
pub hidden: bool, // show hidden files by default
pub highlight_recent: String, // background highlight for recent files (e.g., "236", "22", "green")
pub highlight_old: String, // background highlight for old files (e.g., "", "none")
// Features
pub git_status: bool,
pub build_status: bool,
pub todo_count: bool,
pub languages: bool,
pub ports: bool,
pub docker: bool,
pub inline_preview: bool, // show inline previews for directories
pub mini_tree: bool, // show mini tree on right side when there's space
pub smart_truncation: bool, // smart truncation for big folders
pub numbered: bool, // show [N] numbers next to items for quick navigation
pub number_from_bottom: bool, // [1] on the bottom row (nearest the prompt) so recent
// items get small numbers; false = classic top-down [1]..[N]
pub zebra_rows: bool, // alternating row background tint (disabled by default)
pub open_command: String, // command to open files (e.g., "micro", "nano", "vim")
// Directories to ignore
pub ignore_dirs: Vec<String>,
}
impl Default for Config {
fn default() -> Self {
Self {
icons: true,
colors: true,
compact: false,
verbose: false,
max_display_items: 0,
permission: "rwx".to_string(),
size: "default".to_string(),
date: "date".to_string(),
classify: false,
no_symlink: false,
total_size: true,
columns: vec![
"permission".to_string(),
"owner".to_string(),
"group".to_string(),
"size".to_string(),
"contents".to_string(),
"date".to_string(),
"name".to_string(),
],
hide_columns: vec![],
sort: "name".to_string(),
reverse: false,
// Dirs on top, files at the bottom: with bottom-up numbering the
// files own [1], [2], ... because `f N` opens files while
// directories are reached via cd/z. Override with
// group_dirs = "last"/"none" or --group-dirs.
group_dirs: "first".to_string(),
hyperlink: false,
hidden: false,
highlight_recent: "bold".to_string(), // bold text for recent files
highlight_old: "".to_string(), // no highlight
git_status: true,
build_status: false, // opt-in: build checks spawn subprocesses (cargo check ≈ 6.7s)
todo_count: true,
languages: true,
ports: true,
docker: true,
inline_preview: false, // disabled by default, can be enabled in config
mini_tree: false, // disabled by default - conflicts with inline_preview
smart_truncation: true, // enabled by default - shows most relevant items first
numbered: false, // disabled by default - enable to show [N] navigation numbers
number_from_bottom: true, // bottom-up numbering so `f 1` hits the most recent row
zebra_rows: false, // disabled by default for clean pure-black background
open_command: "micro".to_string(), // default file opener
ignore_dirs: vec![
"node_modules".to_string(),
"target".to_string(),
".git".to_string(),
"dist".to_string(),
"build".to_string(),
".pi".to_string(),
".opencode".to_string(),
".pi-glla".to_string(),
".dracon".to_string(),
".svelte-kit".to_string(),
".claude".to_string(),
".cursor".to_string(),
".cache".to_string(),
],
}
}
}
impl Config {
#[allow(dead_code)]
pub fn config_path() -> Result<PathBuf> {
let proj_dirs = ProjectDirs::from("com", "fab", "fab")
.context("Failed to determine config directory")?;
Ok(proj_dirs.config_dir().join("config.toml"))
}
pub fn load() -> Result<Self> {
let path = Self::config_path();
if let Ok(path) = path {
if path.exists() {
let content = fs::read_to_string(&path).context("Failed to read config")?;
return toml::from_str(&content).context("Failed to parse config TOML");
}
}
Ok(Config::default())
}
#[allow(dead_code)]
pub fn save(&self) -> Result<()> {
let path = Self::config_path()?;
if let Some(parent) = path.parent() {
fs::create_dir_all(parent)?;
}
let content = toml::to_string_pretty(self).context("Failed to serialize config")?;
// Atomic write (temp + rename): a torn write or crash mid-write
// would leave an unparseable config that silently resets to default.
let tmp = path.with_extension("toml.tmp");
fs::write(&tmp, &content).context(format!("Failed to write config to {:?}", tmp))?;
fs::rename(&tmp, &path).context(format!("Failed to write config to {:?}", path))?;
Ok(())
}
}
#[cfg(test)]
mod tests {
use super::*;
#[test]
fn test_config_default() {
let config = Config::default();
assert!(config.icons);
assert!(config.colors);
assert!(!config.compact);
assert_eq!(config.max_display_items, 0);
assert_eq!(config.permission, "rwx");
assert_eq!(config.size, "default");
assert_eq!(config.date, "date");
assert!(!config.classify);
assert!(!config.no_symlink);
assert!(config.total_size);
assert!(config.git_status);
assert!(!config.build_status); // opt-in feature, see Default
assert!(config.todo_count);
assert!(config.languages);
assert!(config.ports);
assert!(config.docker);
assert!(config.ignore_dirs.contains(&"node_modules".to_string()));
assert!(config.ignore_dirs.contains(&".pi".to_string()));
assert!(config.ignore_dirs.contains(&".opencode".to_string()));
}
#[test]
fn test_config_serialization() {
let config = Config::default();
let toml_str = toml::to_string_pretty(&config).unwrap();
assert!(toml_str.contains("icons = true"));
assert!(toml_str.contains("colors = true"));
assert!(toml_str.contains("compact = false"));
let deserialized: Config = toml::from_str(&toml_str).unwrap();
assert_eq!(deserialized.icons, config.icons);
assert_eq!(deserialized.colors, config.colors);
assert_eq!(deserialized.compact, config.compact);
}
}