mirror of
https://github.com/natelandau/obsidian-metadata.git
synced 2025-11-08 05:03:47 -05:00
fix: only ask for valid metadata types when adding new metadata
This commit is contained in:
@@ -84,7 +84,7 @@ class Application:
|
||||
"Add new metadata to your vault. Currently only supports adding to the frontmatter of a note."
|
||||
)
|
||||
|
||||
meta_type = self.questions.ask_area()
|
||||
meta_type = self.questions.ask_meta_type()
|
||||
match meta_type:
|
||||
case MetadataType.FRONTMATTER | MetadataType.INLINE:
|
||||
key = self.questions.ask_new_key(question="Enter the key for the new metadata")
|
||||
|
||||
@@ -313,23 +313,6 @@ class Questions:
|
||||
qmark="INPUT |",
|
||||
).ask()
|
||||
|
||||
def ask_area(self) -> MetadataType | str: # pragma: no cover
|
||||
"""Ask the user for the metadata area to work on.
|
||||
|
||||
Returns:
|
||||
MetadataType: The metadata area to work on.
|
||||
"""
|
||||
choices = []
|
||||
for metadata_type in MetadataType:
|
||||
choices.append({"name": metadata_type.value, "value": metadata_type})
|
||||
|
||||
choices.append(questionary.Separator()) # type: ignore [arg-type]
|
||||
choices.append({"name": "Cancel", "value": "cancel"})
|
||||
return self.ask_selection(
|
||||
choices=choices,
|
||||
question="Select the type of metadata",
|
||||
)
|
||||
|
||||
def ask_confirm(self, question: str, default: bool = True) -> bool: # pragma: no cover
|
||||
"""Ask the user to confirm an action.
|
||||
|
||||
@@ -445,6 +428,27 @@ class Questions:
|
||||
question=question,
|
||||
)
|
||||
|
||||
def ask_meta_type(self) -> MetadataType | str: # pragma: no cover
|
||||
"""Ask the user for the type of metadata to work on.
|
||||
|
||||
Returns:
|
||||
MetadataType: The metadata type
|
||||
"""
|
||||
choices = []
|
||||
for meta_type in MetadataType:
|
||||
match meta_type:
|
||||
case MetadataType.ALL | MetadataType.META | MetadataType.KEYS:
|
||||
continue
|
||||
case _:
|
||||
choices.append({"name": meta_type.value, "value": meta_type})
|
||||
|
||||
choices.append(questionary.Separator()) # type: ignore [arg-type]
|
||||
choices.append({"name": "Cancel", "value": "cancel"})
|
||||
return self.ask_selection(
|
||||
choices=choices,
|
||||
question="Select the type of metadata",
|
||||
)
|
||||
|
||||
def ask_new_key(self, question: str = "New key name") -> str: # pragma: no cover
|
||||
"""Ask the user for a new metadata key.
|
||||
|
||||
|
||||
@@ -66,7 +66,7 @@ def test_add_metadata_frontmatter(test_application, mocker, capsys) -> None:
|
||||
side_effect=["add_metadata", KeyError],
|
||||
)
|
||||
mocker.patch(
|
||||
"obsidian_metadata.models.application.Questions.ask_area",
|
||||
"obsidian_metadata.models.application.Questions.ask_meta_type",
|
||||
return_value=MetadataType.FRONTMATTER,
|
||||
)
|
||||
mocker.patch(
|
||||
@@ -98,7 +98,7 @@ def test_add_metadata_inline(test_application, mocker, capsys) -> None:
|
||||
side_effect=["add_metadata", KeyError],
|
||||
)
|
||||
mocker.patch(
|
||||
"obsidian_metadata.models.application.Questions.ask_area",
|
||||
"obsidian_metadata.models.application.Questions.ask_meta_type",
|
||||
return_value=MetadataType.INLINE,
|
||||
)
|
||||
mocker.patch(
|
||||
@@ -130,7 +130,7 @@ def test_add_metadata_tag(test_application, mocker, capsys) -> None:
|
||||
side_effect=["add_metadata", KeyError],
|
||||
)
|
||||
mocker.patch(
|
||||
"obsidian_metadata.models.application.Questions.ask_area",
|
||||
"obsidian_metadata.models.application.Questions.ask_meta_type",
|
||||
return_value=MetadataType.TAGS,
|
||||
)
|
||||
mocker.patch(
|
||||
|
||||
Reference in New Issue
Block a user