Skip to content

Commit

Permalink
refactor: Change fuzzy_finder::run to return String
Browse files Browse the repository at this point in the history
  • Loading branch information
kyu08 committed Oct 9, 2023
1 parent 1a924d6 commit d061e99
Show file tree
Hide file tree
Showing 10 changed files with 67 additions and 67 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;
39 changes: 39 additions & 0 deletions src/usecase/fzf_make_main.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,39 @@
use crate::models::makefile::Makefile;
use crate::usecase::{fzf_make::fuzzy_finder, usecase::Usecase};
use colored::*;
use std::process;

pub struct FzfMake;

impl FzfMake {
pub fn new() -> Self {
Self {}
}
}

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");
}
}
12 changes: 6 additions & 6 deletions src/usecase/help.rs
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,12 @@ use crate::usecase::usecase::Usecase;

pub struct Help;

impl Help {
pub fn new() -> Self {
Self {}
}
}

impl Usecase for Help {
fn command_str(&self) -> Vec<&'static str> {
vec!["--help", "-h", "help"]
Expand All @@ -12,12 +18,6 @@ impl Usecase for Help {
}
}

impl Help {
pub fn new() -> Self {
Self {}
}
}

// TODO: Make each command have the following information as a struct, and just display it here.
// Define the vector of usecases in only one place and refer to it.
pub fn get_help() -> String {
Expand Down
12 changes: 6 additions & 6 deletions src/usecase/invalid_arg.rs
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,12 @@ use crate::usecase::usecase::Usecase;

pub struct InvalidArg;

impl InvalidArg {
pub fn new() -> Self {
Self {}
}
}

impl Usecase for InvalidArg {
fn command_str(&self) -> Vec<&'static str> {
vec![]
Expand All @@ -14,12 +20,6 @@ impl Usecase for InvalidArg {
}
}

impl InvalidArg {
pub fn new() -> Self {
Self {}
}
}

fn get_message() -> String {
"Invalid argment.".to_string()
}
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
12 changes: 6 additions & 6 deletions src/usecase/version.rs
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,12 @@ use std::env;

pub struct Version;

impl Version {
pub fn new() -> Self {
Self {}
}
}

impl Usecase for Version {
fn command_str(&self) -> Vec<&'static str> {
vec!["--version", "-v", "version"]
Expand All @@ -13,12 +19,6 @@ impl Usecase for Version {
}
}

impl Version {
pub fn new() -> Self {
Self {}
}
}

fn get_version() -> String {
env!("CARGO_PKG_VERSION").to_string()
}

0 comments on commit d061e99

Please sign in to comment.