new feature: command line switch for displaying now playing song

This commit is contained in:
Andrzej Rybczak
2009-08-23 22:38:05 +02:00
parent 56fd7bd7fc
commit b885c4f3a2
4 changed files with 45 additions and 23 deletions

View File

@@ -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;

View File

@@ -24,7 +24,6 @@
# include <sys/stat.h>
#endif // WIN32
#include <fstream>
#include <stdexcept>
#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 += '}';

View File

@@ -25,6 +25,7 @@
#include <cstring>
#include <iomanip>
#include <sstream>
#include <stdexcept>
#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)

View File

@@ -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;