-
Notifications
You must be signed in to change notification settings - Fork 1
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Refactor the table and add a ls cmd for projects
- Loading branch information
Showing
13 changed files
with
346 additions
and
57 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,20 @@ | ||
use clap::{Parser, Subcommand}; | ||
|
||
#[derive(Parser, Debug)] | ||
pub struct ProjectCli { | ||
#[command(subcommand)] | ||
pub action: ProjectCommands, | ||
} | ||
|
||
#[derive(Subcommand, Debug)] | ||
pub enum ProjectCommands { | ||
#[command(alias = "ls")] | ||
List(ProjectListArgs), | ||
} | ||
|
||
#[derive(Debug, Parser)] | ||
pub struct ProjectListArgs { | ||
/// Show minimal output for scripts | ||
#[arg(short, long, default_value_t = false)] | ||
pub minimal: bool, | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,3 +1,4 @@ | ||
pub mod directory; | ||
pub mod init; | ||
pub mod project; | ||
pub mod template; |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,22 @@ | ||
use crate::{ | ||
cli::project::{ProjectCli, ProjectCommands, ProjectListArgs}, | ||
projects::parse_project_config, | ||
widgets::{heading::Heading, table::Table}, | ||
}; | ||
|
||
pub fn project_handler(args: ProjectCli) { | ||
match args.action { | ||
ProjectCommands::List(args) => list_handler(args), | ||
} | ||
} | ||
|
||
fn list_handler(args: ProjectListArgs) { | ||
for proj in parse_project_config() { | ||
if args.minimal { | ||
println!("{}", proj.name); | ||
} else { | ||
println!("{}", Heading(proj.name)); | ||
println!("{}", Table::from(proj.setup)); | ||
} | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,152 @@ | ||
use crate::{ | ||
exit, | ||
helpers::{get_config_dir, Exit}, | ||
templates::{parse_template_config, Window}, | ||
widgets::table::Table, | ||
}; | ||
use serde::Deserialize; | ||
use std::fs; | ||
|
||
#[derive(Debug, PartialEq, Eq)] | ||
pub struct Project { | ||
pub name: String, | ||
pub setup: ProjectSetup, | ||
} | ||
|
||
#[derive(Debug, Deserialize, PartialEq, Eq)] | ||
#[serde(untagged)] | ||
pub enum ProjectSetup { | ||
Template(String), | ||
Windows(Vec<Window>), | ||
} | ||
|
||
impl From<ProjectSetup> for Table<String, String> { | ||
fn from(value: ProjectSetup) -> Self { | ||
let (template_name, windows) = match value { | ||
ProjectSetup::Template(template_name) => { | ||
let all_templates = parse_template_config(); | ||
let template = all_templates | ||
.into_iter() | ||
.find(|t| t.name == template_name) | ||
.unwrap_or_else(|| exit!(1, "Template {} could not be found", template_name)); | ||
|
||
(Some(template_name), template.windows) | ||
} | ||
ProjectSetup::Windows(windows) => (None, windows), | ||
}; | ||
|
||
let mut rows = Self::new(vec![( | ||
"Template".to_string(), | ||
template_name.unwrap_or("None".to_string()), | ||
)]) | ||
.rows; | ||
let windows = Self::from_iter(windows).rows; | ||
rows.extend(windows); | ||
|
||
Self::new(rows) | ||
} | ||
} | ||
|
||
impl<'de> Deserialize<'de> for Project { | ||
fn deserialize<D>(deserializer: D) -> Result<Self, D::Error> | ||
where | ||
D: serde::Deserializer<'de>, | ||
{ | ||
#[derive(Deserialize)] | ||
struct RawProject { | ||
name: String, | ||
template: Option<String>, | ||
windows: Option<Vec<Window>>, | ||
} | ||
|
||
let raw = RawProject::deserialize(deserializer)?; | ||
|
||
let setup = if let Some(template) = raw.template { | ||
ProjectSetup::Template(template) | ||
} else if let Some(windows) = raw.windows { | ||
ProjectSetup::Windows(windows) | ||
} else { | ||
return Err(serde::de::Error::custom( | ||
"Expected either template or windows", | ||
)); | ||
}; | ||
|
||
Ok(Project { | ||
name: raw.name, | ||
setup, | ||
}) | ||
} | ||
} | ||
|
||
pub fn parse_project_config() -> Vec<Project> { | ||
let projects_content = | ||
fs::read_dir(get_config_dir().join("projects/")).exit(1, "Can't read template config"); | ||
|
||
let projects_raw: Vec<_> = projects_content | ||
.filter_map(|x| x.ok()) | ||
.filter(|x| x.path().is_file()) | ||
.filter_map(|x| fs::read_to_string(x.path()).ok()) | ||
.collect(); | ||
|
||
projects_raw | ||
.iter() | ||
.filter_map(|x| serde_yaml::from_str::<Project>(x).ok()) | ||
.collect() | ||
} | ||
|
||
#[cfg(test)] | ||
mod tests { | ||
use super::*; | ||
|
||
#[test] | ||
fn test_parser() { | ||
let project = serde_yaml::from_str::<Project>( | ||
"name: OsmApp | ||
root_dir: ~/GitHub/osmapp/ | ||
windows: | ||
- name: Neovim | ||
panes: | ||
- nvim | ||
- name: Server | ||
panes: | ||
- yarn run dev", | ||
) | ||
.unwrap(); | ||
|
||
assert_eq!( | ||
project, | ||
Project { | ||
name: "OsmApp".to_string(), | ||
setup: ProjectSetup::Windows(vec![ | ||
Window { | ||
name: Some(" Neovim".to_string()), | ||
panes: vec!["nvim".to_string()], | ||
layout: None, | ||
}, | ||
Window { | ||
name: Some("Server".to_string()), | ||
panes: vec!["yarn run dev".to_string()], | ||
layout: None, | ||
} | ||
]) | ||
} | ||
); | ||
|
||
let project = serde_yaml::from_str::<Project>( | ||
"name: Dlool | ||
root_dir: ~/SoftwareDevelopment/web/Dlool/dlool_frontend_v2/ | ||
template: Svelte", | ||
) | ||
.unwrap(); | ||
|
||
assert_eq!( | ||
project, | ||
Project { | ||
name: "Dlool".to_string(), | ||
setup: ProjectSetup::Template("Svelte".to_string()) | ||
} | ||
); | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.