File: format.h

package info (click to toggle)
crawl 2%3A0.34.0-1
  • links: PTS, VCS
  • area: main
  • in suites: forky, sid
  • size: 100,188 kB
  • sloc: cpp: 363,709; ansic: 27,765; javascript: 9,516; python: 8,463; perl: 3,293; java: 3,132; xml: 2,380; makefile: 1,835; sh: 611; objc: 250; cs: 15; sed: 9; lisp: 3
file content (135 lines) | stat: -rw-r--r-- 3,306 bytes parent folder | download | duplicates (2)
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
#pragma once

#include <string>
#include <vector>

#include "externs.h"
#include "macros.h"

using std::string;

// Definitions for formatted_string

enum fs_op_type
{
    FSOP_COLOUR,
    FSOP_TEXT,
    FSOP_BG,
};

class formatted_string
{
public:
    explicit formatted_string(int init_colour = 0);
    explicit formatted_string(const string &s, int init_colour = 0);

    operator string() const;
    void display(int start = 0, int end = -1) const;
    string tostring(int start = 0, int end = -1) const;
    string to_colour_string(int default_colour=COLOUR_INHERIT) const;

    void cprintf(PRINTF(1, ));
    void cprintf(const string &s);
    void add_glyph(cglyph_t g);
    void textcolour(int colour);
    void textbackground(int colour);
    formatted_string chop(int length, bool pad=false) const;
    formatted_string chop_bytes(int length) const;
    formatted_string substr_bytes(int pos, int length) const;
    formatted_string trim() const;
    void del_char();
    void all_caps();
    void capitalise();
    void filter_lang();

    void clear();
    bool empty() const;

    void swap(formatted_string& other);

    int width() const;

    bool operator < (const formatted_string &other) const;
    bool operator == (const formatted_string &other) const;
    const formatted_string &operator += (const formatted_string &other);
    const formatted_string &operator += (const string &other);
    char &operator [] (size_t idx);

public:
    static formatted_string parse_string(
            const string &s,
            int main_colour = LIGHTGREY);

    static void parse_string_to_multiple(const string &s,
                                         vector<formatted_string> &out,
                                         int wrap_col = 0);


private:
    static int get_colour(const string &tag);
    int find_last_colour() const;

    static void parse_string1(const string &s, formatted_string &fs,
                              vector<int> &colour_stack,
                              vector<int> &bg_stack);

public:
    struct fs_op
    {
        fs_op_type type;
        int colour;
        string text;

        fs_op(int _colour, bool fg=true)
            : type(fg ? FSOP_COLOUR : FSOP_BG), colour(_colour), text()
        {
        }

        fs_op(const string &s) : type(FSOP_TEXT), colour(-1), text(s)
        {
        }

        bool operator == (const fs_op &other) const
        {
            return type == other.type
                && colour == other.colour
                && text == other.text;
        }
        void display() const;
    };

    typedef vector<fs_op> oplist;
    oplist ops;
};

template<typename R>
formatted_string operator+(const formatted_string& lhs, const R&& rhs)
{
    formatted_string ret = lhs;
    ret += rhs;
    return ret;
}

template<typename R>
formatted_string&& operator+(formatted_string&& lhs, const R&& rhs)
{
    lhs += rhs;
    return std::move(lhs);
}

inline formatted_string operator+(const formatted_string& lhs, const char* rhs)
{
    formatted_string ret = lhs;
    ret += rhs;
    return ret;
}

inline formatted_string&& operator+(formatted_string&& lhs, const char* rhs)
{
    lhs += rhs;
    return std::move(lhs);
}

int count_linebreaks(const formatted_string& fs);

void display_tagged_block(const string& s);