diff --git a/README.md b/README.md index fde156b..e711c48 100644 --- a/README.md +++ b/README.md @@ -21,6 +21,8 @@ Runs [grep](https://crates.io/crates/grep) ([ripgrep's](https://github.com/Burnt directories are skipped. --editor Text editor used to open selected match. [possible values: check supported text editors section] + --context-viewer Context viewer position at startup [default: none] + [possible values: none, vertical, horizontal] --custom-command Custom command used to open selected match. Must contain {file_name} and {line_number} tokens (check Custom Command section). -g, --glob Include files and directories for searching that match the given glob. diff --git a/src/app.rs b/src/app.rs index 374cf22..372e7d1 100644 --- a/src/app.rs +++ b/src/app.rs @@ -35,6 +35,7 @@ impl App { pub fn new( search_config: SearchConfig, editor_command: EditorCommand, + context_viewer: ContextViewer, theme: Box, ) -> Self { let theme = theme; @@ -42,8 +43,8 @@ impl App { search_config, ig: Ig::new(editor_command), theme, + context_viewer, result_list: ResultList::default(), - context_viewer: ContextViewer::default(), search_popup: SearchPopup::default(), keymap_popup: KeymapPopup::default(), } diff --git a/src/args.rs b/src/args.rs index ba35b03..75eab45 100644 --- a/src/args.rs +++ b/src/args.rs @@ -1,4 +1,7 @@ -use crate::{editor::Editor, ui::theme::ThemeVariant}; +use crate::{ + editor::Editor, + ui::{context_viewer::ContextViewerPosition, theme::ThemeVariant}, +}; use clap::{ArgGroup, CommandFactory, Parser}; use std::{ ffi::OsString, @@ -17,7 +20,7 @@ pub const VISUAL_ENV: &str = "VISUAL"; #[derive(Parser, Debug)] #[clap(author, version, about, long_about = None)] #[clap(group( - ArgGroup::new("excl") + ArgGroup::new("pattern_or_type_list") .args(&["pattern", "type-list"]) .required(true) ))] @@ -59,6 +62,9 @@ pub struct Args { /// Do not search files matching TYPE-NOT. Multiple types-not may be provided. #[clap(short = 'T', long)] pub type_not: Vec, + /// Context viewer position at startup + #[clap(long, value_enum, default_value_t = ContextViewerPosition::None)] + pub context_viewer: ContextViewerPosition, } #[derive(Parser, Debug)] diff --git a/src/main.rs b/src/main.rs index 88795fe..3a538a6 100644 --- a/src/main.rs +++ b/src/main.rs @@ -4,7 +4,10 @@ use igrep::{ args::Args, editor::EditorCommand, ig, - ui::theme::{dark::Dark, light::Light, Theme, ThemeVariant}, + ui::{ + context_viewer::ContextViewer, + theme::{dark::Dark, light::Light, Theme, ThemeVariant}, + }, }; use std::io::Write; @@ -47,6 +50,7 @@ fn main() -> Result<()> { let mut app = App::new( search_config, EditorCommand::new(args.editor.custom_command, args.editor.editor)?, + ContextViewer::new(args.context_viewer), theme, ); app.run()?; diff --git a/src/ui/context_viewer.rs b/src/ui/context_viewer.rs index 35ebc15..7b3b612 100644 --- a/src/ui/context_viewer.rs +++ b/src/ui/context_viewer.rs @@ -5,6 +5,7 @@ use std::{ path::{Path, PathBuf}, }; +use clap::ValueEnum; use itertools::Itertools; use ratatui::{ backend::CrosstermBackend, @@ -22,7 +23,7 @@ use syntect::{ use super::{result_list::ResultList, theme::Theme}; -#[derive(Default, Debug, PartialEq, Eq)] +#[derive(Default, Clone, Debug, PartialEq, Eq, ValueEnum)] pub enum ContextViewerPosition { #[default] None, @@ -40,23 +41,21 @@ pub struct ContextViewer { size: u16, } -impl Default for ContextViewer { - fn default() -> Self { +impl ContextViewer { + const MIN_SIZE: u16 = 20; + const MAX_SIZE: u16 = 80; + const SIZE_CHANGE_DELTA: u16 = 5; + + pub fn new(position: ContextViewerPosition) -> Self { Self { highlighted_file_path: Default::default(), file_highlighted: Default::default(), syntax_set: SyntaxSet::load_defaults_newlines(), theme_set: highlighting::ThemeSet::load_defaults(), - position: Default::default(), + position, size: 50, } } -} - -impl ContextViewer { - const MIN_SIZE: u16 = 20; - const MAX_SIZE: u16 = 80; - const SIZE_CHANGE_DELTA: u16 = 5; pub fn toggle_vertical(&mut self) { match self.position { @@ -236,10 +235,7 @@ mod tests { #[test_case(ContextViewerPosition::Vertical => ContextViewerPosition::None)] #[test_case(ContextViewerPosition::Horizontal => ContextViewerPosition::Vertical)] fn toggle_vertical(initial_position: ContextViewerPosition) -> ContextViewerPosition { - let mut context_viewer = ContextViewer { - position: initial_position, - ..Default::default() - }; + let mut context_viewer = ContextViewer::new(initial_position); context_viewer.toggle_vertical(); context_viewer.position } @@ -248,17 +244,14 @@ mod tests { #[test_case(ContextViewerPosition::Vertical => ContextViewerPosition::Horizontal)] #[test_case(ContextViewerPosition::Horizontal => ContextViewerPosition::None)] fn toggle_horizontal(initial_position: ContextViewerPosition) -> ContextViewerPosition { - let mut context_viewer = ContextViewer { - position: initial_position, - ..Default::default() - }; + let mut context_viewer = ContextViewer::new(initial_position); context_viewer.toggle_horizontal(); context_viewer.position } #[test] fn increase_size() { - let mut context_viewer = ContextViewer::default(); + let mut context_viewer = ContextViewer::new(ContextViewerPosition::None); let default_size = context_viewer.size; context_viewer.increase_size(); assert_eq!( @@ -273,7 +266,7 @@ mod tests { #[test] fn decrease_size() { - let mut context_viewer = ContextViewer::default(); + let mut context_viewer = ContextViewer::new(ContextViewerPosition::None); let default_size = context_viewer.size; context_viewer.decrease_size(); assert_eq!(