:feat: Add hooks for mode and layout changes

pull/560/head
emanuel 1 year ago committed by Arijit Basu
parent 078da205ca
commit d6e33e68e3

@ -382,7 +382,7 @@ impl App {
self
}
pub fn handle_batch_external_msgs(mut self, msgs: Vec<ExternalMsg>) -> Result<Self> {
pub fn handle_batch_external_msgs(self, msgs: Vec<ExternalMsg>) -> Result<Self> {
for task in msgs
.into_iter()
.map(|msg| Task::new(MsgIn::External(msg), None))
@ -1112,6 +1112,13 @@ impl App {
self.config.general.read_only,
self.config.general.global_key_bindings.to_owned(),
);
// Hooks
if !self.hooks.on_mode_switch.is_empty() {
let msgs = self.hooks.on_mode_switch.clone();
self = self.handle_batch_external_msgs(msgs)?
}
Ok(self)
} else {
self.log_error(format!("Mode not found: {}", mode))
@ -1130,6 +1137,13 @@ impl App {
self.config.general.read_only,
self.config.general.global_key_bindings.to_owned(),
);
// Hooks
if !self.hooks.on_mode_switch.is_empty() {
let msgs = self.hooks.on_mode_switch.clone();
self = self.handle_batch_external_msgs(msgs)?
}
Ok(self)
} else {
self.log_error(format!("Builtin mode not found: {}", mode))
@ -1148,6 +1162,13 @@ impl App {
self.config.general.read_only,
self.config.general.global_key_bindings.to_owned(),
);
// Hooks
if !self.hooks.on_mode_switch.is_empty() {
let msgs = self.hooks.on_mode_switch.clone();
self = self.handle_batch_external_msgs(msgs)?
}
Ok(self)
} else {
self.log_error(format!("Custom mode not found: {}", mode))
@ -1157,6 +1178,13 @@ impl App {
fn switch_layout(mut self, layout: &str) -> Result<Self> {
if let Some(l) = self.config.layouts.get(layout) {
self.layout = l.to_owned();
// Hooks
if !self.hooks.on_layout_switch.is_empty() {
let msgs = self.hooks.on_layout_switch.clone();
self = self.handle_batch_external_msgs(msgs)?
}
Ok(self)
} else {
self.log_error(format!("Layout not found: {}", layout))
@ -1166,6 +1194,13 @@ impl App {
fn switch_layout_builtin(mut self, layout: &str) -> Result<Self> {
if let Some(l) = self.config.layouts.builtin.get(layout) {
self.layout = l.to_owned();
// Hooks
if !self.hooks.on_layout_switch.is_empty() {
let msgs = self.hooks.on_layout_switch.clone();
self = self.handle_batch_external_msgs(msgs)?
}
Ok(self)
} else {
self.log_error(format!("Builtin layout not found: {}", layout))
@ -1175,6 +1210,13 @@ impl App {
fn switch_layout_custom(mut self, layout: &str) -> Result<Self> {
if let Some(l) = self.config.layouts.get_custom(layout) {
self.layout = l.to_owned();
// Hooks
if !self.hooks.on_layout_switch.is_empty() {
let msgs = self.hooks.on_layout_switch.clone();
self = self.handle_batch_external_msgs(msgs)?
}
Ok(self)
} else {
self.log_error(format!("Custom layout not found: {}", layout))

@ -624,6 +624,12 @@ pub struct Hooks {
#[serde(default)]
pub on_focus_change: Vec<ExternalMsg>,
#[serde(default)]
pub on_mode_switch: Vec<ExternalMsg>,
#[serde(default)]
pub on_layout_switch: Vec<ExternalMsg>,
// TODO After cleanup or Runner::run
// #[serde(default)]
// pub before_quit: Vec<ExternalMsg>,
@ -634,6 +640,8 @@ impl Hooks {
self.on_load.extend(other.on_load);
self.on_directory_change.extend(other.on_directory_change);
self.on_focus_change.extend(other.on_focus_change);
self.on_mode_switch.extend(other.on_mode_switch);
self.on_layout_switch.extend(other.on_layout_switch);
self
}
}

@ -1048,7 +1048,7 @@ xplr.config.modes.builtin.default = {
BashExec = [===[
[ -z "$PAGER" ] && PAGER="less -+F"
cat -- "${XPLR_PIPE_GLOBAL_HELP_MENU_OUT}" | ${PAGER:?}
]===],
]===] ,
},
},
},
@ -1157,7 +1157,7 @@ xplr.config.modes.builtin.default = {
BashExecSilently0 = [===[
NAME=$(basename "${XPLR_FOCUS_PATH:?}")
"$XPLR" -m 'SetInputBuffer: %q' "${NAME:?}"
]===],
]===] ,
},
},
},
@ -1170,7 +1170,7 @@ xplr.config.modes.builtin.default = {
BashExecSilently0 = [===[
NAME=$(basename "${XPLR_FOCUS_PATH:?}")
"$XPLR" -m 'SetInputBuffer: %q' "${NAME:?}"
]===],
]===] ,
},
},
},
@ -1206,7 +1206,7 @@ xplr.config.modes.builtin.default = {
{
BashExecSilently0 = [===[
"$XPLR" -m 'ChangeDirectory: %q' "${HOME:?}"
]===],
]===] ,
},
},
},
@ -1247,21 +1247,21 @@ xplr.config.modes.builtin.default = {
}
xplr.config.modes.builtin.default.key_bindings.on_key["tab"] =
xplr.config.modes.builtin.default.key_bindings.on_key["ctrl-i"]
xplr.config.modes.builtin.default.key_bindings.on_key["ctrl-i"]
xplr.config.modes.builtin.default.key_bindings.on_key["v"] =
xplr.config.modes.builtin.default.key_bindings.on_key["space"]
xplr.config.modes.builtin.default.key_bindings.on_key["space"]
xplr.config.modes.builtin.default.key_bindings.on_key["V"] =
xplr.config.modes.builtin.default.key_bindings.on_key["ctrl-a"]
xplr.config.modes.builtin.default.key_bindings.on_key["ctrl-a"]
xplr.config.modes.builtin.default.key_bindings.on_key["/"] =
xplr.config.modes.builtin.default.key_bindings.on_key["ctrl-f"]
xplr.config.modes.builtin.default.key_bindings.on_key["ctrl-f"]
xplr.config.modes.builtin.default.key_bindings.on_key["h"] =
xplr.config.modes.builtin.default.key_bindings.on_key["left"]
xplr.config.modes.builtin.default.key_bindings.on_key["left"]
xplr.config.modes.builtin.default.key_bindings.on_key["j"] =
xplr.config.modes.builtin.default.key_bindings.on_key["down"]
xplr.config.modes.builtin.default.key_bindings.on_key["down"]
xplr.config.modes.builtin.default.key_bindings.on_key["k"] =
xplr.config.modes.builtin.default.key_bindings.on_key["up"]
xplr.config.modes.builtin.default.key_bindings.on_key["up"]
xplr.config.modes.builtin.default.key_bindings.on_key["l"] =
xplr.config.modes.builtin.default.key_bindings.on_key["right"]
xplr.config.modes.builtin.default.key_bindings.on_key["right"]
-- The builtin debug error mode.
--
@ -1294,7 +1294,7 @@ xplr.config.modes.builtin.debug_error = {
To disable this mode, set `xplr.config.general.disable_debug_error_mode`
to `true` in your config file.
]],
]] ,
},
},
},
@ -1311,7 +1311,7 @@ xplr.config.modes.builtin.debug_error = {
{
BashExec = [===[
cat "${XPLR_PIPE_LOGS_OUT:?}" | ${EDITOR:-vi} -
]===],
]===] ,
},
},
},
@ -1347,7 +1347,7 @@ xplr.config.modes.builtin.recover = {
To disable this mode, set `xplr.config.general.enable_recover_mode`
to `false` in your config file.
]],
]] ,
},
},
},
@ -1380,7 +1380,7 @@ xplr.config.modes.builtin.go_to_path = {
else
"$XPLR" -m 'LogError: %q' "could not find $PTH_ESC"
fi
]===],
]===] ,
},
"PopMode",
},
@ -1424,7 +1424,7 @@ xplr.config.modes.builtin.selection_ops = {
"$XPLR" -m ExplorePwdAsync
"$XPLR" -m ClearSelection
read -p "[enter to continue]"
]===],
]===] ,
},
"PopMode",
},
@ -1444,7 +1444,7 @@ xplr.config.modes.builtin.selection_ops = {
done < "${XPLR_PIPE_SELECTION_OUT:?}")
"$XPLR" -m ExplorePwdAsync
read -p "[enter to continue]"
]===],
]===] ,
},
"PopMode",
},
@ -1518,7 +1518,7 @@ xplr.config.modes.builtin.create_directory = {
else
"$XPLR" -m PopMode
fi
]===],
]===] ,
},
},
},
@ -1562,7 +1562,7 @@ xplr.config.modes.builtin.create_file = {
else
"$XPLR" -m PopMode
fi
]===],
]===] ,
},
},
},
@ -1620,9 +1620,9 @@ xplr.config.modes.builtin.number = {
}
xplr.config.modes.builtin.number.key_bindings.on_key["j"] =
xplr.config.modes.builtin.number.key_bindings.on_key["down"]
xplr.config.modes.builtin.number.key_bindings.on_key["down"]
xplr.config.modes.builtin.number.key_bindings.on_key["k"] =
xplr.config.modes.builtin.number.key_bindings.on_key["up"]
xplr.config.modes.builtin.number.key_bindings.on_key["up"]
-- The builtin go to mode.
--
@ -1661,7 +1661,7 @@ xplr.config.modes.builtin.go_to = {
{
BashExecSilently0 = [===[
"$XPLR" -m 'ChangeDirectory: %q' "${XPLR_INITIAL_PWD:?}"
]===],
]===] ,
},
},
},
@ -1683,7 +1683,7 @@ xplr.config.modes.builtin.go_to = {
(while IFS= read -r -d '' PTH; do
$OPENER "${PTH:?}" > /dev/null 2>&1
done < "${XPLR_PIPE_RESULT_OUT:?}")
]===],
]===] ,
},
"ClearScreen",
"PopMode",
@ -1723,7 +1723,7 @@ xplr.config.modes.builtin.rename = {
&& "$XPLR" -m 'FocusPath: %q' "$TARGET" \
&& "$XPLR" -m 'LogSuccess: %q' "$SRC_ESC renamed to $TARGET_ESC"
fi
]===],
]===] ,
},
"PopMode",
},
@ -1767,7 +1767,7 @@ xplr.config.modes.builtin.duplicate_as = {
&& "$XPLR" -m 'FocusPath: %q' "$TARGET_ESC" \
&& "$XPLR" -m 'LogSuccess: %q' "$SRC_ESC duplicated as $TARGET_ESC"
fi
]===],
]===] ,
},
"PopMode",
},
@ -1804,7 +1804,7 @@ xplr.config.modes.builtin.delete = {
done < "${XPLR_PIPE_RESULT_OUT:?}")
"$XPLR" -m ExplorePwdAsync
read -p "[enter to continue]"
]===],
]===] ,
},
"PopMode",
},
@ -1832,7 +1832,7 @@ xplr.config.modes.builtin.delete = {
done < "${XPLR_PIPE_RESULT_OUT:?}")
"$XPLR" -m ExplorePwdAsync
read -p "[enter to continue]"
]===],
]===] ,
},
"PopMode",
},
@ -1870,7 +1870,7 @@ xplr.config.modes.builtin.action = {
{
BashExec0 = [===[
${EDITOR:-vi} "${XPLR_FOCUS_PATH:?}"
]===],
]===] ,
},
"PopMode",
},
@ -1882,7 +1882,7 @@ xplr.config.modes.builtin.action = {
BashExec = [===[
[ -z "$PAGER" ] && PAGER="less -+F"
cat -- "${XPLR_PIPE_LOGS_OUT}" | ${PAGER:?}
]===],
]===] ,
},
"PopMode",
},
@ -2036,9 +2036,9 @@ xplr.config.modes.builtin.search = {
}
xplr.config.modes.builtin.search.key_bindings.on_key["ctrl-n"] =
xplr.config.modes.builtin.search.key_bindings.on_key["down"]
xplr.config.modes.builtin.search.key_bindings.on_key["down"]
xplr.config.modes.builtin.search.key_bindings.on_key["ctrl-p"] =
xplr.config.modes.builtin.search.key_bindings.on_key["up"]
xplr.config.modes.builtin.search.key_bindings.on_key["up"]
-- The builtin filter mode.
--
@ -2369,7 +2369,7 @@ xplr.config.modes.builtin.vroot = {
{
BashExecSilently0 = [===[
"$XPLR" -m 'SetVroot: %q' "${PWD:?}"
]===],
]===] ,
},
},
},
@ -2387,7 +2387,7 @@ xplr.config.modes.builtin.vroot = {
{
BashExecSilently0 = [===[
"$XPLR" -m 'SetVroot: %q' "${HOME:?}"
]===],
]===] ,
},
},
},
@ -2703,6 +2703,8 @@ xplr.fn.custom = {}
-- { LogSuccess = "Changed focus" },
-- { CallLuaSilently = "custom.some_plugin_with_hooks.on_focus_change" },
-- }
--
-- ...
-- }
-- ```
@ -2710,4 +2712,6 @@ return {
on_load = {},
on_directory_change = {},
on_focus_change = {},
on_mode_switch = {},
on_layout_switch = {},
}

Loading…
Cancel
Save