summaryrefslogtreecommitdiff
path: root/src/cmdline/text_progress.cc
blob: 15a98a6ce47aeb1dcd3c052519af5a39c140caab (plain)
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
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
/** \file text_progress.cc */


// Copyright (C) 2010 Daniel Burrows
//
// This program is free software; you can redistribute it and/or
// modify it under the terms of the GNU General Public License as
// published by the Free Software Foundation; either version 2 of the
// License, or (at your option) any later version.
//
// This program is distributed in the hope that it will be useful, but
// WITHOUT ANY WARRANTY; without even the implied warranty of
// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
// General Public License for more details.
//
// You should have received a copy of the GNU General Public License
// along with this program; see the file COPYING.  If not, write to
// the Free Software Foundation, Inc., 59 Temple Place - Suite 330,
// Boston, MA 02111-1307, USA.

// Local includes:
#include "text_progress.h"

#include "cmdline_progress_display.h"

#include <aptitude.h>

#include <generic/apt/apt.h>
#include <generic/apt/config_signal.h>
#include <generic/util/progress_info.h>
#include <generic/views/progress.h>


// System includes:
#include <apt-pkg/error.h>

#include <boost/format.hpp>
#include <boost/make_shared.hpp>

#include <cwidget/generic/util/transcode.h>

#include <iostream>

namespace cw = cwidget;

using aptitude::util::progress_info;
using boost::format;
using boost::wformat;
using cwidget::util::transcode;

namespace aptitude
{
  namespace cmdline
  {
    namespace
    {
      class text_progress : public OpProgress
      {
        // If true, this code will assume it has a TTY as its output
        // and use terminal-based trickery.
        bool use_tty_decorations;

        // The last operation we were displaying; used to output an
        // indicator when the operation finishes.
        std::string last_op;

        boost::shared_ptr<views::progress> display;

      public:
        text_progress(bool _use_tty_decorations,
                      const boost::shared_ptr<views::progress> &_display)
          : use_tty_decorations(_use_tty_decorations),
            display(_display)
        {
        }

        void Done();
        void Update();
      };

      void text_progress::Done()
      {
        // If we displayed a progress indicator, finish it off.
        if(use_tty_decorations)
          {
            if(!last_op.empty())
              {
                display->done();

                if(_error->PendingError() == true)
                  // TRANSLATORS: the text between [] should be
                  // exactly 4 character cells wide; "ERR" is short
                  // for "ERROR".
                  std::cout << (format(_("[ ERR] %s")) % last_op) << std::endl;
              }
          }
        else if(!last_op.empty())
          {
            std::cout << std::endl;
            last_op.clear();
          }
      }

      void text_progress::Update()
      {
        if(CheckChange(0.7))
          {
            if(!use_tty_decorations)
              {
                if(MajorChange)
                  {
                    if(!last_op.empty())
                      std::cout << std::endl;

                    std::cout << Op << "...";
                    last_op = Op;
                  }
              }
            else
              {
                progress_info info = progress_info::bar(Percent, Op);
                display->set_progress(info);

                last_op = Op;
              }
          }
      }
    }

    boost::shared_ptr<OpProgress>
    make_text_progress(bool require_tty_decorations,
                       const boost::shared_ptr<views::progress> &display)
    {
      bool hide_tty_decorations = false;
      bool hidden = false;

      if(!isatty(1) ||
         aptcfg->FindI("quiet", 0) >= 1 ||
         aptcfg->FindB("quiet::NoUpdate", false) == true)
        hide_tty_decorations = true;

      if(aptcfg->FindI("quiet", 0) >= 2)
        hidden = true;

      if(require_tty_decorations && hide_tty_decorations)
        hidden = true;

      if(hidden)
        return boost::make_shared<OpProgress>();
      else
        return boost::make_shared<text_progress>(!hide_tty_decorations, display);
    }

    boost::shared_ptr<OpProgress>
    make_text_progress(bool require_tty_decorations,
                       const boost::shared_ptr<terminal_locale> &term_locale,
                       const boost::shared_ptr<terminal_metrics> &term_metrics,
                       const boost::shared_ptr<terminal_output> &term_output)
    {
      return make_text_progress(require_tty_decorations,
                                create_progress_display(term_locale, term_metrics, term_output));
    }
  }
}