Skip to content

Commit

Permalink
allow defining layout with instructions in conf
Browse files Browse the repository at this point in the history
  • Loading branch information
Canop committed Aug 1, 2024
1 parent 7d236da commit 64abd43
Show file tree
Hide file tree
Showing 4 changed files with 33 additions and 25 deletions.
36 changes: 16 additions & 20 deletions src/app/app.rs
Original file line number Diff line number Diff line change
Expand Up @@ -54,9 +54,6 @@ pub struct App {
/// the panels of the application, at least one
panels: NonEmptyVec<Panel>,

/// layout modifiers, like divider moves
layout_instructions: LayoutInstructions,

/// index of the currently focused panel
active_panel_idx: usize,

Expand Down Expand Up @@ -100,19 +97,17 @@ impl App {
if let Some(path) = con.initial_file.as_ref() {
browser_state.tree.try_select_path(path);
}
let layout_instructions = Default::default();
let panel = Panel::new(
PanelId::from(0),
browser_state,
Areas::create(&mut Vec::new(), &layout_instructions, 0, screen, false),
Areas::create(&mut Vec::new(), &con.layout_instructions, 0, screen, false),
con,
);
let (tx_seqs, rx_seqs) = unbounded::<Sequence>();
let mut app = App {
screen,
active_panel_idx: 0,
panels: panel.into(),
layout_instructions,
quitting: false,
launch_at_end: None,
created_panels_count: 1,
Expand Down Expand Up @@ -192,6 +187,7 @@ impl App {
fn close_panel(
&mut self,
panel_idx: usize,
con: &AppContext,
) -> bool {
let active_panel_id = self.panels[self.active_panel_idx].id;
if let Some(preview_id) = self.preview_panel {
Expand All @@ -215,7 +211,7 @@ impl App {
}
Areas::resize_all(
self.panels.as_mut_slice(),
&self.layout_instructions,
&con.layout_instructions,
self.screen,
self.preview_panel.is_some(),
);
Expand All @@ -235,9 +231,9 @@ impl App {
/// Close the panel too if that was its only state.
/// Close nothing and return false if there's not
/// at least two states in the app.
fn remove_state(&mut self) -> bool {
fn remove_state(&mut self, con: &AppContext) -> bool {
self.panels[self.active_panel_idx].remove_state()
|| self.close_panel(self.active_panel_idx)
|| self.close_panel(self.active_panel_idx, con)
}

/// redraw the whole screen. All drawing
Expand Down Expand Up @@ -365,7 +361,7 @@ impl App {
.map(|p| p.to_string_lossy().to_string());
}
}
if self.close_panel(close_idx) {
if self.close_panel(close_idx, con) {
let screen = self.screen;
self.mut_state().refresh(screen, con);
if let Some(new_arg) = new_arg {
Expand All @@ -387,10 +383,10 @@ impl App {
}
}
ChangeLayout(instruction) => {
self.layout_instructions.add(instruction);
con.layout_instructions.push(instruction);
Areas::resize_all(
self.panels.as_mut_slice(),
&self.layout_instructions,
&con.layout_instructions,
self.screen,
self.preview_panel.is_some(),
);
Expand All @@ -415,7 +411,7 @@ impl App {
// we're here because the state wants us to either move to the panel
// to the left, or close the rightest one
if self.active_panel_idx == 0 {
self.close_panel(self.panels.len().get() - 1);
self.close_panel(self.panels.len().get() - 1, con);
None
} else {
Some(self.active_panel_idx - 1)
Expand All @@ -424,7 +420,7 @@ impl App {
// panel_right
// we either move to the right or close the leftest panel
if self.active_panel_idx + 1 == self.panels.len().get() {
self.close_panel(0);
self.close_panel(0, con);
None
} else {
Some(self.active_panel_idx + 1)
Expand Down Expand Up @@ -481,7 +477,7 @@ impl App {
if panels_count >= con.max_panels_count {
for i in (0..panels_count).rev() {
if self.panels[i].state().get_type() != PanelStateType::Tree {
self.close_panel(i);
self.close_panel(i, con);
break;
}
}
Expand Down Expand Up @@ -513,7 +509,7 @@ impl App {
if i == self.active_panel_idx {
continue;
}
self.close_panel(i);
self.close_panel(i, con);
break;
}
}
Expand Down Expand Up @@ -562,7 +558,7 @@ impl App {
if is_input_invocation {
self.mut_panel().clear_input();
}
if self.remove_state() {
if self.remove_state(con) {
self.mut_state().refresh(app_cmd_context.screen, con);
self.mut_panel()
.refresh_input_status(app_state, &app_cmd_context);
Expand All @@ -574,7 +570,7 @@ impl App {
if is_input_invocation {
self.mut_panel().clear_input();
}
if self.remove_state() {
if self.remove_state(con) {
let app_cmd_context = AppCmdContext {
panel_skin,
preview_panel: self.preview_panel,
Expand Down Expand Up @@ -692,7 +688,7 @@ impl App {
let with_preview = purpose.is_preview() || self.preview_panel.is_some();
let areas = Areas::create(
self.panels.as_mut_slice(),
&self.layout_instructions,
&con.layout_instructions,
insertion_idx,
self.screen,
with_preview,
Expand Down Expand Up @@ -885,7 +881,7 @@ impl App {
self.screen.set_terminal_size(width, height, con);
Areas::resize_all(
self.panels.as_mut_slice(),
&self.layout_instructions,
&con.layout_instructions,
self.screen,
self.preview_panel.is_some(),
);
Expand Down
7 changes: 6 additions & 1 deletion src/app/app_context.rs
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@ use {
cli::{Args, TriBool},
conf::*,
content_search,
display::LayoutInstructions,
errors::*,
file_sum,
icon::*,
Expand Down Expand Up @@ -131,6 +132,9 @@ pub struct AppContext {

/// The set of transformers called before previewing a file
pub preview_transformers: PreviewTransformers,

/// layout modifiers, like divider moves
pub layout_instructions: LayoutInstructions,
}

impl AppContext {
Expand Down Expand Up @@ -200,8 +204,8 @@ impl AppContext {
.unwrap_or(content_search::DEFAULT_MAX_FILE_SIZE);

let terminal_title_pattern = config.terminal_title.clone();

let preview_transformers = PreviewTransformers::new(&config.preview_transformers)?;
let layout_instructions = config.layout_instructions.clone().unwrap_or_default();

Ok(Self {
is_tty,
Expand Down Expand Up @@ -236,6 +240,7 @@ impl AppContext {
lines_after_match_in_preview: config.lines_after_match_in_preview.unwrap_or(0),
lines_before_match_in_preview: config.lines_before_match_in_preview.unwrap_or(0),
preview_transformers,
layout_instructions,
})
}
/// Return the --cmd argument, coming from the launch arguments (prefered)
Expand Down
6 changes: 5 additions & 1 deletion src/conf/conf.rs
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@ use {
super::*,
crate::{
app::Mode,
display::ColsConf,
display::{ColsConf, LayoutInstructions},
errors::{ConfError, ProgramError},
kitty::TransmissionMedium,
path::{
Expand Down Expand Up @@ -143,6 +143,9 @@ pub struct Conf {
#[serde(default)]
pub verbs: Vec<VerbConf>,

#[serde(alias="layout-instructions")]
pub layout_instructions: Option<LayoutInstructions>,

// BEWARE: entries added here won't be usable unless also
// added in read_file!
}
Expand Down Expand Up @@ -232,6 +235,7 @@ impl Conf {
overwrite!(self, kitty_graphics_transmission, conf);
overwrite!(self, lines_after_match_in_preview, conf);
overwrite!(self, lines_before_match_in_preview, conf);
overwrite!(self, layout_instructions, conf);
self.verbs.append(&mut conf.verbs);
// the following prefs are "additive": we can add entries from several
// config files and they still make sense
Expand Down
9 changes: 6 additions & 3 deletions src/display/layout_instructions.rs
Original file line number Diff line number Diff line change
@@ -1,14 +1,17 @@
use {
lazy_regex::*,
serde::Deserialize,
std::str::FromStr,
};

#[derive(Debug, Clone, Default)]
#[derive(Debug, Clone, Default, Deserialize)]
#[serde(transparent)]
pub struct LayoutInstructions {
pub instructions: Vec<LayoutInstruction>,
}

#[derive(Debug, Clone, Copy)]
#[derive(Debug, Clone, Copy, Deserialize)]
#[serde(untagged)]
pub enum LayoutInstruction {
Clear, // clear all instructions
MoveDivider { divider: usize, dx: i16 },
Expand Down Expand Up @@ -69,7 +72,7 @@ impl LayoutInstruction {
}

impl LayoutInstructions {
pub fn add(
pub fn push(
&mut self,
new_instruction: LayoutInstruction,
) {
Expand Down

0 comments on commit 64abd43

Please sign in to comment.