Skip to content

Commit

Permalink
Internals for changing the widths of panels (#921)
Browse files Browse the repository at this point in the history
3 new internals:

* `set_panel_width`, taking as parameter the index of the panel and the desired width
* `move_panel_divider`, taking as parameter the index of the divider and the desired change
* `default_layout`, restores the default panel sizes

`ctrl-<` is bound by default to `:move_panel_divider 0 -1`

`ctrl->` is bound by default to `:move_panel_divider 0 1`

It's also possible to define the layout in conf, eg 

```
    layout_instructions: [
         { panel: 0, width: 18 }
     ]
```
  • Loading branch information
Canop authored Aug 3, 2024
1 parent 728a356 commit 33018f4
Show file tree
Hide file tree
Showing 22 changed files with 364 additions and 44 deletions.
6 changes: 6 additions & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,12 @@
ctrl-s now triggers `:search_again` which either
- brings back the last used search pattern, when no filtering pattern is active
- does a "total search" if a filtering pattern is active and the search wasn't complete
#### Major Feature: internals changing panel widths
* `set_panel_width`, taking as parameter the index of the panel and the desired width
* `move_panel_divider`, taking as parameter the index of the divider and the desired change
`ctrl-<` is bound by default to `:move_panel_divider 0 -1`
`ctrl->` is bound by default to `:move_panel_divider 0 1`
See http://dystroy.org/broot/panels/#resize-panels
#### Minor Changes:
- when git file infos are shown, and git ignored files aren't hidden, those files are flagged with a 'I' - Fix #916
- Remove .bak extension from content search exclusion list - Fix #915
Expand Down
39 changes: 24 additions & 15 deletions src/app/app.rs
Original file line number Diff line number Diff line change
Expand Up @@ -8,11 +8,7 @@ use {
Sequence,
},
conf::Conf,
display::{
Areas,
Screen,
W,
},
display::*,
errors::ProgramError,
file_sum,
git,
Expand Down Expand Up @@ -104,7 +100,7 @@ impl App {
let panel = Panel::new(
PanelId::from(0),
browser_state,
Areas::create(&mut Vec::new(), 0, screen, false),
Areas::create(&mut Vec::new(), &con.layout_instructions, 0, screen, false),
con,
);
let (tx_seqs, rx_seqs) = unbounded::<Sequence>();
Expand Down Expand Up @@ -191,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 @@ -214,6 +211,7 @@ impl App {
}
Areas::resize_all(
self.panels.as_mut_slice(),
&con.layout_instructions,
self.screen,
self.preview_panel.is_some(),
);
Expand All @@ -233,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 @@ -363,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 @@ -384,6 +382,15 @@ impl App {
self.quitting = true;
}
}
ChangeLayout(instruction) => {
con.layout_instructions.push(instruction);
Areas::resize_all(
self.panels.as_mut_slice(),
&con.layout_instructions,
self.screen,
self.preview_panel.is_some(),
);
}
DisplayError(txt) => {
error = Some(txt);
}
Expand All @@ -404,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 @@ -413,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 @@ -470,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 @@ -502,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 @@ -551,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 @@ -563,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 @@ -681,6 +688,7 @@ impl App {
let with_preview = purpose.is_preview() || self.preview_panel.is_some();
let areas = Areas::create(
self.panels.as_mut_slice(),
&con.layout_instructions,
insertion_idx,
self.screen,
with_preview,
Expand Down Expand Up @@ -873,6 +881,7 @@ impl App {
self.screen.set_terminal_size(width, height, con);
Areas::resize_all(
self.panels.as_mut_slice(),
&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
3 changes: 3 additions & 0 deletions src/app/cmd_result.rs
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@ use {
crate::{
browser::BrowserState,
command::Sequence,
display::LayoutInstruction,
errors::TreeBuildError,
launchable::Launchable,
verb::Internal,
Expand Down Expand Up @@ -36,6 +37,7 @@ pub enum CmdResult {
validate_purpose: bool,
panel_ref: PanelReference,
},
ChangeLayout(LayoutInstruction),
DisplayError(String),
ExecuteSequence {
sequence: Sequence,
Expand Down Expand Up @@ -110,6 +112,7 @@ impl fmt::Debug for CmdResult {
"{}",
match self {
CmdResult::ApplyOnPanel { .. } => "ApplyOnPanel",
CmdResult::ChangeLayout(_) => "ChangeLayout",
CmdResult::ClosePanel {
validate_purpose: false, ..
} => "CancelPanel",
Expand Down
24 changes: 22 additions & 2 deletions src/app/panel_state.rs
Original file line number Diff line number Diff line change
Expand Up @@ -2,12 +2,12 @@ use {
super::*,
crate::{
command::*,
display::{Screen, W},
display::*,
errors::ProgramError,
flag::Flag,
help::HelpState,
pattern::*,
preview::{PreviewMode, PreviewState},
preview::*,
print,
stage::*,
task_sync::Dam,
Expand Down Expand Up @@ -157,6 +157,25 @@ pub trait PanelState {
validate_purpose: false,
panel_ref: PanelReference::Active,
},
Internal::move_panel_divider => {
let MoveDividerArgs { divider, dx } = get_arg(
input_invocation,
internal_exec,
MoveDividerArgs { divider: 0, dx: 1 },
);
CmdResult::ChangeLayout(LayoutInstruction::MoveDivider { divider, dx })
}
Internal::default_layout => {
CmdResult::ChangeLayout(LayoutInstruction::Clear)
}
Internal::set_panel_width => {
let SetPanelWidthArgs { panel, width } = get_arg(
input_invocation,
internal_exec,
SetPanelWidthArgs { panel: 0, width: 100 },
);
CmdResult::ChangeLayout(LayoutInstruction::SetPanelWidth { panel, width })
}
#[cfg(feature = "trash")]
Internal::purge_trash => {
let res = trash::os_limited::list()
Expand Down Expand Up @@ -1115,3 +1134,4 @@ pub fn get_arg<T: Copy + FromStr>(
.unwrap_or(default)
}


2 changes: 1 addition & 1 deletion src/browser/browser_state.rs
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@ use {
crate::{
app::*,
command::*,
display::{DisplayableTree, Screen, W},
display::*,
errors::{ProgramError, TreeBuildError},
flag::Flag,
git,
Expand Down
2 changes: 1 addition & 1 deletion src/command/panel_input.rs
Original file line number Diff line number Diff line change
Expand Up @@ -72,7 +72,7 @@ impl PanelInput {
/// consume the event to
/// - maybe change the input
/// - build a command
/// then redraw the input field
/// then redraw the input field
#[allow(clippy::too_many_arguments)]
pub fn on_event(
&mut self,
Expand Down
11 changes: 7 additions & 4 deletions src/command/sequence.rs
Original file line number Diff line number Diff line change
Expand Up @@ -67,12 +67,15 @@ impl Sequence {
}
}

/// an input may be made of two parts:
/// Add commands to a sequence.
///
/// An input may be made of two parts:
/// - a search pattern
/// - a verb followed by its arguments
/// we need to build a command for each part so
/// that the search is effectively done before
/// the verb invocation
///
/// We need to build a command for each part so
/// that the search is effectively done before
/// the verb invocation
fn add_commands(
input: &str,
commands: &mut Vec<(String, Command)>,
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
Loading

0 comments on commit 33018f4

Please sign in to comment.