1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
|
/***************************************************************************
* Copyright (C) 2008-2014 by Andrzej Rybczak *
* electricityispower@gmail.com *
* *
* This program is free software; you can redistribute it and/or modify *
* it under the terms of the GNU General Public License as published by *
* the Free Software Foundation; either version 2 of the License, or *
* (at your option) any later version. *
* *
* This program is distributed in the hope that it will be useful, *
* but WITHOUT ANY WARRANTY; without even the implied warranty of *
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the *
* GNU General Public License for more details. *
* *
* You should have received a copy of the GNU General Public License *
* along with this program; if not, write to the *
* Free Software Foundation, Inc., *
* 51 Franklin St, Fifth Floor, Boston, MA 02110-1301, USA. *
***************************************************************************/
#include <algorithm>
#include <time.h>
#include "helpers.h"
#include "playlist.h"
#include "statusbar.h"
bool addSongToPlaylist(const MPD::Song &s, bool play, int position)
{
bool result = false;
if (Config.space_add_mode == SpaceAddMode::AddRemove && myPlaylist->checkForSong(s))
{
auto &w = myPlaylist->main();
if (play)
{
auto song = std::find(w.beginV(), w.endV(), s);
assert(song != w.endV());
Mpd.PlayID(song->getID());
result = true;
}
else
{
Mpd.StartCommandsList();
for (auto it = w.rbeginV(); it != w.rendV(); ++it)
if (*it == s)
Mpd.Delete(it->getPosition());
Mpd.CommitCommandsList();
// we return false in this case
}
}
else
{
int id = Mpd.AddSong(s, position);
if (id >= 0)
{
Statusbar::printf("Added to playlist: %s",
s.toString(Config.song_status_format_no_colors, Config.tags_separator)
);
if (play)
Mpd.PlayID(id);
result = true;
}
}
return result;
}
std::string timeFormat(const char *format, time_t t)
{
char result[32];
tm tinfo;
localtime_r(&t, &tinfo);
strftime(result, sizeof(result), format, &tinfo);
return result;
}
std::string Timestamp(time_t t)
{
char result[32];
# ifdef WIN32
result[strftime(result, 31, "%x %X", localtime(&t))] = 0;
# else
tm info;
result[strftime(result, 31, "%x %X", localtime_r(&t, &info))] = 0;
# endif // WIN32
return result;
}
void markSongsInPlaylist(ProxySongList pl)
{
size_t list_size = pl.size();
for (size_t i = 0; i < list_size; ++i)
if (auto s = pl.getSong(i))
pl.setBold(i, myPlaylist->checkForSong(*s));
}
std::wstring Scroller(const std::wstring &str, size_t &pos, size_t width)
{
std::wstring s(str);
if (!Config.header_text_scrolling)
return s;
std::wstring result;
size_t len = wideLength(s);
if (len > width)
{
s += L" ** ";
len = 0;
auto b = s.begin(), e = s.end();
for (auto it = b+pos; it < e && len < width; ++it)
{
if ((len += wcwidth(*it)) > width)
break;
result += *it;
}
if (++pos >= s.length())
pos = 0;
for (; len < width; ++b)
{
if ((len += wcwidth(*b)) > width)
break;
result += *b;
}
}
else
result = s;
return result;
}
void writeCyclicBuffer(const NC::WBuffer &buf, NC::Window &w, size_t &start_pos,
size_t width, const std::wstring &separator)
{
const auto &s = buf.str();
size_t len = wideLength(s);
if (len > width)
{
len = 0;
const auto &ps = buf.properties();
auto p = ps.begin();
// load attributes from before starting pos
for (; p != ps.end() && p->position() < start_pos; ++p)
w << *p;
auto write_buffer = [&](size_t start) {
for (size_t i = start; i < s.length() && len < width; ++i)
{
for (; p != ps.end() && p->position() == i; ++p)
w << *p;
len += wcwidth(s[i]);
if (len > width)
break;
w << s[i];
}
for (; p != ps.end(); ++p)
w << *p;
p = ps.begin();
};
write_buffer(start_pos);
size_t i = 0;
if (start_pos > s.length())
i = start_pos - s.length();
for (; i < separator.length() && len < width; ++i)
{
len += wcwidth(separator[i]);
if (len > width)
break;
w << separator[i];
}
write_buffer(0);
++start_pos;
if (start_pos >= s.length() + separator.length())
start_pos = 0;
}
else
w << buf;
}
|