monotone

monotone Mtn Source Tree

Root/ui.cc

1#include "ui.hh"
2#include "sanity.hh"
3
4#include <iostream>
5#include <boost/lexical_cast.hpp>
6
7// copyright (C) 2002, 2003 graydon hoare <graydon@pobox.com>
8// all rights reserved.
9// licensed to the public under the terms of the GNU GPL (>= 2)
10// see the file COPYING for details
11
12// this file contains a couple utilities to deal with the user
13// interface. the global user_interface object 'ui' owns clog, so no
14// writing to it directly!
15
16using namespace std;
17using boost::lexical_cast;
18struct user_interface ui;
19
20ticker::ticker(string const & tickname, size_t mod) : mod(mod), name (tickname)
21{
22 I(ui.ticks.find(tickname) == ui.ticks.end());
23 ui.ticks.insert(make_pair(tickname,0));
24}
25
26ticker::~ticker()
27{
28 I(ui.ticks.find(name) != ui.ticks.end());
29 ui.ticks.erase(name);
30 ui.finish_ticking();
31}
32
33void ticker::operator++()
34{
35 I(ui.ticks.find(name) != ui.ticks.end());
36 ui.ticks[name]++;
37 if (ui.ticks[name] % mod == 0)
38 ui.write_ticks();
39}
40
41void ticker::operator+=(size_t t)
42{
43 I(ui.ticks.find(name) != ui.ticks.end());
44 size_t old = ui.ticks[name];
45
46 ui.ticks[name] += t;
47 if ((old+t) % mod == 0
48 || (old % mod) > ((old+t) % mod)
49 || t > mod)
50 ui.write_ticks();
51}
52
53
54user_interface::user_interface() :
55 last_write_was_a_tick(false),
56 last_tick_len(0)
57{
58 clog.sync_with_stdio(false);
59 clog.unsetf(ios_base::unitbuf);
60}
61
62user_interface::~user_interface()
63{
64}
65
66void user_interface::finish_ticking()
67{
68 if (ticks.size() == 0 &&
69 last_write_was_a_tick)
70 {
71 tick_trailer = "";
72 clog << endl;
73 last_write_was_a_tick = false;
74 }
75}
76
77void user_interface::set_tick_trailer(string const & t)
78{
79 tick_trailer = t;
80}
81
82void user_interface::write_ticks()
83{
84
85 string tickline = "\rmonotone: ";
86 for (map<string,size_t>::const_iterator i = ticks.begin();
87 i != ticks.end(); ++i)
88 tickline += string("[") + i->first + ": " + lexical_cast<string>(i->second) + "] ";
89 tickline += tick_trailer;
90
91 size_t curr_sz = tickline.size();
92 if (curr_sz < last_tick_len)
93 tickline += string(last_tick_len - curr_sz, ' ');
94 last_tick_len = curr_sz;
95
96 clog << tickline;
97 clog.flush();
98 last_write_was_a_tick = true;
99}
100
101void user_interface::warn(string const & warning)
102{
103 if (issued_warnings.find(warning) == issued_warnings.end())
104 inform("warning: " + warning);
105 issued_warnings.insert(warning);
106}
107
108
109static inline string sanitize(string const & line)
110{
111 // FIXME: you might want to adjust this if you're using a charset
112 // which has safe values in the sub-0x20 range. ASCII, UTF-8,
113 // and most ISO8859-x sets do not.
114 string tmp;
115 tmp.reserve(line.size());
116 for (size_t i = 0; i < line.size(); ++i)
117 {
118 if ((line[i] == '\n')
119 || (line[i] >= static_cast<char>(0x20)
120 && line[i] != static_cast<char>(0x7F)))
121tmp += line[i];
122 else
123tmp += ' ';
124 }
125 return tmp;
126}
127
128void user_interface::inform(string const & line)
129{
130 if (last_write_was_a_tick)
131 clog << endl;
132 clog << "monotone: " << sanitize(line);
133 clog.flush();
134 last_write_was_a_tick = false;
135}

Archive Download this file

Branches

Tags

Quick Links:     www.monotone.ca    -     Downloads    -     Documentation    -     Wiki    -     Code Forge    -     Build Status