cli: Refactor printing field
This commit is contained in:
parent
2f39fd4b1c
commit
5d0db0fafe
|
@ -1,16 +1,20 @@
|
|||
#include "./helper.h"
|
||||
|
||||
#include "../application/knownfieldmodel.h"
|
||||
|
||||
#include <tagparser/mediafileinfo.h>
|
||||
|
||||
#include <c++utilities/application/argumentparser.h>
|
||||
|
||||
#include <iostream>
|
||||
#include <cstring>
|
||||
|
||||
using namespace std;
|
||||
using namespace ApplicationUtilities;
|
||||
using namespace ConversionUtilities;
|
||||
using namespace ChronoUtilities;
|
||||
using namespace Media;
|
||||
using namespace Settings;
|
||||
|
||||
namespace Cli {
|
||||
|
||||
|
@ -127,6 +131,46 @@ void printProperty(const char *propName, ElementPosition elementPosition, const
|
|||
}
|
||||
}
|
||||
|
||||
void printFieldName(const char *fieldName, size_t fieldNameLen)
|
||||
{
|
||||
cout << ' ' << fieldName;
|
||||
// also write padding
|
||||
for(auto i = fieldNameLen; i < 18; ++i) {
|
||||
cout << ' ';
|
||||
}
|
||||
}
|
||||
|
||||
void printField(const FieldScope &scope, const Tag *tag, bool skipEmpty)
|
||||
{
|
||||
const auto &values = tag->values(scope.field);
|
||||
if(!skipEmpty || !values.empty()) {
|
||||
// write field name
|
||||
const char *fieldName = KnownFieldModel::fieldName(scope.field);
|
||||
const auto fieldNameLen = strlen(fieldName);
|
||||
|
||||
// write value
|
||||
if(values.empty()) {
|
||||
printFieldName(fieldName, fieldNameLen);
|
||||
cout << "none\n";
|
||||
} else {
|
||||
for(const auto &value : values) {
|
||||
printFieldName(fieldName, fieldNameLen);
|
||||
try {
|
||||
const auto textValue = value->toString(TagTextEncoding::Utf8);
|
||||
if(textValue.empty()) {
|
||||
cout << "can't display here (see --extract)";
|
||||
} else {
|
||||
cout << textValue;
|
||||
}
|
||||
} catch(const ConversionException &) {
|
||||
cout << "conversion error";
|
||||
}
|
||||
cout << '\n';
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
TagUsage parseUsageDenotation(const Argument &usageArg, TagUsage defaultUsage)
|
||||
{
|
||||
if(usageArg.isPresent()) {
|
||||
|
|
20
cli/helper.h
20
cli/helper.h
|
@ -49,6 +49,24 @@ inline TagType &operator|= (TagType &lhs, TagType rhs)
|
|||
return lhs = static_cast<TagType>(static_cast<unsigned int>(lhs) | static_cast<unsigned int>(rhs));
|
||||
}
|
||||
|
||||
struct FieldId
|
||||
{
|
||||
constexpr FieldId(KnownField field);
|
||||
constexpr FieldId(const char *field);
|
||||
KnownField genericField;
|
||||
const char *nativeField;
|
||||
};
|
||||
|
||||
constexpr FieldId::FieldId(KnownField field) :
|
||||
genericField(field),
|
||||
nativeField(nullptr)
|
||||
{}
|
||||
|
||||
constexpr FieldId::FieldId(const char *field) :
|
||||
genericField(KnownField::Invalid),
|
||||
nativeField(field)
|
||||
{}
|
||||
|
||||
struct FieldScope
|
||||
{
|
||||
FieldScope(KnownField field = KnownField::Invalid, TagType tagType = TagType::Unspecified, TagTarget tagTarget = TagTarget());
|
||||
|
@ -192,6 +210,8 @@ inline void printProperty(const char *propName, const intType value, const char
|
|||
}
|
||||
}
|
||||
|
||||
void printField(const FieldScope &scope, const Tag *tag, bool skipEmpty);
|
||||
|
||||
TagUsage parseUsageDenotation(const ApplicationUtilities::Argument &usageArg, TagUsage defaultUsage);
|
||||
TagTextEncoding parseEncodingDenotation(const ApplicationUtilities::Argument &encodingArg, TagTextEncoding defaultEncoding);
|
||||
ElementPosition parsePositionDenotation(const ApplicationUtilities::Argument &posArg, const ApplicationUtilities::Argument &valueArg, ElementPosition defaultPos);
|
||||
|
|
|
@ -272,71 +272,13 @@ void displayTagInfo(const Argument &fieldsArg, const Argument &filesArg, const A
|
|||
// iterate through fields specified by the user
|
||||
if(fields.empty()) {
|
||||
for(auto field = firstKnownField; field != KnownField::Invalid; field = nextKnownField(field)) {
|
||||
const auto &values = tag->values(field);
|
||||
if(!values.empty()) {
|
||||
const char *fieldName = KnownFieldModel::fieldName(field);
|
||||
const auto fieldNameLen = strlen(fieldName);
|
||||
for(const auto &value : values) {
|
||||
// write field name
|
||||
cout << ' ' << fieldName;
|
||||
// write padding
|
||||
for(auto i = fieldNameLen; i < 18; ++i) {
|
||||
cout << ' ';
|
||||
}
|
||||
// write value
|
||||
try {
|
||||
const auto textValue = value->toString(TagTextEncoding::Utf8);
|
||||
if(textValue.empty()) {
|
||||
cout << "can't display here (see --extract)";
|
||||
} else {
|
||||
cout << textValue;
|
||||
}
|
||||
} catch(const ConversionException &) {
|
||||
cout << "conversion error";
|
||||
}
|
||||
cout << endl;
|
||||
}
|
||||
}
|
||||
printField(FieldScope(field), tag, true);
|
||||
}
|
||||
} else {
|
||||
for(const auto &fieldDenotation : fields) {
|
||||
const FieldScope &denotedScope = fieldDenotation.first;
|
||||
if(denotedScope.tagType == TagType::Unspecified || (denotedScope.tagType | tagType) != TagType::Unspecified) {
|
||||
const auto &values = tag->values(denotedScope.field);
|
||||
const char *fieldName = KnownFieldModel::fieldName(denotedScope.field);
|
||||
const auto fieldNameLen = strlen(fieldName);
|
||||
if(values.empty()) {
|
||||
// write field name
|
||||
const char *fieldName = KnownFieldModel::fieldName(denotedScope.field);
|
||||
cout << ' ' << fieldName;
|
||||
// write padding
|
||||
for(auto i = fieldNameLen; i < 18; ++i) {
|
||||
cout << ' ';
|
||||
}
|
||||
cout << "none";
|
||||
} else {
|
||||
for(const auto &value : values) {
|
||||
// write field name
|
||||
const char *fieldName = KnownFieldModel::fieldName(denotedScope.field);
|
||||
cout << ' ' << fieldName;
|
||||
// write padding
|
||||
for(auto i = fieldNameLen; i < 18; ++i) {
|
||||
cout << ' ';
|
||||
}
|
||||
// write value
|
||||
try {
|
||||
const auto textValue = value->toString(TagTextEncoding::Utf8);
|
||||
if(textValue.empty()) {
|
||||
cout << "can't display here (see --extract)";
|
||||
} else {
|
||||
cout << textValue;
|
||||
}
|
||||
} catch(const ConversionException &) {
|
||||
cout << "conversion error";
|
||||
}
|
||||
cout << endl;
|
||||
}
|
||||
}
|
||||
printField(denotedScope, tag, false);
|
||||
}
|
||||
}
|
||||
}
|
||||
|
|
Loading…
Reference in New Issue