From f09f7c396f23dba080212a06d0796ef4ce247ec7 Mon Sep 17 00:00:00 2001 From: Andrzej Rybczak Date: Sun, 22 Aug 2010 20:56:38 +0200 Subject: [PATCH] song format: add support for limiting maximal length of a tag --- doc/config | 4 ++++ doc/ncmpcpp.1 | 2 ++ src/song.cpp | 25 ++++++++++++++++++++----- 3 files changed, 26 insertions(+), 5 deletions(-) diff --git a/doc/config b/doc/config index 4925957b..64fc1406 100644 --- a/doc/config +++ b/doc/config @@ -115,6 +115,10 @@ ## only if all requested values are available and/or define alternate ## 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 ## braces) is equal to "{%a - %t}", so if one of the tags is missing, ## you'll get nothing. diff --git a/doc/ncmpcpp.1 b/doc/ncmpcpp.1 index 852c0354..8bec5210 100644 --- a/doc/ncmpcpp.1 +++ b/doc/ncmpcpp.1 @@ -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. +\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. Text can have different color than the main window, e.g. if you want length to be green, write $3%l$9. diff --git a/src/song.cpp b/src/song.cpp index 5c44b9b6..a0e9e7b6 100644 --- a/src/song.cpp +++ b/src/song.cpp @@ -22,6 +22,7 @@ #include #endif +#include #include #include #include @@ -370,13 +371,21 @@ std::string MPD::Song::ParseFormat(std::string::const_iterator &it, const char * if (*it == '%') { - switch (*++it) + size_t delimiter = 0; + if (isdigit(*++it)) { - case '%': - result += *it; // no break here - default: - get = toGetFunction(*it); + delimiter = atol(&*it); + while (isdigit(*++it)) { } } + + if (*it == '%') + { + result += *it; + get = 0; + } + else + get = toGetFunction(*it); + if (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); if (!tag.empty() && (get != &MPD::Song::GetLength || GetTotalLength())) { + if (delimiter) + { + const std::basic_string &s = TO_WSTRING(tag); + if (NCurses::Window::Length(s) > delimiter) + tag = Shorten(s, delimiter); + } has_some_tags = 1; result += tag; }