Skip to content

Commit

Permalink
add option to open Context Viewer at startup (#62)
Browse files Browse the repository at this point in the history
  • Loading branch information
konradsz authored Jan 7, 2024
1 parent f2972eb commit 0789d8e
Show file tree
Hide file tree
Showing 5 changed files with 30 additions and 24 deletions.
2 changes: 2 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -21,6 +21,8 @@ Runs [grep](https://crates.io/crates/grep) ([ripgrep's](https://github.com/Burnt
directories are skipped.
--editor <EDITOR> Text editor used to open selected match.
[possible values: check supported text editors section]
--context-viewer <VIEWER> Context viewer position at startup [default: none]
[possible values: none, vertical, horizontal]
--custom-command <COMMAND> Custom command used to open selected match.
Must contain {file_name} and {line_number} tokens (check Custom Command section).
-g, --glob <GLOB> Include files and directories for searching that match the given glob.
Expand Down
3 changes: 2 additions & 1 deletion src/app.rs
Original file line number Diff line number Diff line change
Expand Up @@ -35,15 +35,16 @@ impl App {
pub fn new(
search_config: SearchConfig,
editor_command: EditorCommand,
context_viewer: ContextViewer,
theme: Box<dyn Theme>,
) -> Self {
let theme = theme;
Self {
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(),
}
Expand Down
10 changes: 8 additions & 2 deletions src/args.rs
Original file line number Diff line number Diff line change
@@ -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,
Expand All @@ -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)
))]
Expand Down Expand Up @@ -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<String>,
/// Context viewer position at startup
#[clap(long, value_enum, default_value_t = ContextViewerPosition::None)]
pub context_viewer: ContextViewerPosition,
}

#[derive(Parser, Debug)]
Expand Down
6 changes: 5 additions & 1 deletion src/main.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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;

Expand Down Expand Up @@ -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()?;
Expand Down
33 changes: 13 additions & 20 deletions src/ui/context_viewer.rs
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@ use std::{
path::{Path, PathBuf},
};

use clap::ValueEnum;
use itertools::Itertools;
use ratatui::{
backend::CrosstermBackend,
Expand All @@ -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,
Expand All @@ -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 {
Expand Down Expand Up @@ -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
}
Expand All @@ -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!(
Expand All @@ -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!(
Expand Down

0 comments on commit 0789d8e

Please sign in to comment.