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
use std::env;
use serde::Deserialize;
#[derive(Debug, Deserialize, Clone)]
pub struct Config {
/// Where should lockbook store data, including logs?
pub writeable_path: String,
/// Should lb do background work like keep search indexes up to date?
pub background_work: bool,
/// Should we log at all?
pub logs: bool,
/// Should logs be printed to stdout?
pub stdout_logs: bool,
/// Should logs be colored?
pub colored_logs: bool,
}
impl Config {
/// Configures lockbook for CLI use with no stdout logs or background work. `writeable_path_subfolder` is generally
/// a hardcoded client name like `"cli"`.
pub fn cli_config(writeable_path_subfolder: &str) -> Config {
Config {
writeable_path: Self::writeable_path(writeable_path_subfolder),
background_work: false,
logs: true,
stdout_logs: false,
colored_logs: true,
}
}
/// Configures lockbook for UI use with stdout logs and background work. `writeable_path_subfolder` is generally
/// a hardcoded client name like `"macos"`.
pub fn ui_config(writeable_path_subfolder: &str) -> Config {
Config {
writeable_path: Self::writeable_path(writeable_path_subfolder),
background_work: true,
logs: true,
stdout_logs: true,
colored_logs: true,
}
}
/// Produces a full writable path for lockbook to use based on environment variables and platform. Useful for
/// initializing the Config struct.
pub fn writeable_path(writeable_path_subfolder: &str) -> String {
let specified_path = env::var("LOCKBOOK_PATH");
let default_path =
env::var("HOME") // unix
.or(env::var("HOMEPATH")) // windows
.map(|home| format!("{home}/.lockbook/{writeable_path_subfolder}"));
let Ok(writeable_path) = specified_path.or(default_path) else {
panic!("no location for lockbook to initialize");
};
writeable_path
}
}
// todo: we added background work as a flag to speed up test execution in debug mode
// turn background work back to true in test_utils to see the slow test
// the slow test primarily does a large amount of allocations due to ownership model
// of treelike. In a universe where these operations could be expressed as iterators
// we would be able to vastly cut down on allocations and eliminate this complexity
//
// another nice aspect of background work is that it is a workaround for CLI's lack
// of graceful shutdown. Ideally, both of these situations will be handled differently.