Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

refactor: Return String from fuzzy_finder::run() #96

Merged
merged 1 commit into from
Oct 9, 2023
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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()
}