From b885c4f3a2b86ca356d9a9c68fe48b7e6680ce14 Mon Sep 17 00:00:00 2001 From: Andrzej Rybczak Date: Sun, 23 Aug 2009 22:38:05 +0200 Subject: [PATCH] new feature: command line switch for displaying now playing song --- src/helpers.cpp | 22 +++++++++++++++++++++- src/settings.cpp | 29 +++++++---------------------- src/song.cpp | 15 +++++++++++++++ src/song.h | 2 ++ 4 files changed, 45 insertions(+), 23 deletions(-) diff --git a/src/helpers.cpp b/src/helpers.cpp index 77790e8d..a6d369be 100644 --- a/src/helpers.cpp +++ b/src/helpers.cpp @@ -44,6 +44,7 @@ bool ConnectToMPD() void ParseArgv(int argc, char **argv) { bool quit = 0; + std::string now_playing_format = "{{(%l) }{{%a - }%t}}|{%f}}"; for (int i = 1; i < argc; ++i) { @@ -85,6 +86,8 @@ void ParseArgv(int argc, char **argv) << " -p, --port connect to server at port [6600]\n" << " -?, --help show this help message\n" << " -v, --version display version information\n\n" + << " --now-playing display now playing song [" << now_playing_format << "\n" + << "\n" << " play start playing\n" << " pause pause the currently playing song\n" << " toggle toggle play/pause mode\n" @@ -99,7 +102,24 @@ void ParseArgv(int argc, char **argv) if (!ConnectToMPD()) exit(0); - if (!strcmp(argv[i], "play")) + if (!strcmp(argv[i], "--now-playing")) + { + Mpd.UpdateStatus(); + if (Mpd.GetState() > psStop) + { + if (argc > ++i) + { + // apply additional pair of braces + now_playing_format = "{"; + now_playing_format += argv[i]; + now_playing_format += "}"; + MPD::Song::ValidateFormat("now-playing format", now_playing_format); + } + std::cout << Mpd.GetCurrentSong().toString(now_playing_format) << "\n"; + } + quit = 1; + } + else if (!strcmp(argv[i], "play")) { Mpd.Play(); quit = 1; diff --git a/src/settings.cpp b/src/settings.cpp index 513f513b..f229433a 100644 --- a/src/settings.cpp +++ b/src/settings.cpp @@ -24,7 +24,6 @@ # include #endif // WIN32 #include -#include #include "global.h" #include "helpers.h" @@ -62,20 +61,6 @@ namespace key[1] = !two.empty() && two[0] == '\'' ? two[1] : (atoi(two.c_str()) == 0 ? null_key : atoi(two.c_str())); } - void ValidateSongFormat(const std::string &type, const std::string &s) - { - int braces = 0; - for (std::string::const_iterator it = s.begin(); it != s.end(); ++it) - { - if (*it == '{') - ++braces; - else if (*it == '}') - --braces; - } - if (braces) - throw std::runtime_error(type + ": number of opening and closing braces does not equal!"); - } - Border IntoBorder(const std::string &color) { return Border(IntoColor(color)); @@ -538,7 +523,7 @@ void ReadConfiguration(ncmpcpp_config &conf) { if (!v.empty()) { - ValidateSongFormat("song_list_format", v); + MPD::Song::ValidateFormat("song_list_format", v); conf.song_list_format = '{'; conf.song_list_format += v; conf.song_list_format += '}'; @@ -553,7 +538,7 @@ void ReadConfiguration(ncmpcpp_config &conf) { if (!v.empty()) { - ValidateSongFormat("song_status_format", v); + MPD::Song::ValidateFormat("song_status_format", v); conf.song_status_format = '{'; conf.song_status_format += v; conf.song_status_format += '}'; @@ -563,7 +548,7 @@ void ReadConfiguration(ncmpcpp_config &conf) { if (!v.empty()) { - ValidateSongFormat("song_library_format", v); + MPD::Song::ValidateFormat("song_library_format", v); conf.song_library_format = '{'; conf.song_library_format += v; conf.song_library_format += '}'; @@ -573,7 +558,7 @@ void ReadConfiguration(ncmpcpp_config &conf) { if (!v.empty()) { - ValidateSongFormat("tag_editor_album_format", v); + MPD::Song::ValidateFormat("tag_editor_album_format", v); conf.tag_editor_album_format = '{'; conf.tag_editor_album_format += v; conf.tag_editor_album_format += '}'; @@ -598,7 +583,7 @@ void ReadConfiguration(ncmpcpp_config &conf) { if (!v.empty()) { - ValidateSongFormat("alternative_header_first_line_format", v); + MPD::Song::ValidateFormat("alternative_header_first_line_format", v); conf.new_header_first_line = '{'; conf.new_header_first_line += v; conf.new_header_first_line += '}'; @@ -608,7 +593,7 @@ void ReadConfiguration(ncmpcpp_config &conf) { if (!v.empty()) { - ValidateSongFormat("alternative_header_second_line_format", v); + MPD::Song::ValidateFormat("alternative_header_second_line_format", v); conf.new_header_second_line = '{'; conf.new_header_second_line += v; conf.new_header_second_line += '}'; @@ -790,7 +775,7 @@ void ReadConfiguration(ncmpcpp_config &conf) { if (!v.empty()) { - ValidateSongFormat("song_window_title_format", v); + MPD::Song::ValidateFormat("song_window_title_format", v); conf.song_window_title_format = '{'; conf.song_window_title_format += v; conf.song_window_title_format += '}'; diff --git a/src/song.cpp b/src/song.cpp index af7c32d0..7610c12a 100644 --- a/src/song.cpp +++ b/src/song.cpp @@ -25,6 +25,7 @@ #include #include #include +#include #include "charset.h" #include "misc.h" @@ -446,6 +447,20 @@ std::string MPD::Song::ShowTime(int length) return ss.str(); } +void MPD::Song::ValidateFormat(const std::string &type, const std::string &s) +{ + int braces = 0; + for (std::string::const_iterator it = s.begin(); it != s.end(); ++it) + { + if (*it == '{') + ++braces; + else if (*it == '}') + --braces; + } + if (braces) + throw std::runtime_error(type + ": number of opening and closing braces does not equal!"); +} + void MPD::Song::SetHashAndSlash() { if (!itsSong->file) diff --git a/src/song.h b/src/song.h index 6b66915c..e1c74603 100644 --- a/src/song.h +++ b/src/song.h @@ -92,6 +92,8 @@ namespace MPD Song &operator=(const Song &); static std::string ShowTime(int); + static void ValidateFormat(const std::string &type, const std::string &format); + private: void SetHashAndSlash(); std::string ParseFormat(std::string::const_iterator &it) const;