-
Notifications
You must be signed in to change notification settings - Fork 33
/
Copy pathsupport.rs
148 lines (121 loc) · 3.87 KB
/
support.rs
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
use tracing::error;
use notedeck::{DataPath, DataPathType, Directory};
pub struct Support {
directory: Directory,
mailto_url: String,
most_recent_log: Option<String>,
}
fn new_log_dir(paths: &DataPath) -> Directory {
Directory::new(paths.path(DataPathType::Log))
}
impl Support {
pub fn new(path: &DataPath) -> Self {
let directory = new_log_dir(path);
Self {
mailto_url: MailtoBuilder::new(SUPPORT_EMAIL.to_string())
.with_subject("Help Needed".to_owned())
.with_content(EMAIL_TEMPLATE.to_owned())
.build(),
directory,
most_recent_log: None,
}
}
}
static MAX_LOG_LINES: usize = 500;
static EMAIL_TEMPLATE: &str = concat!("version ", env!("CARGO_PKG_VERSION"), "\nCommit hash: ", env!("GIT_COMMIT_HASH"), "\n\nDescribe the bug you have encountered:\n<-- your statement here -->\n\n===== Paste your log below =====\n\n");
impl Support {
pub fn refresh(&mut self) {
self.most_recent_log = get_log_str(&self.directory);
}
pub fn get_mailto_url(&self) -> &str {
&self.mailto_url
}
pub fn get_log_dir(&self) -> Option<&str> {
self.directory.file_path.to_str()
}
pub fn get_most_recent_log(&self) -> Option<&String> {
self.most_recent_log.as_ref()
}
}
fn get_log_str(interactor: &Directory) -> Option<String> {
match interactor.get_most_recent() {
Ok(Some(most_recent_name)) => {
match interactor.get_file_last_n_lines(most_recent_name.clone(), MAX_LOG_LINES) {
Ok(file_output) => {
return Some(
get_prefix(
&most_recent_name,
file_output.output_num_lines,
file_output.total_lines_in_file,
) + &file_output.output,
)
}
Err(e) => {
error!(
"Error retrieving the last lines from file {}: {:?}",
most_recent_name, e
);
}
}
}
Ok(None) => {
error!("No files were found.");
}
Err(e) => {
error!("Error fetching the most recent file: {:?}", e);
}
}
None
}
fn get_prefix(file_name: &str, lines_displayed: usize, num_total_lines: usize) -> String {
format!(
"===\nDisplaying the last {} of {} lines in file {}\n===\n\n",
lines_displayed, num_total_lines, file_name,
)
}
struct MailtoBuilder {
content: Option<String>,
address: String,
subject: Option<String>,
}
impl MailtoBuilder {
fn new(address: String) -> Self {
Self {
content: None,
address,
subject: None,
}
}
// will be truncated so the whole URL is at most 2000 characters
pub fn with_content(mut self, content: String) -> Self {
self.content = Some(content);
self
}
pub fn with_subject(mut self, subject: String) -> Self {
self.subject = Some(subject);
self
}
pub fn build(self) -> String {
let mut url = String::new();
url.push_str("mailto:");
url.push_str(&self.address);
let has_subject = self.subject.is_some();
if has_subject || self.content.is_some() {
url.push('?');
}
if let Some(subject) = self.subject {
url.push_str("subject=");
url.push_str(&urlencoding::encode(&subject));
}
if let Some(content) = self.content {
if has_subject {
url.push('&');
}
url.push_str("body=");
let body = urlencoding::encode(&content);
url.push_str(&body);
}
url
}
}