Skip to content

Commit

Permalink
update reading enum properties
Browse files Browse the repository at this point in the history
  • Loading branch information
jhspetersson committed Sep 18, 2024
1 parent 08d26a1 commit 7ca7099
Show file tree
Hide file tree
Showing 4 changed files with 19 additions and 11 deletions.
4 changes: 2 additions & 2 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -286,8 +286,8 @@ Configure known task properties (you can add any other if you wish to any task):
You can also set up their own colors for specific values of the properties (assuming you've already added `priority` property):

git task config prop enum add priority HIGH Red
git task config prop enum get priority HIGH
git task config prop enum set priority HIGH Magenta
git task config prop enum get priority HIGH color
git task config prop enum set priority HIGH Magenta bold
git task config prop enum list priority
git task config prop enum del priority HIGH

Expand Down
8 changes: 5 additions & 3 deletions src/main.rs
Original file line number Diff line number Diff line change
Expand Up @@ -427,12 +427,14 @@ enum PropertiesEnumCommand {
/// property enum style (e.g., bold or underline)
enum_value_style: Option<String>,
},
/// Get a color of enum value
/// Get parameter of enum value
Get {
/// property name
name: String,
property: String,
/// property enum value
enum_value_name: String,
/// parameter (color or style)
parameter: String,
},
/// Set color for a property enum value
Set {
Expand Down Expand Up @@ -529,7 +531,7 @@ fn task_config_properties_enum(subcommand: PropertiesEnumCommand) -> bool {
match subcommand {
PropertiesEnumCommand::List { name } => task_config_properties_enum_list(name),
PropertiesEnumCommand::Add { name, enum_value_name, enum_value_color, enum_value_style } => task_config_properties_enum_add(name, enum_value_name, enum_value_color, enum_value_style),
PropertiesEnumCommand::Get { name, enum_value_name } => task_config_properties_enum_get(name, enum_value_name),
PropertiesEnumCommand::Get { property, enum_value_name, parameter } => task_config_properties_enum_get(property, enum_value_name, parameter),
PropertiesEnumCommand::Set { name, enum_value_name, enum_value_color, enum_value_style } => task_config_properties_enum_set(name, enum_value_name, enum_value_color, enum_value_style),
PropertiesEnumCommand::Delete { name, enum_value_name } => task_config_properties_enum_delete(name, enum_value_name),
}
Expand Down
4 changes: 2 additions & 2 deletions src/operations/config.rs
Original file line number Diff line number Diff line change
Expand Up @@ -312,9 +312,9 @@ pub(crate) fn task_config_properties_enum_add(name: String, enum_value_name: Str
}
}

pub(crate) fn task_config_properties_enum_get(name: String, enum_value_name: String) -> bool {
pub(crate) fn task_config_properties_enum_get(property: String, enum_value_name: String, parameter: String) -> bool {
let prop_manager = PropertyManager::new();
match prop_manager.get_enum_property(name, enum_value_name) {
match prop_manager.get_enum_parameter(property, enum_value_name, parameter) {
Ok(s) => success_message(s),
Err(e) => error_message(format!("ERROR: {e}"))
}
Expand Down
14 changes: 10 additions & 4 deletions src/property.rs
Original file line number Diff line number Diff line change
Expand Up @@ -275,16 +275,22 @@ impl PropertyManager {
}
}

pub fn get_enum_property(&self, name: String, enum_value_name: String) -> Result<String, String> {
let property = self.properties.iter().find(|saved_prop| saved_prop.name == name);
pub fn get_enum_parameter(&self, property: String, enum_value_name: String, parameter: String) -> Result<String, String> {
let property = self.properties.iter().find(|saved_prop| saved_prop.name == property);
match property {
Some(property) => {
match &property.enum_values {
Some(enum_values) => {
let enum_value = enum_values.iter().find(|saved_enum| saved_enum.name == enum_value_name);
match enum_value {
Some(enum_value) => Ok(enum_value.color.clone() + (if enum_value.get_style().is_some() { ", " } else { "" }) + enum_value.get_style().unwrap_or_else(|| "")),
None => Err("Property not found".to_string()),
Some(enum_value) => {
match parameter.as_str() {
"color" => Ok(enum_value.color.clone()),
"style" => Ok(enum_value.style.clone().unwrap_or_else(|| String::new())),
_ => Err("Unknown parameter, use `color` or `style`".to_string()),
}
},
None => Err("Property enum value not found".to_string()),
}
},
None => Err("Property has no enum values".to_string())
Expand Down

0 comments on commit 7ca7099

Please sign in to comment.