Skip to content
This repository has been archived by the owner on Apr 6, 2023. It is now read-only.

Commit

Permalink
reorganize settings to allow them to be overwritten by CLI arguments
Browse files Browse the repository at this point in the history
  • Loading branch information
mrjones2014 committed Dec 19, 2021
1 parent 8c6805d commit b0625f6
Show file tree
Hide file tree
Showing 9 changed files with 108 additions and 56 deletions.
2 changes: 1 addition & 1 deletion Cargo.lock

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

31 changes: 0 additions & 31 deletions src/commands/ctrlgcommand.rs

This file was deleted.

28 changes: 28 additions & 0 deletions src/commands/find.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,28 @@
use structopt::StructOpt;

use crate::{
dirs::{get_dirs, GetDirsError},
finder::find,
settings::Settings,
};

#[derive(StructOpt)]
pub struct Cmd {
#[structopt(long)]
pub search_dirs: Option<Vec<String>>,
#[structopt(long)]
pub preview_files: Option<Vec<String>>,
#[structopt(long)]
pub preview: Option<bool>,
#[structopt(long)]
pub preview_with_bat: Option<bool>,
}

impl Cmd {
pub fn run(&self) -> Result<Option<String>, GetDirsError> {
Settings::merge_find_args(self);
let dirs = get_dirs()?;
let selected = find(&dirs);
Ok(selected)
}
}
18 changes: 5 additions & 13 deletions src/commands/init.rs
Original file line number Diff line number Diff line change
Expand Up @@ -11,20 +11,12 @@ pub enum Cmd {
}

impl Cmd {
pub fn run(&self) {
pub fn run(&self) -> String {
match self {
Cmd::Fish => {
let script = include_str!("./shell/ctrlg.fish");
println!("{}", script);
}
Cmd::Bash => {
let script = include_str!("./shell/ctrlg.bash");
println!("{}", script);
}
Cmd::Zsh => {
let script = include_str!("./shell/ctrlg.zsh");
println!("{}", script);
}
Cmd::Fish => include_str!("./shell/ctrlg.fish"),
Cmd::Bash => include_str!("./shell/ctrlg.bash"),
Cmd::Zsh => include_str!("./shell/ctrlg.zsh"),
}
.to_string()
}
}
32 changes: 31 additions & 1 deletion src/commands/mod.rs
Original file line number Diff line number Diff line change
@@ -1,2 +1,32 @@
pub mod ctrlgcommand;
use std::error::Error;
use structopt::StructOpt;

pub mod find;
pub mod init;

#[derive(StructOpt)]
pub enum CtrlgCommand {
#[structopt(about = "Find a directory based on configured globbing patterns")]
Find(find::Cmd),
#[structopt(about = "Set up ctrl+g keybind for specified shell")]
Init(init::Cmd),
}

impl CtrlgCommand {
pub fn run(self) -> Result<(), Box<dyn Error>> {
match self {
CtrlgCommand::Find(cmd) => {
let selected = cmd.run()?;
if let Some(selected) = selected {
println!("{}", selected);
}
Ok(())
}
CtrlgCommand::Init(cmd) => {
let script = cmd.run();
println!("{}", script);
Ok(())
}
}
}
}
6 changes: 3 additions & 3 deletions src/dirs.rs
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
use crate::settings::SETTINGS;
use crate::settings::Settings;
use glob::{glob, GlobError};
use std::{error::Error, fmt::Display, io, path::PathBuf};

Expand All @@ -10,7 +10,7 @@ pub struct DirItem {
}

fn get_readme(path: PathBuf) -> Result<Option<String>, io::Error> {
for glob_pattern in SETTINGS.preview_files.iter() {
for glob_pattern in Settings::get_readonly().preview_files.iter() {
let mut preview_file_pattern = path.clone();
preview_file_pattern.push(glob_pattern);

Expand Down Expand Up @@ -66,7 +66,7 @@ impl Display for GetDirsError {

pub fn get_dirs() -> Result<Vec<DirItem>, GetDirsError> {
let mut items = Vec::new();
for dir in SETTINGS.search_dirs.iter() {
for dir in Settings::get_readonly().search_dirs.iter() {
let dir = shellexpand::tilde(dir);
for child in glob(&dir).expect("Failed to resolve globbing pattern") {
let path = child?;
Expand Down
10 changes: 7 additions & 3 deletions src/finder.rs
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
use crate::dirs::DirItem;
use crate::settings::SETTINGS;
use crate::settings::{Settings, SETTINGS};
use skim::prelude::*;
use skim::{prelude::unbounded, SkimItem, SkimItemReceiver, SkimItemSender};
use std::{borrow::Cow, path::PathBuf, sync::Arc};
Expand Down Expand Up @@ -28,7 +28,7 @@ impl SkimItem for DirItem {
}

let readme_path = self.readme.as_ref().unwrap();
if SETTINGS.preview_with_bat {
if SETTINGS.lock().unwrap().preview_with_bat {
ItemPreview::Command(format!(
"bat --style=plain --color=always \"{}\"",
readme_path
Expand All @@ -51,7 +51,11 @@ fn receiver(items: &[DirItem]) -> SkimItemReceiver {
pub fn find(items: &[DirItem]) -> Option<String> {
let skim_options = SkimOptionsBuilder::default()
.height(Some("100%"))
.preview(if SETTINGS.preview { Some("") } else { None })
.preview(if Settings::get_readonly().preview {
Some("")
} else {
None
})
.multi(false)
.build()
.unwrap();
Expand Down
2 changes: 1 addition & 1 deletion src/main.rs
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
#[macro_use]
extern crate lazy_static;
use commands::ctrlgcommand::CtrlgCommand;
use commands::CtrlgCommand;
use std::error::Error;
use structopt::{clap::AppSettings, StructOpt};

Expand Down
35 changes: 32 additions & 3 deletions src/settings.rs
Original file line number Diff line number Diff line change
@@ -1,9 +1,11 @@
use config::{Config, ConfigError, File};
use dirs_next::home_dir;
use serde::Deserialize;
use std::path::PathBuf;
use std::{path::PathBuf, sync::Mutex};

#[derive(Deserialize)]
use crate::commands::find;

#[derive(Debug, Deserialize, Clone)]
pub struct Settings {
pub search_dirs: Vec<String>,
pub preview_files: Vec<String>,
Expand Down Expand Up @@ -41,8 +43,35 @@ impl Settings {

s.try_into()
}

pub fn merge_find_args(find_args: &find::Cmd) {
let mut settings_mut = SETTINGS.lock().unwrap();

if let Some(search_dirs) = &find_args.search_dirs {
settings_mut.search_dirs = search_dirs.clone();
}

if let Some(preview_files) = &find_args.preview_files {
settings_mut.preview_files = preview_files.clone();
}

if let Some(preview) = find_args.preview {
settings_mut.preview = preview;
}

if let Some(preview_with_bat) = find_args.preview_with_bat {
settings_mut.preview_with_bat = preview_with_bat;
}
}

pub fn get_readonly() -> Self {
let settings_lock = SETTINGS.lock().unwrap();
let settings = settings_lock.clone();
drop(settings_lock);
settings
}
}

lazy_static! {
pub static ref SETTINGS: Settings = Settings::new().unwrap();
pub static ref SETTINGS: Mutex<Settings> = Mutex::from(Settings::new().unwrap());
}

0 comments on commit b0625f6

Please sign in to comment.