mirror of
https://github.com/natelandau/obsidian-metadata.git
synced 2025-11-14 07:53: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."
|
"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:
|
match meta_type:
|
||||||
case MetadataType.FRONTMATTER | MetadataType.INLINE:
|
case MetadataType.FRONTMATTER | MetadataType.INLINE:
|
||||||
key = self.questions.ask_new_key(question="Enter the key for the new metadata")
|
key = self.questions.ask_new_key(question="Enter the key for the new metadata")
|
||||||
|
|||||||
@@ -313,23 +313,6 @@ class Questions:
|
|||||||
qmark="INPUT |",
|
qmark="INPUT |",
|
||||||
).ask()
|
).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
|
def ask_confirm(self, question: str, default: bool = True) -> bool: # pragma: no cover
|
||||||
"""Ask the user to confirm an action.
|
"""Ask the user to confirm an action.
|
||||||
|
|
||||||
@@ -445,6 +428,27 @@ class Questions:
|
|||||||
question=question,
|
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
|
def ask_new_key(self, question: str = "New key name") -> str: # pragma: no cover
|
||||||
"""Ask the user for a new metadata key.
|
"""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],
|
side_effect=["add_metadata", KeyError],
|
||||||
)
|
)
|
||||||
mocker.patch(
|
mocker.patch(
|
||||||
"obsidian_metadata.models.application.Questions.ask_area",
|
"obsidian_metadata.models.application.Questions.ask_meta_type",
|
||||||
return_value=MetadataType.FRONTMATTER,
|
return_value=MetadataType.FRONTMATTER,
|
||||||
)
|
)
|
||||||
mocker.patch(
|
mocker.patch(
|
||||||
@@ -98,7 +98,7 @@ def test_add_metadata_inline(test_application, mocker, capsys) -> None:
|
|||||||
side_effect=["add_metadata", KeyError],
|
side_effect=["add_metadata", KeyError],
|
||||||
)
|
)
|
||||||
mocker.patch(
|
mocker.patch(
|
||||||
"obsidian_metadata.models.application.Questions.ask_area",
|
"obsidian_metadata.models.application.Questions.ask_meta_type",
|
||||||
return_value=MetadataType.INLINE,
|
return_value=MetadataType.INLINE,
|
||||||
)
|
)
|
||||||
mocker.patch(
|
mocker.patch(
|
||||||
@@ -130,7 +130,7 @@ def test_add_metadata_tag(test_application, mocker, capsys) -> None:
|
|||||||
side_effect=["add_metadata", KeyError],
|
side_effect=["add_metadata", KeyError],
|
||||||
)
|
)
|
||||||
mocker.patch(
|
mocker.patch(
|
||||||
"obsidian_metadata.models.application.Questions.ask_area",
|
"obsidian_metadata.models.application.Questions.ask_meta_type",
|
||||||
return_value=MetadataType.TAGS,
|
return_value=MetadataType.TAGS,
|
||||||
)
|
)
|
||||||
mocker.patch(
|
mocker.patch(
|
||||||
|
|||||||
Reference in New Issue
Block a user