Skip to content

Commit

Permalink
refactor: Return String from fuzzy_finder::run()
Browse files Browse the repository at this point in the history
  • Loading branch information
kyu08 committed Oct 9, 2023
1 parent 1a924d6 commit 35e06e8
Show file tree
Hide file tree
Showing 7 changed files with 50 additions and 49 deletions.
6 changes: 3 additions & 3 deletions src/controller/controller_main.rs
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
use std::sync::Arc;
use std::{collections::HashMap, env};

use crate::usecase::{fzf_make, help, invalid_arg, usecase, version};
use crate::usecase::{fzf_make_main, help, invalid_arg, usecase, version};

pub fn run() {
let command_line_args = env::args().collect();
Expand All @@ -18,7 +18,7 @@ fn args_to_usecase(args: Vec<String>) -> Arc<dyn usecase::Usecase> {

let command = match args.get(1) {
Some(s) => s,
None => return Arc::new(fzf_make::FzfMake),
None => return Arc::new(fzf_make_main::FzfMake),
};

match usecases().get(command.as_str()) {
Expand All @@ -29,7 +29,7 @@ fn args_to_usecase(args: Vec<String>) -> Arc<dyn usecase::Usecase> {

fn usecases() -> HashMap<&'static str, Arc<dyn usecase::Usecase>> {
let usecases: Vec<Arc<dyn usecase::Usecase>> = vec![
Arc::new(fzf_make::FzfMake::new()),
Arc::new(fzf_make_main::FzfMake::new()),
Arc::new(help::Help::new()),
Arc::new(invalid_arg::InvalidArg::new()),
Arc::new(version::Version::new()),
Expand Down
1 change: 0 additions & 1 deletion src/main.rs
Original file line number Diff line number Diff line change
@@ -1,5 +1,4 @@
mod controller;
mod fuzzy_finder;
mod models;
mod usecase;

Expand Down
29 changes: 0 additions & 29 deletions src/usecase/fzf_make.rs

This file was deleted.

21 changes: 5 additions & 16 deletions src/fuzzy_finder.rs → src/usecase/fzf_make/fuzzy_finder.rs
Original file line number Diff line number Diff line change
@@ -1,10 +1,9 @@
use colored::*;
use skim::prelude::{Receiver, Skim, SkimItem, SkimItemReader, SkimOptions, SkimOptionsBuilder};
use std::{io::Cursor, process, sync::Arc};

use crate::models::makefile::Makefile;

pub fn run(makefile: Makefile) {
pub fn run(makefile: Makefile) -> String {
let preview_command = get_preview_command(makefile.to_include_files_string());
let options = get_skim_options(&preview_command);
let item = get_skim_item(makefile.to_targets_string());
Expand All @@ -18,20 +17,10 @@ pub fn run(makefile: Makefile) {
.map(|out| out.selected_items)
.unwrap_or_else(Vec::new);

for item in selected_items.iter() {
println!(
"{}",
("make ".to_string() + &item.output().to_string()).blue() // TODO: Make output color configurable via config file
);

process::Command::new("make")
.stdin(process::Stdio::inherit())
.arg(item.output().to_string())
.spawn()
.expect("Failed to execute process")
.wait()
.expect("Failed to execute process");
}
selected_items.first().unwrap().output().to_string()
} else {
println!("[ERR] {}", "Fail to get selected target.".to_string());
process::exit(1)
}
}

Expand Down
1 change: 1 addition & 0 deletions src/usecase/fzf_make/mod.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
pub(super) mod fuzzy_finder;
40 changes: 40 additions & 0 deletions src/usecase/fzf_make_main.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,40 @@
use crate::{
models::makefile::Makefile, usecase::fzf_make::fuzzy_finder, usecase::usecase::Usecase,
};
use colored::*;
use std::process;

pub struct FzfMake;

impl Usecase for FzfMake {
fn command_str(&self) -> Vec<&'static str> {
vec![]
}

fn run(&self) {
let makefile = match Makefile::create_makefile() {
Err(e) => {
println!("[ERR] {}", e.to_string());
process::exit(1)
}
Ok(f) => f,
};

let target = fuzzy_finder::run(makefile);

println!("{}", ("make ".to_string() + &target).blue()); // TODO: Make output color configurable via config file
process::Command::new("make")
.stdin(process::Stdio::inherit())
.arg(target)
.spawn()
.expect("Failed to execute process")
.wait()
.expect("Failed to execute process");
}
}

impl FzfMake {
pub fn new() -> Self {
Self {}
}
}
1 change: 1 addition & 0 deletions src/usecase/mod.rs
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
pub(super) mod fzf_make;
pub(super) mod fzf_make_main;
pub(super) mod help;
pub(super) mod invalid_arg;
pub(super) mod usecase;
Expand Down

0 comments on commit 35e06e8

Please sign in to comment.