Skip to content

Commit

Permalink
refactored projects to dirs command
Browse files Browse the repository at this point in the history
  • Loading branch information
KCaverly committed Jan 5, 2023
1 parent f6c6319 commit 5c5e055
Show file tree
Hide file tree
Showing 3 changed files with 18 additions and 19 deletions.
8 changes: 4 additions & 4 deletions src/config.rs
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@ use toml;

#[derive(Debug, Deserialize)]
pub struct Config {
pub projects: HashMap<String, Project>,
pub directories: HashMap<String, ProjectDirectory>,
pub dotfiles: Dotfiles,
pub ssh: HashMap<String, SSH>,
}
Expand Down Expand Up @@ -35,7 +35,7 @@ impl Config {
}

#[derive(Debug, Deserialize)]
pub struct Project {
pub struct ProjectDirectory {
pub session_name: String,
pub directory: String,
pub max_depth: u8,
Expand Down Expand Up @@ -70,8 +70,8 @@ fn get_peaches_path() -> String {

pub fn generate_config() {
const DEFAULT_CONFIG: &str = r#"
[projects]
[projects.default]
[directories]
[directories.default]
session_name = "default"
directory = "/"
min_depth = 1
Expand Down
18 changes: 9 additions & 9 deletions src/projects.rs → src/dirs.rs
Original file line number Diff line number Diff line change
Expand Up @@ -2,14 +2,14 @@ use std::{collections::HashMap, io};
use walkdir::{DirEntry, WalkDir};

use crate::{
config::{Config, Project},
config::{Config, ProjectDirectory},
fuzzy_finder,
tmux::TMUX,
};

pub struct ProjectsCommand {}
pub struct DirsCommand {}

impl ProjectsCommand {
impl DirsCommand {
fn get_folders(path: &str, min_depth: u8, max_depth: u8) -> Result<Vec<DirEntry>, io::Error> {
Ok(WalkDir::new(path)
.min_depth(min_depth.into())
Expand All @@ -21,10 +21,10 @@ impl ProjectsCommand {
.collect::<Vec<DirEntry>>())
}

fn get_options(projects: &HashMap<String, Project>) -> Vec<String> {
fn get_options(directories: &HashMap<String, ProjectDirectory>) -> Vec<String> {
let mut file_list: Vec<String> = Vec::new();

for (_key, value) in &*projects {
for (_key, value) in &*directories {
let dir_files: Vec<String> =
Self::get_folders(&value.directory, value.min_depth, value.max_depth)
.unwrap()
Expand All @@ -51,8 +51,8 @@ impl ProjectsCommand {
return file_list;
}

fn get_project_details<'a>(cfg: &'a Config, selected: &'a str) -> &'a Project {
for (_key, value) in &cfg.projects {
fn get_directory_details<'a>(cfg: &'a Config, selected: &'a str) -> &'a ProjectDirectory {
for (_key, value) in &cfg.directories {
if selected.contains(&value.directory) {
return value;
}
Expand All @@ -61,7 +61,7 @@ impl ProjectsCommand {
}

fn post_search_command(cfg: &Config, selected: &str) {
let details = Self::get_project_details(cfg, selected);
let details = Self::get_directory_details(cfg, selected);
let name = selected.split("/").last().unwrap();

TMUX::create_window(&details.session_name, name);
Expand All @@ -74,7 +74,7 @@ impl ProjectsCommand {
}

pub fn run(cfg: &Config) {
let options = Self::get_options(&cfg.projects);
let options = Self::get_options(&cfg.directories);
let selected = fuzzy_finder::search_options(options);
Self::post_search_command(&cfg, &selected);
}
Expand Down
11 changes: 5 additions & 6 deletions src/main.rs
Original file line number Diff line number Diff line change
@@ -1,14 +1,14 @@
mod config;
mod dirs;
mod docker;
mod dotfiles;
mod fuzzy_finder;
mod projects;
mod ssh;
mod tmux;

use clap::{Parser, Subcommand};
use docker::DockerCommand;
use projects::ProjectsCommand;
use dirs::DirsCommand;
use ssh::SSHCommand;
use std::process::{Command, Stdio};
use std::str;
Expand All @@ -23,7 +23,7 @@ struct Value {
#[derive(Subcommand)]
enum Commands {
/// Run projects fuzzy finder
Projects {},
Dirs {},
/// Run ssh server launcher
SSH {},
/// Upgrade peaches to latest version available on github
Expand Down Expand Up @@ -80,10 +80,9 @@ fn main() {
let value = Value::parse();

match &value.commands {
Commands::Projects {} => {
Commands::Dirs {} => {
let cfg: config::Config = config::load_config();
ProjectsCommand::run(&cfg);
// run_projects(&cfg);
DirsCommand::run(&cfg);
}

Commands::SSH {} => {
Expand Down

0 comments on commit 5c5e055

Please sign in to comment.