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
use gtk;
use gtk::prelude::*;
use super::store;
pub struct Builder<'a> {
title: &'a str,
}
impl<'a> Builder<'a> {
pub fn new(title: &'a str) -> Self {
Builder { title }
}
pub fn show<F: IsA<gtk::Window>>(&self, parent: &F) -> Option<store::PlugInfo> {
let dlg = gtk::Dialog::new_with_buttons(
Some(self.title),
Some(parent),
gtk::DialogFlags::USE_HEADER_BAR | gtk::DialogFlags::DESTROY_WITH_PARENT,
&[
("Cancel", gtk::ResponseType::Cancel),
("Ok", gtk::ResponseType::Ok),
],
);
let content = dlg.get_content_area();
let border = gtk::Box::new(gtk::Orientation::Horizontal, 0);
border.set_border_width(12);
let list = gtk::ListBox::new();
list.set_selection_mode(gtk::SelectionMode::None);
let path = gtk::Box::new(gtk::Orientation::Horizontal, 5);
path.set_border_width(5);
let path_lbl = gtk::Label::new(Some("Repo"));
let path_e = gtk::Entry::new();
path_e.set_placeholder_text(Some("user_name/repo_name"));
path.pack_start(&path_lbl, true, true, 0);
path.pack_end(&path_e, false, true, 0);
list.add(&path);
let name = gtk::Box::new(gtk::Orientation::Horizontal, 5);
name.set_border_width(5);
let name_lbl = gtk::Label::new(Some("Name"));
let name_e = gtk::Entry::new();
name.pack_start(&name_lbl, true, true, 0);
name.pack_end(&name_e, false, true, 0);
list.add(&name);
border.pack_start(&list, true, true, 0);
content.add(&border);
content.show_all();
path_e.connect_changed(clone!(name_e => move |p| {
if let Some(name) = p.get_text().and_then(|t| extract_name(&t)) {
name_e.set_text(&name);
}
}));
let res = if dlg.run() == gtk::ResponseType::Ok {
path_e.get_text().map(|path| {
let name = name_e
.get_text()
.map(String::from)
.and_then(|name| {
if name.trim().is_empty() {
None
} else {
Some(name.to_owned())
}
})
.or_else(|| extract_name(path.as_str()))
.unwrap_or_else(|| path.as_str().to_owned());
store::PlugInfo::new(name, path.to_owned())
})
} else {
None
};
dlg.destroy();
res
}
}
fn extract_name(path: &str) -> Option<String> {
if let Some(idx) = path.rfind(|c| c == '/' || c == '\\') {
if idx < path.len() - 1 {
let path = path.trim_end_matches(".git");
Some(path[idx + 1..].to_owned())
} else {
None
}
} else {
None
}
}
#[cfg(test)]
mod tests {
use super::*;
#[test]
fn test_extract_name() {
assert_eq!(
Some("plugin_name".to_owned()),
extract_name("http://github.com/somebody/plugin_name.git")
);
}
}