glances_bars.py 2.8 KB
Newer Older
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22
# -*- coding: utf-8 -*-
#
# This file is part of Glances.
#
# Copyright (C) 2015 Nicolargo <nicolas@nicolargo.com>
#
# Glances is free software; you can redistribute it and/or modify
# it under the terms of the GNU Lesser General Public License as published by
# the Free Software Foundation, either version 3 of the License, or
# (at your option) any later version.
#
# Glances 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 Lesser General Public License for more details.
#
# You should have received a copy of the GNU Lesser General Public License
# along with this program. If not, see <http://www.gnu.org/licenses/>.

"""Manage bars for Glances output."""

# Import system lib
23
import locale
A
flake8  
Alessio Sergi 已提交
24
from math import modf
25 26 27


class Bar(object):
A
PEP 257  
Alessio Sergi 已提交
28 29

    r"""Manage bar (progression or status).
30 31 32 33 34

    import sys
    import time
    b = Bar(10)
    for p in range(0, 100):
35
        b.percent = p
36 37 38 39 40
        print("\r%s" % b),
        time.sleep(0.1)
        sys.stdout.flush()
    """

N
Nicolargo 已提交
41 42 43
    def __init__(self, size,
                 pre_char='[',
                 post_char=']',
N
nicolargo 已提交
44 45
                 empty_char='_',
                 with_text=True):
46 47 48 49
        # Bar size
        self.__size = size
        # Bar current percent
        self.__percent = 0
N
Nicolargo 已提交
50 51 52 53
        # Char used for the decoration
        self.__pre_char = pre_char
        self.__post_char = post_char
        self.__empty_char = empty_char
N
nicolargo 已提交
54
        self.__with_text = with_text
55 56 57 58 59 60 61 62 63
        # Char used for the bar
        self.curses_bars = self.__get_curses_bars()

    def __get_curses_bars(self):
        # Return the chars used to display the bar
        if locale.getdefaultlocale()[1] == 'UTF-8':
            return [" ", "▏", "▎", "▍", "▌", "▋", "▊", "▉", "█"]
        else:
            return [" ", " ", " ", " ", "|", "|", "|", "|", "|"]
64

65 66
    @property
    def size(self, with_decoration=False):
N
nicolargo 已提交
67 68 69 70 71
        # Return the bar size, with or without decoration
        if with_decoration:
            return self.__size
        if self.__with_text:
            return self.__size - 6
72

73 74 75
    # @size.setter
    # def size(self, value):
    #     self.__size = value
76

77 78
    @property
    def percent(self):
79 80
        return self.__percent

81 82 83 84 85
    @percent.setter
    def percent(self, value):
        assert value >= 0
        assert value <= 100
        self.__percent = value
86 87

    def __str__(self):
88 89
        """Return the bars."""
        frac, whole = modf(self.size * self.percent / 100.0)
90
        ret = self.curses_bars[8] * int(whole)
91
        if frac > 0:
92
            ret += self.curses_bars[int(frac * 8)]
93
            whole += 1
94
        ret += self.__empty_char * int(self.size - whole)
N
nicolargo 已提交
95
        if self.__with_text:
96
            ret = '{0}{1:>5}%'.format(ret, self.percent)
N
Nicolargo 已提交
97
        return self.__pre_char + ret + self.__post_char