~bzr-pqm/bzr/bzr.dev

« back to all changes in this revision

Viewing changes to bzrlib/ui/text.py

  • Committer: Matthäus G. Chajdas
  • Date: 2010-10-12 01:18:01 UTC
  • mto: (5484.1.1 trunk)
  • mto: This revision was merged to the branch mainline in revision 5485.
  • Revision ID: dev@anteru.net-20101012011801-thahmhfxdzz0j6d4
Remove spaces.

Show diffs side-by-side

added added

removed removed

Lines of Context:
60
60
        self.stderr = stderr
61
61
        # paints progress, network activity, etc
62
62
        self._progress_view = self.make_progress_view()
63
 
        
 
63
 
64
64
    def be_quiet(self, state):
65
65
        if state and not self._quiet:
66
66
            self.clear_term()
153
153
        """Construct and return a new ProgressView subclass for this UI.
154
154
        """
155
155
        # with --quiet, never any progress view
156
 
        # <https://bugs.edge.launchpad.net/bzr/+bug/320035>.  Otherwise if the
 
156
        # <https://bugs.launchpad.net/bzr/+bug/320035>.  Otherwise if the
157
157
        # user specifically requests either text or no progress bars, always
158
158
        # do that.  otherwise, guess based on $TERM and tty presence.
159
159
        if self.is_quiet():
229
229
 
230
230
    def show_warning(self, msg):
231
231
        self.clear_term()
 
232
        if isinstance(msg, unicode):
 
233
            te = osutils.get_terminal_encoding()
 
234
            msg = msg.encode(te, 'replace')
232
235
        self.stderr.write("bzr: warning: %s\n" % msg)
233
236
 
234
237
    def _progress_updated(self, task):
249
252
    def _progress_all_finished(self):
250
253
        self._progress_view.clear()
251
254
 
 
255
    def show_user_warning(self, warning_id, **message_args):
 
256
        """Show a text message to the user.
 
257
 
 
258
        Explicitly not for warnings about bzr apis, deprecations or internals.
 
259
        """
 
260
        # eventually trace.warning should migrate here, to avoid logging and
 
261
        # be easier to test; that has a lot of test fallout so for now just
 
262
        # new code can call this
 
263
        if warning_id not in self.suppressed_warnings:
 
264
            self.stderr.write(self.format_user_warning(warning_id, message_args) +
 
265
                '\n')
 
266
 
252
267
 
253
268
class TextProgressView(object):
254
269
    """Display of progress bar and other information on a tty.
285
300
        # correspond reliably to overall command progress
286
301
        self.enable_bar = False
287
302
 
 
303
    def _avail_width(self):
 
304
        # we need one extra space for terminals that wrap on last char
 
305
        w = osutils.terminal_width() 
 
306
        if w is None:
 
307
            return None
 
308
        else:
 
309
            return w - 1
 
310
 
288
311
    def _show_line(self, s):
289
312
        # sys.stderr.write("progress %r\n" % s)
290
 
        width = osutils.terminal_width()
 
313
        width = self._avail_width()
291
314
        if width is not None:
292
 
            # we need one extra space for terminals that wrap on last char
293
 
            width = width - 1
294
315
            s = '%-*.*s' % (width, width, s)
295
316
        self._term_file.write('\r' + s + '\r')
296
317
 
333
354
            return ''
334
355
 
335
356
    def _format_task(self, task):
 
357
        """Format task-specific parts of progress bar.
 
358
 
 
359
        :returns: (text_part, counter_part) both unicode strings.
 
360
        """
336
361
        if not task.show_count:
337
362
            s = ''
338
363
        elif task.current_cnt is not None and task.total_cnt is not None:
348
373
            t = t._parent_task
349
374
            if t.msg:
350
375
                m = t.msg + ':' + m
351
 
        return m + s
 
376
        return m, s
352
377
 
353
378
    def _render_line(self):
354
379
        bar_string = self._render_bar()
355
380
        if self._last_task:
356
 
            task_msg = self._format_task(self._last_task)
 
381
            task_part, counter_part = self._format_task(self._last_task)
357
382
        else:
358
 
            task_msg = ''
 
383
            task_part = counter_part = ''
359
384
        if self._last_task and not self._last_task.show_transport_activity:
360
385
            trans = ''
361
386
        else:
362
387
            trans = self._last_transport_msg
363
 
            if trans:
364
 
                trans += ' | '
365
 
        return (bar_string + trans + task_msg)
 
388
        # the bar separates the transport activity from the message, so even
 
389
        # if there's no bar or spinner, we must show something if both those
 
390
        # fields are present
 
391
        if (task_part or trans) and not bar_string:
 
392
            bar_string = '| '
 
393
        # preferentially truncate the task message if we don't have enough
 
394
        # space
 
395
        avail_width = self._avail_width()
 
396
        if avail_width is not None:
 
397
            # if terminal avail_width is unknown, don't truncate
 
398
            current_len = len(bar_string) + len(trans) + len(task_part) + len(counter_part)
 
399
            gap = current_len - avail_width
 
400
            if gap > 0:
 
401
                task_part = task_part[:-gap-2] + '..'
 
402
        s = trans + bar_string + task_part + counter_part
 
403
        if avail_width is not None:
 
404
            if len(s) < avail_width:
 
405
                s = s.ljust(avail_width)
 
406
            elif len(s) > avail_width:
 
407
                s = s[:avail_width]
 
408
        return s
366
409
 
367
410
    def _repaint(self):
368
411
        s = self._render_line()
424
467
            rate = (self._bytes_since_update
425
468
                    / (now - self._transport_update_time))
426
469
            # using base-10 units (see HACKING.txt).
427
 
            msg = ("%6dkB %5dkB/s" %
 
470
            msg = ("%6dkB %5dkB/s " %
428
471
                    (self._total_byte_count / 1000, int(rate) / 1000,))
429
472
            self._transport_update_time = now
430
473
            self._last_repaint = now