song format: add support for limiting maximal length of a tag

This commit is contained in:
Andrzej Rybczak
2010-08-22 20:56:38 +02:00
parent e4b3d6aea2
commit f09f7c396f
3 changed files with 26 additions and 5 deletions

View File

@@ -115,6 +115,10 @@
## only if all requested values are available and/or define alternate ## only if all requested values are available and/or define alternate
## value with { }|{ } eg. {%a - %t}|{%f} ## value with { }|{ } eg. {%a - %t}|{%f}
## ##
## Note: If you want to set limit on maximal length of a tag, just
## put the appropriate number between % and character that defines
## tag type, e.g. to make album take max. 20 terminal cells, use '%20b'.
##
## Note: Format that is similar to "%a - %t" (i.e. without any additional ## Note: Format that is similar to "%a - %t" (i.e. without any additional
## braces) is equal to "{%a - %t}", so if one of the tags is missing, ## braces) is equal to "{%a - %t}", so if one of the tags is missing,
## you'll get nothing. ## you'll get nothing.

View File

@@ -369,6 +369,8 @@ For song format you can use:
You can also put them in { } and then they will be displayed only if all requested values are available and/or define alternate value with { }|{ } e.g. {%a - %t}|{%f} will check if artist and title tags are available and if they are, display them. Otherwise it'll display filename. You can also put them in { } and then they will be displayed only if all requested values are available and/or define alternate value with { }|{ } e.g. {%a - %t}|{%f} will check if artist and title tags are available and if they are, display them. Otherwise it'll display filename.
\fBNote\fR: If you want to set limit on maximal length of a tag, just put the appropriate number between % and character that defines tag type, e.g. to make album take max. 20 terminal cells, use '%20b'.
\fBNote\fR: Format that is similar to "%a - %t" (i.e. without any additional braces) is equal to "{%a - %t}", so if one of the tags is missing, you'll get nothing. \fBNote\fR: Format that is similar to "%a - %t" (i.e. without any additional braces) is equal to "{%a - %t}", so if one of the tags is missing, you'll get nothing.
Text can have different color than the main window, e.g. if you want length to be green, write $3%l$9. Text can have different color than the main window, e.g. if you want length to be green, write $3%l$9.

View File

@@ -22,6 +22,7 @@
#include <config.h> #include <config.h>
#endif #endif
#include <cstdlib>
#include <cstring> #include <cstring>
#include <iomanip> #include <iomanip>
#include <sstream> #include <sstream>
@@ -370,13 +371,21 @@ std::string MPD::Song::ParseFormat(std::string::const_iterator &it, const char *
if (*it == '%') if (*it == '%')
{ {
switch (*++it) size_t delimiter = 0;
if (isdigit(*++it))
{ {
case '%': delimiter = atol(&*it);
result += *it; // no break here while (isdigit(*++it)) { }
default:
get = toGetFunction(*it);
} }
if (*it == '%')
{
result += *it;
get = 0;
}
else
get = toGetFunction(*it);
if (get) if (get)
{ {
std::string tag = GetTags(get); std::string tag = GetTags(get);
@@ -386,6 +395,12 @@ std::string MPD::Song::ParseFormat(std::string::const_iterator &it, const char *
tag.replace(i, 1, std::string(1, FormatEscapeCharacter) + *ch); tag.replace(i, 1, std::string(1, FormatEscapeCharacter) + *ch);
if (!tag.empty() && (get != &MPD::Song::GetLength || GetTotalLength())) if (!tag.empty() && (get != &MPD::Song::GetLength || GetTotalLength()))
{ {
if (delimiter)
{
const std::basic_string<my_char_t> &s = TO_WSTRING(tag);
if (NCurses::Window::Length(s) > delimiter)
tag = Shorten(s, delimiter);
}
has_some_tags = 1; has_some_tags = 1;
result += tag; result += tag;
} }