From 410b54f2df4a338574aa4b36e93b8b1d8178f841 Mon Sep 17 00:00:00 2001 From: Collins Muriuki Date: Wed, 20 Nov 2024 17:52:40 +0300 Subject: [PATCH 1/5] Add go back functionality for hc scaffold entry-type --- src/scaffold/entry_type/fields.rs | 99 +++++++++++++++++++++++++++---- 1 file changed, 86 insertions(+), 13 deletions(-) diff --git a/src/scaffold/entry_type/fields.rs b/src/scaffold/entry_type/fields.rs index f3d3534b..0ba1aaa7 100644 --- a/src/scaffold/entry_type/fields.rs +++ b/src/scaffold/entry_type/fields.rs @@ -34,6 +34,7 @@ pub fn choose_fields( zome_file_tree, field_types_templates, no_ui, + None, )?; println!(); @@ -45,25 +46,95 @@ pub fn choose_fields( } println!( - "Chosen fields:\n {}", + "Current fields:\n{}", fields .iter() - .map(|f| format!("{}: {}", f.field_name.clone(), f.field_type)) + .map(|f| format!(" {}: {}\n", f.field_name.clone(), f.field_type)) .collect::>() - .join(", ") + .join("") .italic() ); let selection = Select::with_theme(&ColorfulTheme::default()) .with_prompt( - "Do you want to proceed with the current entry type or restart from the beginning?", + "Do you want to proceed with the current entry type, modify or start again from the beginning?", ) .item("Confirm") + .item("Modify") .item("Restart") .default(0) .interact()?; if selection == 1 { + loop { + let action = Select::with_theme(&ColorfulTheme::default()) + .with_prompt("What would you like to do?") + .items(&["Modify Field", "Add Field", "Remove Field", "Done"]) + .interact()?; + + match action { + 0 => { + // Modify field + let field_to_modify = Select::with_theme(&ColorfulTheme::default()) + .with_prompt("Select field to modify") + .items( + &fields + .iter() + .map(|f| format!("{}: {}", f.field_name, f.field_type).italic()) + .collect::>(), + ) + .interact()?; + + let new_field = choose_field( + entry_type_name, + zome_file_tree, + field_types_templates, + no_ui, + Some(&fields[field_to_modify].field_name), + )?; + fields[field_to_modify] = new_field; + } + 1 => { + // Add field + let new_field = choose_field( + entry_type_name, + zome_file_tree, + field_types_templates, + no_ui, + None, + )?; + fields.push(new_field); + } + 2 => { + // Remove field + if !fields.is_empty() { + let field_to_remove = Select::with_theme(&ColorfulTheme::default()) + .with_prompt("Select field to remove") + .items( + &fields + .iter() + .map(|f| format!("{}: {}", f.field_name, f.field_type).italic()) + .collect::>(), + ) + .interact()?; + fields.remove(field_to_remove); + } + } + 3 => break, // Done + _ => unreachable!(), + } + + println!( + "\nCurrent fields:\n{}", + fields + .iter() + .map(|f| format!(" {}: {}\n", f.field_name, f.field_type)) + .collect::>() + .join("") + .italic() + ); + } + } else if selection == 2 { return choose_fields( entry_type_name, zome_file_tree, @@ -80,6 +151,7 @@ fn choose_field( zome_file_tree: &ZomeFileTree, field_types_templates: &FileTree, no_ui: bool, + initial_field_name: Option<&str>, ) -> ScaffoldResult { let field_types = FieldType::list(); let field_type_names: Vec = field_types @@ -88,15 +160,16 @@ fn choose_field( .map(|s| s.to_string()) .collect(); - let field_name = input_with_custom_validation("Field name (snake_case):", None, |input| { - if let Err(e) = check_case(&input, "field_name", Case::Snake) { - return Err(e.to_string()); - } - if let Err(e) = check_for_reserved_keywords(&input) { - return Err(e.to_string()); - } - Ok(()) - })?; + let field_name = + input_with_custom_validation("Field name (snake_case):", initial_field_name, |input| { + if let Err(e) = check_case(&input, "field_name", Case::Snake) { + return Err(e.to_string()); + } + if let Err(e) = check_for_reserved_keywords(&input) { + return Err(e.to_string()); + } + Ok(()) + })?; let selection = Select::with_theme(&ColorfulTheme::default()) .with_prompt("Choose field type:") From 212261a72100f22d63949fcc14fd65dd778c0edc Mon Sep 17 00:00:00 2001 From: Collins Muriuki Date: Fri, 22 Nov 2024 14:16:05 +0300 Subject: [PATCH 2/5] address PR comments --- src/scaffold/entry_type/fields.rs | 26 +++++++++++++++----------- 1 file changed, 15 insertions(+), 11 deletions(-) diff --git a/src/scaffold/entry_type/fields.rs b/src/scaffold/entry_type/fields.rs index 0ba1aaa7..ea13080d 100644 --- a/src/scaffold/entry_type/fields.rs +++ b/src/scaffold/entry_type/fields.rs @@ -49,9 +49,9 @@ pub fn choose_fields( "Current fields:\n{}", fields .iter() - .map(|f| format!(" {}: {}\n", f.field_name.clone(), f.field_type)) + .map(|f| format!(" {}: {}", f.field_name.clone(), f.field_type)) .collect::>() - .join("") + .join("\n") .italic() ); @@ -118,21 +118,25 @@ pub fn choose_fields( ) .interact()?; fields.remove(field_to_remove); + } else { + println!("{}", "All fields have been removed".yellow()) } } 3 => break, // Done _ => unreachable!(), } - println!( - "\nCurrent fields:\n{}", - fields - .iter() - .map(|f| format!(" {}: {}\n", f.field_name, f.field_type)) - .collect::>() - .join("") - .italic() - ); + if !fields.is_empty() { + println!( + "\nCurrent fields:\n{}", + fields + .iter() + .map(|f| format!(" {}: {}", f.field_name, f.field_type)) + .collect::>() + .join("\n") + .italic() + ); + } } } else if selection == 2 { return choose_fields( From ee5c2884ce6259a5aa7f3b28c8bd715cb9dfa4dc Mon Sep 17 00:00:00 2001 From: Collins Muriuki Date: Fri, 22 Nov 2024 14:26:02 +0300 Subject: [PATCH 3/5] Ensure the fields vec is not empty on change --- src/scaffold/entry_type/fields.rs | 42 +++++++++++++++++-------------- 1 file changed, 23 insertions(+), 19 deletions(-) diff --git a/src/scaffold/entry_type/fields.rs b/src/scaffold/entry_type/fields.rs index ea13080d..4f6aa770 100644 --- a/src/scaffold/entry_type/fields.rs +++ b/src/scaffold/entry_type/fields.rs @@ -69,30 +69,34 @@ pub fn choose_fields( loop { let action = Select::with_theme(&ColorfulTheme::default()) .with_prompt("What would you like to do?") - .items(&["Modify Field", "Add Field", "Remove Field", "Done"]) + .items(&["Change Field", "Add Field", "Remove Field", "Done"]) .interact()?; match action { 0 => { - // Modify field - let field_to_modify = Select::with_theme(&ColorfulTheme::default()) - .with_prompt("Select field to modify") - .items( - &fields - .iter() - .map(|f| format!("{}: {}", f.field_name, f.field_type).italic()) - .collect::>(), - ) - .interact()?; + // Change field + if !fields.is_empty() { + let field_to_change = Select::with_theme(&ColorfulTheme::default()) + .with_prompt("Select field to change") + .items( + &fields + .iter() + .map(|f| format!("{}: {}", f.field_name, f.field_type).italic()) + .collect::>(), + ) + .interact()?; - let new_field = choose_field( - entry_type_name, - zome_file_tree, - field_types_templates, - no_ui, - Some(&fields[field_to_modify].field_name), - )?; - fields[field_to_modify] = new_field; + let new_field = choose_field( + entry_type_name, + zome_file_tree, + field_types_templates, + no_ui, + Some(&fields[field_to_change].field_name), + )?; + fields[field_to_change] = new_field; + } else { + println!("{}", "No fields left to change".yellow()) + } } 1 => { // Add field From d5824f041ca976ea7a15eb3b7b4e7286a0ec0bb9 Mon Sep 17 00:00:00 2001 From: Collins Muriuki Date: Fri, 22 Nov 2024 14:31:22 +0300 Subject: [PATCH 4/5] Improve text spacing --- src/scaffold/entry_type/fields.rs | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/src/scaffold/entry_type/fields.rs b/src/scaffold/entry_type/fields.rs index 4f6aa770..3475e508 100644 --- a/src/scaffold/entry_type/fields.rs +++ b/src/scaffold/entry_type/fields.rs @@ -46,7 +46,7 @@ pub fn choose_fields( } println!( - "Current fields:\n{}", + "Current fields:\n{}\n", fields .iter() .map(|f| format!(" {}: {}", f.field_name.clone(), f.field_type)) @@ -132,7 +132,7 @@ pub fn choose_fields( if !fields.is_empty() { println!( - "\nCurrent fields:\n{}", + "\nCurrent fields:\n{}\n", fields .iter() .map(|f| format!(" {}: {}", f.field_name, f.field_type)) From 9479a2635924f7262e2342b9149ac9d800340c17 Mon Sep 17 00:00:00 2001 From: Collins Muriuki Date: Fri, 22 Nov 2024 15:39:01 +0300 Subject: [PATCH 5/5] Simplify prompt --- src/scaffold/entry_type/fields.rs | 8 ++------ 1 file changed, 2 insertions(+), 6 deletions(-) diff --git a/src/scaffold/entry_type/fields.rs b/src/scaffold/entry_type/fields.rs index 3475e508..3993665a 100644 --- a/src/scaffold/entry_type/fields.rs +++ b/src/scaffold/entry_type/fields.rs @@ -56,12 +56,8 @@ pub fn choose_fields( ); let selection = Select::with_theme(&ColorfulTheme::default()) - .with_prompt( - "Do you want to proceed with the current entry type, modify or start again from the beginning?", - ) - .item("Confirm") - .item("Modify") - .item("Restart") + .with_prompt("Do you want to proceed with the current entry type?") + .items(&["Confirm", "Modify", "Restart"]) .default(0) .interact()?;