Add '--quiet' comand line option to suppress some output
[ncmpcpp.git] / src / settings.cpp
blob6f26c4d4f2b785a6396b852d1ff09a34296fd3d3
1 /***************************************************************************
2 * Copyright (C) 2008-2017 by Andrzej Rybczak *
3 * electricityispower@gmail.com *
4 * *
5 * This program is free software; you can redistribute it and/or modify *
6 * it under the terms of the GNU General Public License as published by *
7 * the Free Software Foundation; either version 2 of the License, or *
8 * (at your option) any later version. *
9 * *
10 * This program is distributed in the hope that it will be useful, *
11 * but WITHOUT ANY WARRANTY; without even the implied warranty of *
12 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the *
13 * GNU General Public License for more details. *
14 * *
15 * You should have received a copy of the GNU General Public License *
16 * along with this program; if not, write to the *
17 * Free Software Foundation, Inc., *
18 * 51 Franklin St, Fifth Floor, Boston, MA 02110-1301, USA. *
19 ***************************************************************************/
21 #include <boost/tuple/tuple.hpp>
22 #include <boost/tokenizer.hpp>
23 #include <fstream>
24 #include <stdexcept>
26 #include "configuration.h"
27 #include "format_impl.h"
28 #include "helpers.h"
29 #include "settings.h"
30 #include "utility/conversion.h"
31 #include "utility/option_parser.h"
32 #include "utility/type_conversions.h"
34 #ifdef HAVE_LANGINFO_H
35 # include <langinfo.h>
36 #endif
38 Configuration Config;
40 namespace {
42 std::vector<Column> generate_columns(const std::string &format)
44 std::vector<Column> result;
45 std::string width;
46 size_t pos = 0;
47 while (!(width = getEnclosedString(format, '(', ')', &pos)).empty())
49 Column col;
50 auto scolor = getEnclosedString(format, '[', ']', &pos);
51 if (scolor.empty())
52 col.color = NC::Color::Default;
53 else
54 col.color = boost::lexical_cast<NC::Color>(scolor);
56 if (*width.rbegin() == 'f')
58 col.fixed = true;
59 width.resize(width.size()-1);
61 else
62 col.fixed = false;
64 auto tag_type = getEnclosedString(format, '{', '}', &pos);
65 // alternative name
66 size_t tag_type_colon_pos = tag_type.find(':');
67 if (tag_type_colon_pos != std::string::npos)
69 col.name = ToWString(tag_type.substr(tag_type_colon_pos+1));
70 tag_type.resize(tag_type_colon_pos);
73 if (!tag_type.empty())
75 size_t i = -1;
77 // extract tag types in format a|b|c etc.
79 col.type += tag_type[(++i)++]; // nice one.
80 while (tag_type[i] == '|');
82 // apply attributes
83 for (; i < tag_type.length(); ++i)
85 switch (tag_type[i])
87 case 'r':
88 col.right_alignment = 1;
89 break;
90 case 'E':
91 col.display_empty_tag = 0;
92 break;
96 else // empty column
97 col.display_empty_tag = 0;
99 col.width = boost::lexical_cast<int>(width);
100 result.push_back(col);
103 // calculate which column is the last one to have relative width and stretch it accordingly
104 if (!result.empty())
106 int stretch_limit = 0;
107 auto it = result.rbegin();
108 for (; it != result.rend(); ++it)
110 if (it->fixed)
111 stretch_limit += it->width;
112 else
113 break;
115 // if it's false, all columns are fixed
116 if (it != result.rend())
117 it->stretch_limit = stretch_limit;
120 return result;
123 Format::AST<char> columns_to_format(const std::vector<Column> &columns)
125 std::vector<Format::Expression<char>> result;
127 auto column = columns.begin();
128 while (true)
130 Format::FirstOf<char> first_of;
131 for (const auto &type : column->type)
133 auto f = charToGetFunction(type);
134 assert(f != nullptr);
135 first_of.base().push_back(f);
137 result.push_back(std::move(first_of));
139 if (++column != columns.end())
140 result.push_back(" ");
141 else
142 break;
145 return Format::AST<char>(std::move(result));
148 void add_slash_at_the_end(std::string &s)
150 if (s.empty() || *s.rbegin() != '/')
152 s.resize(s.size()+1);
153 *s.rbegin() = '/';
157 std::string adjust_directory(std::string s)
159 add_slash_at_the_end(s);
160 expand_home(s);
161 return s;
164 std::string adjust_path(std::string s)
166 expand_home(s);
167 return s;
170 NC::Buffer buffer(const std::string &v)
172 NC::Buffer result;
173 Format::print(
174 Format::parse(v, Format::Flags::Color | Format::Flags::Format),
175 result,
176 nullptr);
177 return result;
180 void deprecated(const char *option, double version_removal, const char *advice)
182 std::cerr << "WARNING: Variable '" << option
183 << "' is deprecated and will be removed in "
184 << version_removal;
185 if (advice)
186 std::cerr << " (" << advice << ")";
187 std::cerr << ".\n";
192 bool Configuration::read(const std::vector<std::string> &config_paths, bool ignore_errors)
194 option_parser p;
196 p.add<void>("visualizer_sample_multiplier", nullptr, "", [](std::string v) {
197 if (!v.empty())
198 deprecated(
199 "visualizer_sample_multiplier",
200 0.9,
201 "visualizer scales automatically");
203 p.add<void>("progressbar_boldness", nullptr, "", [](std::string v) {
204 if (!v.empty())
205 deprecated(
206 "progressbar_boldness",
207 0.9,
208 "use extended progressbar_color and progressbar_elapsed_color instead");
211 // keep the same order of variables as in configuration file
212 p.add("ncmpcpp_directory", &ncmpcpp_directory, "~/.ncmpcpp/", adjust_directory);
213 p.add("lyrics_directory", &lyrics_directory, "~/.lyrics/", adjust_directory);
214 p.add<void>("mpd_host", nullptr, "localhost", [](std::string host) {
215 expand_home(host);
216 Mpd.SetHostname(host);
218 p.add<void>("mpd_port", nullptr, "6600", [](std::string port) {
219 Mpd.SetPort(verbose_lexical_cast<unsigned>(port));
221 p.add("mpd_music_dir", &mpd_music_dir, "~/music", adjust_directory);
222 p.add("mpd_connection_timeout", &mpd_connection_timeout, "5");
223 p.add("mpd_crossfade_time", &crossfade_time, "5");
224 p.add("visualizer_fifo_path", &visualizer_fifo_path, "/tmp/mpd.fifo", adjust_path);
225 p.add("visualizer_output_name", &visualizer_output_name, "Visualizer feed");
226 p.add("visualizer_in_stereo", &visualizer_in_stereo, "yes", yes_no);
227 p.add("visualizer_sync_interval", &visualizer_sync_interval, "30",
228 [](std::string v) {
229 unsigned sync_interval = verbose_lexical_cast<unsigned>(v);
230 lowerBoundCheck<unsigned>(sync_interval, 10);
231 return boost::posix_time::seconds(sync_interval);
233 p.add("visualizer_type", &visualizer_type, "wave");
234 p.add("visualizer_look", &visualizer_chars, "●▮", [](std::string s) {
235 auto result = ToWString(std::move(s));
236 boundsCheck<std::wstring::size_type>(result.size(), 2, 2);
237 return result;
239 p.add("visualizer_color", &visualizer_colors,
240 "blue, cyan, green, yellow, magenta, red", list_of<NC::FormattedColor>);
241 p.add("system_encoding", &system_encoding, "", [](std::string encoding) {
242 #ifdef HAVE_LANGINFO_H
243 // try to autodetect system encoding
244 if (encoding.empty())
246 encoding = nl_langinfo(CODESET);
247 if (encoding == "UTF-8") // mpd uses utf-8 by default so no need to convert
248 encoding.clear();
250 #endif // HAVE_LANGINFO_H
251 return encoding;
253 p.add("playlist_disable_highlight_delay", &playlist_disable_highlight_delay,
254 "5", [](std::string v) {
255 return boost::posix_time::seconds(verbose_lexical_cast<unsigned>(v));
257 p.add("message_delay_time", &message_delay_time, "5");
258 p.add("song_list_format", &song_list_format,
259 "{%a - }{%t}|{$8%f$9}$R{$3(%l)$9}", [](std::string v) {
260 return Format::parse(v);
262 p.add("song_status_format", &song_status_format,
263 "{{%a{ \"%b\"{ (%y)}} - }{%t}}|{%f}", [this](std::string v) {
264 auto flags = Format::Flags::All ^ Format::Flags::OutputSwitch;
265 // precompute wide format for status display
266 song_status_wformat = Format::parse(ToWString(v), flags);
267 return Format::parse(v, flags);
269 p.add("song_library_format", &song_library_format,
270 "{%n - }{%t}|{%f}", [](std::string v) {
271 return Format::parse(v);
273 p.add("alternative_header_first_line_format", &new_header_first_line,
274 "$b$1$aqqu$/a$9 {%t}|{%f} $1$atqq$/a$9$/b", [](std::string v) {
275 return Format::parse(ToWString(std::move(v)),
276 Format::Flags::All ^ Format::Flags::OutputSwitch);
278 p.add("alternative_header_second_line_format", &new_header_second_line,
279 "{{$4$b%a$/b$9}{ - $7%b$9}{ ($4%y$9)}}|{%D}", [](std::string v) {
280 return Format::parse(ToWString(std::move(v)),
281 Format::Flags::All ^ Format::Flags::OutputSwitch);
283 p.add("now_playing_prefix", &now_playing_prefix,
284 "$b", [this](std::string v) {
285 NC::Buffer result = buffer(v);
286 now_playing_prefix_length = wideLength(ToWString(result.str()));
287 return result;
289 p.add("now_playing_suffix", &now_playing_suffix,
290 "$/b", [this](std::string v) {
291 NC::Buffer result = buffer(v);
292 now_playing_suffix_length = wideLength(ToWString(result.str()));
293 return result;
295 p.add("browser_playlist_prefix", &browser_playlist_prefix, "$2playlist$9 ", buffer);
296 p.add("selected_item_prefix", &selected_item_prefix,
297 "$6", [this](std::string v) {
298 NC::Buffer result = buffer(v);
299 selected_item_prefix_length = wideLength(ToWString(result.str()));
300 return result;
302 p.add("selected_item_suffix", &selected_item_suffix,
303 "$9", [this](std::string v) {
304 NC::Buffer result = buffer(v);
305 selected_item_suffix_length = wideLength(ToWString(result.str()));
306 return result;
308 p.add("modified_item_prefix", &modified_item_prefix, "$3>$9 ", buffer);
309 p.add("song_window_title_format", &song_window_title_format,
310 "{%a - }{%t}|{%f}", [](std::string v) {
311 return Format::parse(v, Format::Flags::Tag);
313 p.add("browser_sort_mode", &browser_sort_mode, "name");
314 p.add("browser_sort_format", &browser_sort_format,
315 "{%a - }{%t}|{%f} {(%l)}", [](std::string v) {
316 return Format::parse(v, Format::Flags::Tag);
318 p.add("song_columns_list_format", &song_columns_mode_format,
319 "(20)[]{a} (6f)[green]{NE} (50)[white]{t|f:Title} (20)[cyan]{b} (7f)[magenta]{l}",
320 [this](std::string v) {
321 columns = generate_columns(v);
322 return columns_to_format(columns);
324 p.add("execute_on_song_change", &execute_on_song_change, "", adjust_path);
325 p.add("execute_on_player_state_change", &execute_on_player_state_change,
326 "", adjust_path);
327 p.add("playlist_show_mpd_host", &playlist_show_mpd_host, "no", yes_no);
328 p.add("playlist_show_remaining_time", &playlist_show_remaining_time, "no", yes_no);
329 p.add("playlist_shorten_total_times", &playlist_shorten_total_times, "no", yes_no);
330 p.add("playlist_separate_albums", &playlist_separate_albums, "no", yes_no);
331 p.add("playlist_display_mode", &playlist_display_mode, "columns");
332 p.add("browser_display_mode", &browser_display_mode, "classic");
333 p.add("search_engine_display_mode", &search_engine_display_mode, "classic");
334 p.add("playlist_editor_display_mode", &playlist_editor_display_mode, "classic");
335 p.add("discard_colors_if_item_is_selected", &discard_colors_if_item_is_selected,
336 "yes", yes_no);
337 p.add("show_duplicate_tags", &MPD::Song::ShowDuplicateTags, "yes", yes_no);
338 p.add("incremental_seeking", &incremental_seeking, "yes", yes_no);
339 p.add("seek_time", &seek_time, "1");
340 p.add("volume_change_step", &volume_change_step, "2");
341 p.add("autocenter_mode", &autocenter_mode, "no", yes_no);
342 p.add("centered_cursor", &centered_cursor, "no", yes_no);
343 p.add("progressbar_look", &progressbar, "=>", [](std::string v) {
344 auto result = ToWString(std::move(v));
345 boundsCheck<std::wstring::size_type>(result.size(), 2, 3);
346 // If two characters were specified, fill \0 as the third one.
347 result.resize(3);
348 return result;
350 p.add("default_place_to_search_in", &search_in_db, "database", [](std::string v) {
351 if (v == "database")
352 return true;
353 else if (v == "playlist")
354 return false;
355 else
356 invalid_value(v);
358 p.add("user_interface", &design, "classic");
359 p.add("data_fetching_delay", &data_fetching_delay, "yes", yes_no);
360 p.add("media_library_primary_tag", &media_lib_primary_tag, "artist", [](std::string v) {
361 if (v == "artist")
362 return MPD_TAG_ARTIST;
363 else if (v == "album_artist")
364 return MPD_TAG_ALBUM_ARTIST;
365 else if (v == "date")
366 return MPD_TAG_DATE;
367 else if (v == "genre")
368 return MPD_TAG_GENRE;
369 else if (v == "composer")
370 return MPD_TAG_COMPOSER;
371 else if (v == "performer")
372 return MPD_TAG_PERFORMER;
373 else
374 invalid_value(v);
376 p.add("default_find_mode", &wrapped_search, "wrapped", [](std::string v) {
377 if (v == "wrapped")
378 return true;
379 else if (v == "normal")
380 return false;
381 else
382 invalid_value(v);
384 p.add("default_tag_editor_pattern", &pattern, "%n - %t");
385 p.add("header_visibility", &header_visibility, "yes", yes_no);
386 p.add("statusbar_visibility", &statusbar_visibility, "yes", yes_no);
387 p.add("titles_visibility", &titles_visibility, "yes", yes_no);
388 p.add("header_text_scrolling", &header_text_scrolling, "yes", yes_no);
389 p.add("cyclic_scrolling", &use_cyclic_scrolling, "no", yes_no);
390 p.add("lines_scrolled", &lines_scrolled, "2");
391 p.add("lyrics_fetchers", &lyrics_fetchers,
392 "lyricwiki, azlyrics, genius, sing365, lyricsmania, metrolyrics, justsomelyrics, jahlyrics, plyrics, tekstowo, internet",
393 list_of<LyricsFetcher_>);
394 p.add("follow_now_playing_lyrics", &now_playing_lyrics, "no", yes_no);
395 p.add("fetch_lyrics_for_current_song_in_background", &fetch_lyrics_in_background,
396 "no", yes_no);
397 p.add("store_lyrics_in_song_dir", &store_lyrics_in_song_dir, "no", yes_no);
398 p.add("generate_win32_compatible_filenames", &generate_win32_compatible_filenames,
399 "yes", yes_no);
400 p.add("allow_for_physical_item_deletion", &allow_for_physical_item_deletion,
401 "no", yes_no);
402 p.add("lastfm_preferred_language", &lastfm_preferred_language, "en");
403 p.add("space_add_mode", &space_add_mode, "add_remove");
404 p.add("show_hidden_files_in_local_browser", &local_browser_show_hidden_files,
405 "no", yes_no);
406 p.add<void>(
407 "screen_switcher_mode", nullptr, "playlist, browser",
408 [this](std::string v) {
409 if (v == "previous")
410 screen_switcher_previous = true;
411 else
413 screen_switcher_previous = false;
414 screen_sequence = list_of<ScreenType>(v, [](std::string s) {
415 auto screen = stringtoStartupScreenType(s);
416 if (screen == ScreenType::Unknown)
417 invalid_value(s);
418 return screen;
422 p.add("startup_screen", &startup_screen_type, "playlist", [](std::string v) {
423 auto screen = stringtoStartupScreenType(v);
424 if (screen == ScreenType::Unknown)
425 invalid_value(v);
426 return screen;
428 p.add("startup_slave_screen", &startup_slave_screen_type, "", [](std::string v) {
429 boost::optional<ScreenType> screen;
430 if (!v.empty())
432 screen = stringtoStartupScreenType(v);
433 if (screen == ScreenType::Unknown)
434 invalid_value(v);
436 return screen;
438 p.add("startup_slave_screen_focus", &startup_slave_screen_focus, "no", yes_no);
439 p.add("locked_screen_width_part", &locked_screen_width_part,
440 "50", [](std::string v) {
441 return verbose_lexical_cast<double>(v) / 100;
443 p.add("ask_for_locked_screen_width_part", &ask_for_locked_screen_width_part,
444 "yes", yes_no);
445 p.add("jump_to_now_playing_song_at_start", &jump_to_now_playing_song_at_start,
446 "yes", yes_no);
447 p.add("ask_before_clearing_playlists", &ask_before_clearing_playlists,
448 "yes", yes_no);
449 p.add("ask_before_shuffling_playlists", &ask_before_shuffling_playlists,
450 "yes", yes_no);
451 p.add("clock_display_seconds", &clock_display_seconds, "no", yes_no);
452 p.add("display_volume_level", &display_volume_level, "yes", yes_no);
453 p.add("display_bitrate", &display_bitrate, "no", yes_no);
454 p.add("display_remaining_time", &display_remaining_time, "no", yes_no);
455 p.add("regular_expressions", &regex_type, "perl", [](std::string v) {
456 if (v == "none")
457 return boost::regex::icase | boost::regex::literal;
458 else if (v == "basic")
459 return boost::regex::icase | boost::regex::basic;
460 else if (v == "extended")
461 return boost::regex::icase | boost::regex::extended;
462 else if (v == "perl")
463 return boost::regex::icase | boost::regex::perl;
464 else
465 invalid_value(v);
467 p.add("ignore_leading_the", &ignore_leading_the, "no", yes_no);
468 p.add("block_search_constraints_change_if_items_found",
469 &block_search_constraints_change, "yes", yes_no);
470 p.add("mouse_support", &mouse_support, "yes", yes_no);
471 p.add("mouse_list_scroll_whole_page", &mouse_list_scroll_whole_page, "yes", yes_no);
472 p.add("empty_tag_marker", &empty_tag, "<empty>");
473 p.add("tags_separator", &MPD::Song::TagsSeparator, " | ");
474 p.add("tag_editor_extended_numeration", &tag_editor_extended_numeration, "no", yes_no);
475 p.add("media_library_sort_by_mtime", &media_library_sort_by_mtime, "no", yes_no);
476 p.add("enable_window_title", &set_window_title, "yes", [](std::string v) {
477 // Consider this variable only if TERM variable is available and we're not
478 // in emacs terminal nor tty (through any wrapper like screen).
479 auto term = getenv("TERM");
480 if (term != nullptr
481 && strstr(term, "linux") == nullptr
482 && strncmp(term, "eterm", const_strlen("eterm")))
483 return yes_no(v);
484 else
486 std::clog << "Terminal doesn't support window title, skipping 'enable_window_title'.\n";
487 return false;
490 p.add("search_engine_default_search_mode", &search_engine_default_search_mode,
491 "1", [](std::string v) {
492 auto mode = verbose_lexical_cast<unsigned>(v);
493 boundsCheck<unsigned>(mode, 1, 3);
494 return --mode;
496 p.add("external_editor", &external_editor, "nano", adjust_path);
497 p.add("use_console_editor", &use_console_editor, "yes", yes_no);
498 p.add("colors_enabled", &colors_enabled, "yes", yes_no);
499 p.add("empty_tag_color", &empty_tags_color, "cyan");
500 p.add("header_window_color", &header_color, "default");
501 p.add("volume_color", &volume_color, "default");
502 p.add("state_line_color", &state_line_color, "default");
503 p.add("state_flags_color", &state_flags_color, "default:b");
504 p.add("main_window_color", &main_color, "yellow");
505 p.add("color1", &color1, "white");
506 p.add("color2", &color2, "green");
507 p.add("main_window_highlight_color", &main_highlight_color, "yellow");
508 p.add("progressbar_color", &progressbar_color, "black:b");
509 p.add("progressbar_elapsed_color", &progressbar_elapsed_color, "green:b");
510 p.add("statusbar_color", &statusbar_color, "default");
511 p.add("statusbar_time_color", &statusbar_time_color, "default:b");
512 p.add("player_state_color", &player_state_color, "default:b");
513 p.add("alternative_ui_separator_color", &alternative_ui_separator_color, "black:b");
514 p.add("active_column_color", &active_column_color, "red");
515 p.add("window_border_color", &window_border, "green", verbose_lexical_cast<NC::Color>);
516 p.add("active_window_border", &active_window_border, "red",
517 verbose_lexical_cast<NC::Color>);
519 return std::all_of(
520 config_paths.begin(),
521 config_paths.end(),
522 [&](const std::string &config_path) {
523 std::ifstream f(config_path);
524 if (f.is_open())
525 std::clog << "Reading configuration from " << config_path << "...\n";
526 return p.run(f, ignore_errors);
528 ) && p.initialize_undefined(ignore_errors);