diff --git a/doc/config b/doc/config index 719539a3..f966c6f3 100644 --- a/doc/config +++ b/doc/config @@ -179,15 +179,15 @@ #tag_editor_album_format = "{(%y) }%b" # ## -## Note: Below variables are used for sorting the library browser. -## The sort mode determines how the song library is sorted, and can -## be used in combination with a sort format to specify a custom sorting format. -## Possible values for sort_mode are "name", "mtime" and "format". +## Note: Below variables are used for sorting songs in browser. +## The sort mode determines how songs are sorted, and can be used +## in combination with a sort format to specify a custom sorting format. +## Possible values for browser_sort_mode are "name", "mtime" and "format". ## # -#sort_mode = "name" +#browser_sort_mode = "name" # -#sort_format = "{%b~%n}|{~%n}|{~~%t}|{~~%f}" +#browser_sort_format = "{%a - }{%t}|{%f} {(%l)}" # ## ## Note: Below variables are for alternative version of user's interface. diff --git a/doc/ncmpcpp.1 b/doc/ncmpcpp.1 index bbd9eb21..9ddf4c22 100644 --- a/doc/ncmpcpp.1 +++ b/doc/ncmpcpp.1 @@ -132,11 +132,11 @@ Format for albums' list in Tag editor. .B song_window_title_format Song format for window title. .TP -.B sort_mode -Determines the sort mode for the song library. Possible values are "name", "mtime" and "format". +.B browser_sort_mode +Determines sort mode for browser. Possible values are "name", "mtime" and "format". .TP -.B sort_format -Format to use for sorting the song library. For this option to be effective, sort_mode must be set to "format". +.B browser_sort_format +Format to use for sorting songs in browser. For this option to be effective, browser_sort_mode must be set to "format". .TP .B external_editor = PATH Path to external editor used to edit lyrics. @@ -301,7 +301,7 @@ If enabled, bitrate of currently playing song will be displayed in statusbar. If enabled, remaining time of currently playing song will be be displayed in statusbar instead of elapsed time. .TP .B ignore_leading_the = yes/no -If enabled, word "the" at the beginning of tags/filenames will be ignored while sorting items. Note that this doesn't currently work with custom sorting formats. +If enabled, word "the" at the beginning of tags/filenames/sort format will be ignored while sorting items. .TP .B use_console_editor = yes/no If your external editor is console application, you need to enable it. diff --git a/src/browser.cpp b/src/browser.cpp index 6e866e1a..dd61c4b7 100644 --- a/src/browser.cpp +++ b/src/browser.cpp @@ -100,6 +100,9 @@ void Browser::SwitchTo() if (hasToBeResized || myLockedScreen) Resize(); + if (isLocal() && Config.browser_sort_mode == smMTime) // local browser doesn't support sorting by mtime + Config.browser_sort_mode = smName; + w->Empty() ? myBrowser->GetDirectory(itsBrowsedDir) : myBrowser->UpdateItemList(); if (myScreen != this && myScreen->isTabbable()) diff --git a/src/helpers.cpp b/src/helpers.cpp index c81b4b54..78f32a96 100644 --- a/src/helpers.cpp +++ b/src/helpers.cpp @@ -298,30 +298,36 @@ int CaseInsensitiveStringComparison::operator()(const std::string &a, const std: bool CaseInsensitiveSorting::operator()(const MPD::Item &a, const MPD::Item &b) { + bool result; if (a.type == b.type) { switch (a.type) { case MPD::itDirectory: - return cmp(ExtractTopName(a.name), ExtractTopName(b.name)) < 0; + result = cmp(ExtractTopName(a.name), ExtractTopName(b.name)) < 0; + break; case MPD::itPlaylist: - return cmp(a.name, b.name) < 0; - case MPD::itSong: { - unsigned mode = Config.sort_mode; - if (myBrowser->isLocal() && mode == 1) mode = 0; // local browser doesn't support sorting by mtime. - switch (mode) { - case 0: return operator()(a.song, b.song); - case 1: return a.song->GetMTime() > b.song->GetMTime(); - case 2: return cmp(a.song->toString(Config.sort_format), b.song->toString(Config.sort_format)) < 0; - default: return 0; // no other mode. + result = cmp(a.name, b.name) < 0; + break; + case MPD::itSong: + switch (Config.browser_sort_mode) + { + case smName: + result = operator()(a.song, b.song); + break; + case smMTime: + result = a.song->GetMTime() > b.song->GetMTime(); + break; + case smCustomFormat: + result = cmp(a.song->toString(Config.browser_sort_format), b.song->toString(Config.browser_sort_format)) < 0; + break; } - } - default: // there's no other type, just silence compiler. - return 0; + break; } } else - return a.type < b.type; + result = a.type < b.type; + return result; } std::string Timestamp(time_t t) diff --git a/src/ncmpcpp.cpp b/src/ncmpcpp.cpp index e175a55c..fd181dd4 100644 --- a/src/ncmpcpp.cpp +++ b/src/ncmpcpp.cpp @@ -2175,15 +2175,28 @@ int main(int argc, char **argv) if (number && (answer == 's' ? Mpd.AddRandomSongs(number) : Mpd.AddRandomTag(tag_type, number))) ShowMessage("%zu random %s%s added to playlist!", number, tag_type_str.c_str(), number == 1 ? "" : "s"); } - else if (myScreen == myBrowser && !myBrowser->isLocal()) + else if (myScreen == myBrowser) { - Config.sort_mode = (Config.sort_mode + 1) % 3; - myBrowser->Main()->Sort(myBrowser->CurrentDir() != "/"); - switch (Config.sort_mode) { - case 0: ShowMessage("Sort songs by: Name"); break; - case 1: ShowMessage("Sort songs by: Modification time"); break; - case 2: ShowMessage("Sort songs by: Custom format"); break; + switch (Config.browser_sort_mode) + { + case smName: + if (!myBrowser->isLocal()) + { + Config.browser_sort_mode = smMTime; + ShowMessage("Sort songs by: Modification time"); + break; + } + // local browser doesn't support sorting by mtime, so we just skip it. + case smMTime: + Config.browser_sort_mode = smCustomFormat; + ShowMessage("Sort songs by: Custom format"); + break; + case smCustomFormat: + Config.browser_sort_mode = smName; + ShowMessage("Sort songs by: Name"); + break; } + myBrowser->Main()->Sort(myBrowser->CurrentDir() != "/"); } else if (myScreen->ActiveWindow() == myLibrary->Artists || (myLibrary->Columns() == 2 && myScreen->ActiveWindow() == myLibrary->Albums)) diff --git a/src/settings.cpp b/src/settings.cpp index c88505ac..ba2ddb2c 100644 --- a/src/settings.cpp +++ b/src/settings.cpp @@ -372,7 +372,7 @@ void NcmpcppConfig::SetDefaults() song_status_format_no_colors = song_status_format; song_window_title_format = "{{%a - }{%t}|{%f}}"; song_library_format = "{{%n - }{%t}|{%f}}"; - sort_format = "{%b~%n}|{~%n}|{~~%t}|{~~%f}"; + browser_sort_format = "{{%a - }{%t}|{%f} {(%l)}}"; tag_editor_album_format = "{{(%y) }%b}"; new_header_first_line = "{$b$1$aqqu$/a$9 {%t}|{%f} $1$atqq$/a$9$/b}"; new_header_second_line = "{{{$4$b%a$/b$9}{ - $7%b$9}{ ($4%y$9)}}|{%D}}"; @@ -464,7 +464,6 @@ void NcmpcppConfig::SetDefaults() lines_scrolled = 2; search_engine_default_search_mode = 0; visualizer_sync_interval = 30; - sort_mode = 0; locked_screen_width_part = 0.5; selected_item_suffix_length = 0; now_playing_suffix_length = 0; @@ -474,6 +473,7 @@ void NcmpcppConfig::SetDefaults() system_encoding.clear(); # endif // HAVE_LANGINFO_H startup_screen = myPlaylist; + browser_sort_mode = smName; // default screens sequence screens_seq.push_back(myPlaylist); screens_seq.push_back(myBrowser); @@ -854,10 +854,14 @@ void NcmpcppConfig::Read() tag_editor_album_format += '}'; } } - else if (name == "sort_format") + else if (name == "browser_sort_format") { - if (!v.empty()) - sort_format = v; + if (!v.empty() && MPD::Song::isFormatOk("browser_sort_format", v)) + { + browser_sort_format = '{'; + browser_sort_format += v; + browser_sort_format += '}'; + } } else if (name == "external_editor") { @@ -1240,9 +1244,12 @@ void NcmpcppConfig::Read() } else if (name == "sort_mode") { - if (v == "mtime") sort_mode = 1; - else if (v == "format") sort_mode = 2; - else sort_mode = 0; // "name" or invalid + if (v == "mtime") + browser_sort_mode = smMTime; + else if (v == "format") + browser_sort_mode = smCustomFormat; + else + browser_sort_mode = smName; // "name" or invalid } else if (name == "locked_screen_width_part") { diff --git a/src/settings.h b/src/settings.h index 69562f97..3ae364b3 100644 --- a/src/settings.h +++ b/src/settings.h @@ -29,6 +29,8 @@ class BasicScreen; // forward declaration for screens sequence +enum SortMode { smName, smMTime, smCustomFormat }; + struct Column { Column() : right_alignment(0), display_empty_tag(1) { } @@ -168,7 +170,7 @@ struct NcmpcppConfig std::string song_library_format; std::string tag_editor_album_format; std::string song_in_columns_to_string_format; - std::string sort_format; + std::string browser_sort_format; std::string external_editor; std::string system_encoding; std::string execute_on_song_change; @@ -273,7 +275,6 @@ struct NcmpcppConfig unsigned lines_scrolled; unsigned search_engine_default_search_mode; unsigned visualizer_sync_interval; - unsigned sort_mode; double locked_screen_width_part; @@ -283,6 +284,8 @@ struct NcmpcppConfig BasicScreen *startup_screen; std::list screens_seq; + SortMode browser_sort_mode; + private: void MakeProperPath(std::string &dir);