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
extern crate url;
extern crate toml;
extern crate getopts;
#[macro_use]
extern crate log;
pub mod command;
pub mod config;
pub mod ui;
pub mod optparse;
pub mod script;
mod keybinding;
use ui::*;
use script::ScriptingEngine;
pub const WEBKITTEN_APP_ID: &'static str = "me.delisa.Webkitten";
pub const WEBKITTEN_TITLE: &'static str = "Webkitten";
pub struct Engine {
pub config: config::Config,
run_config: optparse::RunConfiguration,
}
impl Engine {
pub fn new(runtime: optparse::RunConfiguration) -> Option<Self> {
config::Config::parse_file(&runtime.path).and_then(|config| {
info!("Creating application engine with config path: {}", &runtime.path);
Some(Engine {
config: config,
run_config: runtime
})
})
}
pub fn initial_pages<'a>(&'a self) -> &'a Vec<String> {
&self.run_config.start_pages
}
pub fn reload(&mut self) -> bool {
self.config.load(&self.run_config.path)
}
fn use_argument_completion(&self, prefix: &str) -> bool {
prefix.contains(" ")
}
}
impl EventHandler for Engine {
fn on_new_frame_request<T, S>(&self, ui: &T, window_index: u32, uri: &str)
where T: ApplicationUI<S>,
S: ScriptingEngine {
if self.config.new_frame_uses_focused_window() {
ui.open_webview::<_, config::Config>(window_index, Some(uri), None);
} else {
ui.open_window::<_, config::Config>(Some(uri), None);
}
}
fn execute_command<T, S>(&self, ui: &T, window_index: Option<u32>, text: &str)
where T: ApplicationUI<S>,
S: ScriptingEngine {
if let Some(text) = self.config.command_matching_prefix(text) {
return self.execute_command(ui, window_index, &text);
} else if let Some(command) = command::Command::parse(text, &self.config, S::file_extension()) {
info!("Found command match: {}", command.path);
if let Some(file) = command.file() {
match S::execute::<T, S>(file, command.arguments, ui, &self.run_config.path) {
Err(err) => warn!("{}", err),
Ok(success) => if let (true, Some(index)) = (success, window_index) {
ui.set_command_field_text(index, "")
}
}
}
} else if let Some(default) = self.config.default_command() {
if !text.starts_with(&default) {
let mut command = String::from(default);
command.push_str(" ");
command.push_str(text);
info!("Running the default command: {}", command);
return self.execute_command(ui, window_index, &command);
}
}
}
fn close<T, S>(&self, _ui: &T)
where T: ApplicationUI<S>,
S: ScriptingEngine {}
fn command_completions<T, S>(&self, ui: &T, prefix: &str) -> Vec<String>
where T: ApplicationUI<S>,
S: ScriptingEngine {
if self.use_argument_completion(prefix) {
if let Some(command) = command::Command::parse(prefix, &self.config, S::file_extension()) {
info!("Found command match for completion: {}", prefix);
if let Some(file) = command.file() {
info!("Completing command text using {}", command.path);
return match S::autocomplete::<T, S>(file, command.arguments, prefix, ui, &self.run_config.path) {
Err(err) => {
warn!("{}", err);
vec![]
},
Ok(completions) => completions
}
}
}
}
command::Command::list_commands(prefix, &self.config)
}
fn on_buffer_event<T, S>(&self, ui: &T, window_index: u32, webview_index: u32, uri: Option<&str>, event: BufferEvent)
where T: ApplicationUI<S>,
S: ScriptingEngine {
for name in self.config.on_buffer_event_commands(&event) {
if let Some(command) = command::Command::parse(&name, &self.config, S::file_extension()) {
if let Some(file) = command.file() {
match S::on_buffer_event::<T, S>(file, ui, &self.run_config.path, window_index, webview_index, uri, &event) {
Err(err) => warn!("{}", err),
Ok(_) => (),
}
}
}
}
}
}