Imported Upstream version 17.3.0
[platform/upstream/libzypp.git] / zypp / base / DrunkenBishop.h
1 /*---------------------------------------------------------------------\
2 |                          ____ _   __ __ ___                          |
3 |                         |__  / \ / / . \ . \                         |
4 |                           / / \ V /|  _/  _/                         |
5 |                          / /__ | | | | | |                           |
6 |                         /_____||_| |_| |_|                           |
7 |                                                                      |
8 \---------------------------------------------------------------------*/
9 /** \file       zypp/base/DrunkenBishop.h
10  */
11 #ifndef ZYPP_BASE_DRUNKENBISHOP_H
12 #define ZYPP_BASE_DRUNKENBISHOP_H
13
14 #include <iosfwd>
15 #include <vector>
16 #include <string>
17
18 #include "zypp/base/PtrTypes.h"
19 #include "zypp/base/Flags.h"
20
21 ///////////////////////////////////////////////////////////////////
22 namespace zypp
23 {
24   ///////////////////////////////////////////////////////////////////
25   namespace base
26   {
27     ///////////////////////////////////////////////////////////////////
28     /// \class DrunkenBishop
29     /// \brief Random art fingerprint visualization
30     /// Visualize fingerprint data on a [17x9] (SSH) or [19x11] (GPG) or
31     /// custom sized board. The default board size and layout depends on
32     /// the data string length (above 32 the GPG board and layout is used).
33     ///
34     /// The data string should be an even sized HEX string, otherwise
35     /// it will be 0-padded.
36     ///
37     /// All ctor calls may throw \ref std::invalid_argument.
38     ///
39     /// \code
40     ///     [SuSE Package Signing Key <build@suse.de>]
41     ///      +------[Title]------+
42     ///      |   . . ^           |  fpr FEAB502539D846DB2C0961CA70AF9E8139DB7C82
43     ///      |    : : .          |   id 70AF9E8139DB7C82
44     ///      |     ^ ^ .         |  cre 1481108255 Wed Dec  7 11:57:35 2016
45     ///      |    ^ . l i        |  exp 1607252255 Sun Dec  6 11:57:35 2020
46     ///      |   : ^ . f :       |  ttl 992
47     ///      |    ? ^ .Sl        |  rpm 39db7c82-5847eb1f
48     ///      |   E i ...         |
49     ///      |      ^ ..         |
50     ///      |       .  .        |
51     ///      |        .  .       |
52     ///      |         ....      |
53     ///      +----[39DB7C82]-----+
54     /// \endcode
55     ////
56     /// Based on https://github.com/atoponce/keyart, the development location
57     /// for the Debian signing-party package. We try to use the same charset
58     /// and heatmap.
59     /// See also http://dirk-loss.de/sshvis/drunken_bishop.pdf.
60     ///////////////////////////////////////////////////////////////////
61     class DrunkenBishop
62     {
63       friend std::ostream & operator<<( std::ostream & str, const DrunkenBishop & obj );
64
65     public:
66       /** Default ctor: empty board (1x1) */
67       DrunkenBishop();
68
69       /** Ctor taking a data string (and optional title) and using a default (SSH/GPG) board. */
70       DrunkenBishop( const std::string & data_r, const std::string & title_r = std::string() );
71
72       /** Ctor also taking a desired board height (even value is incremented, width is 2*height-1). */
73       DrunkenBishop( const std::string & data_r, const std::string & title_r, unsigned height_r );
74       /** Ctor \overload without optional title */
75       DrunkenBishop( const std::string & data_r, unsigned height_r )
76       : DrunkenBishop( data_r, std::string(), height_r )
77       {}
78
79       /** Ctor also taking a desired board height and width (even values are incremented). */
80       DrunkenBishop( const std::string & data_r, const std::string & title_r, unsigned height_r, unsigned width_r );
81       /** Ctor \overload without optional title  */
82       DrunkenBishop( const std::string & data_r, unsigned height_r, unsigned width_r )
83       : DrunkenBishop( data_r, std::string(), height_r, width_r )
84       {}
85
86       /** Dtor */
87       ~DrunkenBishop();
88
89     public:
90       /* Rendering options */
91       enum OptionBits {
92         USE_COLOR       = (1<<0),       ///< use colors
93       };
94       ZYPP_DECLARE_FLAGS(Options,OptionBits);
95
96       /** Render board to steam.*/
97       std::ostream & dumpOn( std::ostream & str, Options options_r = Options() ) const
98       { return dumpOn( str, std::string(), options_r ); }
99       /** \overload taking an indent string prefixing each line. */
100       std::ostream & dumpOn( std::ostream & str, const std::string & prefix_r, Options options_r = Options() ) const;
101
102       /** Render board as string.*/
103       std::string asString( Options options_r = Options() ) const
104       { return asString( std::string(), options_r ); }
105       /** \overload taking an indent string prefixing each line. */
106       std::string asString( const std::string & prefix_r, Options options_r = Options() ) const;
107
108       /** Render to an array of lines. */
109       std::vector<std::string> asLines( Options options_r = Options() ) const
110       { return asLines( std::string(), options_r ); }
111       /** \overload taking an indent string prefixing each line. */
112       std::vector<std::string> asLines( const std::string & prefix_r, Options options_r = Options() ) const;
113
114     public:
115       class Impl;              ///< Implementation class.
116     private:
117       RW_pointer<Impl> _pimpl; ///< Pointer to implementation.
118     };
119
120     ZYPP_DECLARE_OPERATORS_FOR_FLAGS(DrunkenBishop::Options);
121
122     /** \relates DrunkenBishop Stream output */
123     inline std::ostream & operator<<( std::ostream & str, const DrunkenBishop & obj )
124     { return obj.dumpOn( str ); }
125
126   } // namespace base
127   ///////////////////////////////////////////////////////////////////
128 } // namespace zypp
129 ///////////////////////////////////////////////////////////////////
130 #endif // ZYPP_BASE_DRUNKENBISHOP_H