Skip to content

Commit

Permalink
Add new 'case' filename option for lists (fix #2655)
Browse files Browse the repository at this point in the history
  • Loading branch information
Bionus committed May 31, 2022
1 parent 102e796 commit 141aa4e
Show file tree
Hide file tree
Showing 4 changed files with 69 additions and 4 deletions.
22 changes: 22 additions & 0 deletions docs/_docs/filename.md
Original file line number Diff line number Diff line change
Expand Up @@ -224,6 +224,28 @@ artist1 tag1 character1 tag2
artist1 character1 tag1 tag2
```

### Case (enum)
Change the casing (capitalization) of words in that list.

Possible options:
* `lower`: some_tag
* `upper_first`: Some_tag
* `upper`: Some_Tag
* `caps`: SOME_TAG

Example:
```
%character%
test_tag1 test_tag2
%character:case=upper_first%
Test_tag1 Test_tag2
%character:case=upper%
Test_Tag1 Test_Tag2
```


## Tag lists
### Include namespace (boolean)
If enabled, the namespace of the tags will be included in the result. Better used with the `usafe` option to keep the `:`.
Expand Down
18 changes: 18 additions & 0 deletions src/lib/src/filename/filename-execution-visitor.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -21,6 +21,7 @@
#include "loader/token.h"
#include "logger.h"
#include "models/image.h"
#include "tags/tag-name-format.h"


FilenameExecutionVisitor::FilenameExecutionVisitor(const QMap<QString, Token> &tokens, QSettings *settings)
Expand Down Expand Up @@ -278,6 +279,23 @@ QString FilenameExecutionVisitor::variableToString(const QString &name, QStringL
std::sort(val.begin(), val.end());
}

if (options.contains("case")) {
static const QMap<QString, TagNameFormat::CaseFormat> caseAssoc
{
{ "lower", TagNameFormat::Lower },
{ "upper_first", TagNameFormat::UpperFirst },
{ "upper", TagNameFormat::Upper },
{ "caps", TagNameFormat::Caps },
};
const QString tagCase = options.value("case");
if (caseAssoc.contains(tagCase)) {
TagNameFormat tagFormat(caseAssoc.value(tagCase), "_");
for (QString &t : val) {
t = tagFormat.formatted(t.split('_'));
}
}
}

// Clean each value separately
if (!name.startsWith("source")) {
for (QString &t : val) {
Expand Down
24 changes: 20 additions & 4 deletions src/lib/src/tags/tag-name-format.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -25,13 +25,29 @@ QString TagNameFormat::wordSeparator() const

QString TagNameFormat::formatted(const QStringList &words) const
{
QStringList res;
res.reserve(words.count());
QString ret;

// Early return for empty inputs
if (words.isEmpty()) {
return ret;
}

// Allocate the proper size for the return string
int totalLength = m_wordSeparator.length() * (words.length() - 1);
for (const QString &word : words) {
totalLength += word.length();
}
ret.reserve(totalLength);

// Format every word
for (int i = 0; i < words.length(); ++i) {
res.append(formatted(words[i], i));
if (i > 0) {
ret.append(m_wordSeparator);
}
ret.append(formatted(words[i], i));
}

return res.join(m_wordSeparator);
return ret;
}

QString TagNameFormat::formatted(const QString &word, int index) const
Expand Down
9 changes: 9 additions & 0 deletions src/tests/src/models/filename-test.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -496,6 +496,15 @@ TEST_CASE("Filename")
{
assertPath(profile, img, "%general:separator=^,%", "tag1,tag2,tag3,test_tag1,test_tag2,test_tag3");
}
SECTION("PathOptionCase")
{
assertPath(profile, img, "%general:case,separator=+%", "tag1+tag2+tag3+test_tag1+test_tag2+test_tag3");
assertPath(profile, img, "%general:case=invalid,separator=+%", "tag1+tag2+tag3+test_tag1+test_tag2+test_tag3");
assertPath(profile, img, "%general:case=lower,separator=+%", "tag1+tag2+tag3+test_tag1+test_tag2+test_tag3");
assertPath(profile, img, "%general:case=upper_first,separator=+%", "Tag1+Tag2+Tag3+Test_tag1+Test_tag2+Test_tag3");
assertPath(profile, img, "%general:case=upper,separator=+%", "Tag1+Tag2+Tag3+Test_Tag1+Test_Tag2+Test_Tag3");
assertPath(profile, img, "%general:case=caps,separator=+%", "TAG1+TAG2+TAG3+TEST_TAG1+TEST_TAG2+TEST_TAG3");
}
SECTION("PathOptionCount")
{
assertPath(profile, img, "%md5% (%count%).%ext%", "1bc29b36f623ba82aaf6724fd3b16718 (7).jpg");
Expand Down

0 comments on commit 141aa4e

Please sign in to comment.