Formatted color: include <istream>
[ncmpcpp.git] / src / curses / formatted_color.cpp
blob285b8e67c73268b2058201c9ca11986df125f7bb
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 <istream>
22 #include "formatted_color.h"
24 namespace {
26 void verifyFormats(const NC::FormattedColor::Formats &formats)
28 for (auto &fmt : formats)
30 if (fmt == NC::Format::NoBold
31 || fmt == NC::Format::NoUnderline
32 || fmt == NC::Format::NoReverse
33 || fmt == NC::Format::NoAltCharset)
34 throw std::logic_error("FormattedColor can't hold disabling formats");
40 NC::FormattedColor::FormattedColor(Color color_, Formats formats_)
42 if (color_ == NC::Color::End)
43 throw std::logic_error("FormattedColor can't hold Color::End");
44 m_color = std::move(color_);
45 verifyFormats(formats_);
46 m_formats = std::move(formats_);
49 std::istream &NC::operator>>(std::istream &is, NC::FormattedColor &fc)
51 NC::Color c;
52 is >> c;
53 if (!is.eof() && is.peek() == ':')
55 is.get();
56 NC::FormattedColor::Formats formats;
57 while (!is.eof() && isalpha(is.peek()))
59 char flag = is.get();
60 switch (flag)
62 case 'b':
63 formats.push_back(NC::Format::Bold);
64 break;
65 case 'u':
66 formats.push_back(NC::Format::Underline);
67 break;
68 case 'r':
69 formats.push_back(NC::Format::Reverse);
70 break;
71 case 'a':
72 formats.push_back(NC::Format::AltCharset);
73 break;
74 default:
75 is.setstate(std::ios::failbit);
76 break;
79 fc = NC::FormattedColor(c, std::move(formats));
81 else
82 fc = NC::FormattedColor(c, {});
83 return is;