Add reckless mode

Pressing an invalid key will take you to the "reckless" mode. All you
need to do is calm down, escape that mode, and try again.

Closes: https://github.com/sayanarijit/xplr/issues/142
pull/145/head
Arijit Basu 3 years ago committed by Arijit Basu
parent efec86c616
commit c68bd96253

@ -1237,6 +1237,11 @@ pub enum ExternalMsg {
/// **Example:** `LogSuccess: satellite reached destination`.
LogSuccess(String),
/// Log an warning message.
///
/// **Example:** `LogWarning: satellite is heating`
LogWarning(String),
/// Log an error message.
///
/// **Example:** `LogError: satellite crashed`
@ -1327,6 +1332,7 @@ impl Task {
#[serde(rename_all = "UPPERCASE")]
pub enum LogLevel {
Info,
Warning,
Success,
Error,
}
@ -1367,6 +1373,7 @@ impl std::fmt::Display for Log {
fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result {
let level_str = match self.level {
LogLevel::Info => "INFO ",
LogLevel::Warning => "WARNING",
LogLevel::Success => "SUCCESS",
LogLevel::Error => "ERROR ",
};
@ -1657,6 +1664,7 @@ impl App {
ExternalMsg::ClearNodeSorters => self.clear_node_sorters(),
ExternalMsg::LogInfo(l) => self.log_info(l),
ExternalMsg::LogSuccess(l) => self.log_success(l),
ExternalMsg::LogWarning(l) => self.log_warning(l),
ExternalMsg::LogError(l) => self.log_error(l),
ExternalMsg::Quit => self.quit(),
ExternalMsg::PrintResultAndQuit => self.print_result_and_quit(),
@ -1691,7 +1699,15 @@ impl App {
None
}
})
.unwrap_or_else(|| default.map(|a| a.messages().clone()).unwrap_or_default());
.or_else(|| default.map(|a| a.messages().clone()))
.unwrap_or_else(|| {
vec![
ExternalMsg::SwitchModeBuiltin("reckless".into()),
ExternalMsg::LogWarning(
"You're being reckless. Let's calm down, escape, and try again.".into(),
),
]
});
for msg in msgs {
self = self.enqueue(Task::new(MsgIn::External(msg), Some(key)));
@ -2280,6 +2296,11 @@ impl App {
Ok(self)
}
pub fn log_warning(mut self, message: String) -> Result<Self> {
self.logs.push(Log::new(LogLevel::Warning, message));
Ok(self)
}
pub fn log_error(mut self, message: String) -> Result<Self> {
self.logs.push(Log::new(LogLevel::Error, message));
Ok(self)
@ -2433,6 +2454,7 @@ impl App {
[
builtin.default(),
builtin.reckless(),
builtin.filter(),
builtin.number(),
builtin.go_to(),

@ -323,6 +323,9 @@ pub struct LogsConfig {
#[serde(default)]
success: UiElement,
#[serde(default)]
warning: UiElement,
#[serde(default)]
error: UiElement,
}
@ -331,6 +334,7 @@ impl LogsConfig {
pub fn extend(mut self, other: Self) -> Self {
self.info = self.info.extend(other.info);
self.success = self.success.extend(other.success);
self.warning = self.warning.extend(other.warning);
self.error = self.error.extend(other.error);
self
}
@ -349,6 +353,11 @@ impl LogsConfig {
pub fn error(&self) -> &UiElement {
&self.error
}
/// Get a reference to the logs config's warning.
pub fn warning(&self) -> &UiElement {
&self.warning
}
}
#[derive(Debug, Clone, Default, Serialize, Deserialize)]
@ -839,6 +848,9 @@ pub struct BuiltinModesConfig {
#[serde(default)]
default: Mode,
#[serde(default)]
reckless: Mode,
#[serde(default)]
selection_ops: Mode,
@ -888,6 +900,7 @@ pub struct BuiltinModesConfig {
impl BuiltinModesConfig {
pub fn extend(mut self, other: Self) -> Self {
self.default = self.default.extend(other.default);
self.reckless = self.reckless.extend(other.reckless);
self.selection_ops = self.selection_ops.extend(other.selection_ops);
self.go_to = self.go_to.extend(other.go_to);
self.create = self.create.extend(other.create);
@ -913,6 +926,7 @@ impl BuiltinModesConfig {
pub fn get(&self, name: &str) -> Option<&Mode> {
match name {
"default" => Some(&self.default),
"reckless" => Some(&self.reckless),
"selection ops" => Some(&self.selection_ops),
"selection_ops" => Some(&self.selection_ops),
"create" => Some(&self.create),
@ -1018,6 +1032,11 @@ impl BuiltinModesConfig {
pub fn switch_layout(&self) -> &Mode {
&self.switch_layout
}
/// Get a reference to the builtin modes config's reckless.
pub fn reckless(&self) -> &Mode {
&self.reckless
}
}
#[derive(Debug, Clone, Default, Serialize, Deserialize)]

@ -103,6 +103,10 @@ general:
format: "SUCCESS"
style:
fg: Green
warning:
format: "WARNING"
style:
fg: Yellow
error:
format: "ERROR"
style:
@ -896,19 +900,25 @@ modes:
help: quit
messages:
- Quit
on_alphabet: null
on_number:
help: input
messages:
- ResetInputBuffer
- SwitchModeBuiltin: number
- BufferInputFromKey
on_special_character: null
default:
help: null
messages:
- SwitchModeBuiltin: default
reckless:
name: reckless
key_bindings:
on_key:
esc:
help: back to default mode
messages:
- SwitchModeBuiltin: default
ctrl-c:
help: terminate
messages:
- Terminate
go_to:
name: go to

@ -842,6 +842,19 @@ fn draw_logs<B: Backend>(
l.message()
))
.style(logs_config.info().style().clone().into()),
app::LogLevel::Warning => ListItem::new(format!(
"{} | {} | {}",
&time,
&logs_config
.warning()
.format()
.to_owned()
.unwrap_or_default(),
l.message()
))
.style(logs_config.warning().style().clone().into()),
app::LogLevel::Success => ListItem::new(format!(
"{} | {} | {}",
&time,
@ -853,6 +866,7 @@ fn draw_logs<B: Backend>(
l.message()
))
.style(logs_config.success().style().clone().into()),
app::LogLevel::Error => ListItem::new(format!(
"{} | {} | {}",
&time,

Loading…
Cancel
Save