Test Failed
Push — develop ( faa4bd...66c9ff )
by Nicolas
04:00
created

glances/outputs/glances_bars.py (1 issue)

1
# -*- coding: utf-8 -*-
2
#
3
# This file is part of Glances.
4
#
5
# Copyright (C) 2019 Nicolargo <[email protected]>
6
#
7
# Glances is free software; you can redistribute it and/or modify
8
# it under the terms of the GNU Lesser General Public License as published by
9
# the Free Software Foundation, either version 3 of the License, or
10
# (at your option) any later version.
11
#
12
# Glances is distributed in the hope that it will be useful,
13
# but WITHOUT ANY WARRANTY; without even the implied warranty of
14
# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
15
# GNU Lesser General Public License for more details.
16
#
17
# You should have received a copy of the GNU Lesser General Public License
18
# along with this program. If not, see <http://www.gnu.org/licenses/>.
19
20
"""Manage bars for Glances output."""
21
22
from __future__ import division
23
24
from math import modf
25
26
27
class Bar(object):
0 ignored issues
show
Too many instance attributes (11/7)
Loading history...
28
29
    r"""Manage bar (progression or status).
30
31
    import sys
32
    import time
33
    b = Bar(10)
34
    for p in range(0, 100):
35
        b.percent = p
36
        print("\r%s" % b),
37
        time.sleep(0.1)
38
        sys.stdout.flush()
39
    """
40
41
    def __init__(self, size,
42
                 percentage_char='|', empty_char=' ',
43
                 pre_char='[', post_char=']',
44
                 with_text=True):
45
        # Build curses_bars
46
        self.__curses_bars = [empty_char] * 5 + [percentage_char] * 5
47
        # Bar size
48
        self.__size = size
49
        # Bar current percent
50
        self.__percent = 0
51
        # Min and max value
52
        self.min_value = 0
53
        self.max_value = 100
54
        # Char used for the decoration
55
        self.__pre_char = pre_char
56
        self.__post_char = post_char
57
        self.__empty_char = empty_char
58
        self.__with_text = with_text
59
60
    @property
61
    def size(self, with_decoration=False):
62
        # Return the bar size, with or without decoration
63
        if with_decoration:
64
            return self.__size
65
        if self.__with_text:
66
            return self.__size - 6
67
68
    @property
69
    def percent(self):
70
        return self.__percent
71
72
    @percent.setter
73
    def percent(self, value):
74
        if value <= self.min_value:
75
            value = self.min_value
76
        if value >= self.max_value:
77
            value = self.max_value
78
        self.__percent = value
79
80
    @property
81
    def pre_char(self):
82
        return self.__pre_char
83
84
    @property
85
    def post_char(self):
86
        return self.__post_char
87
88
    def get(self):
89
        """Return the bars."""
90
        frac, whole = modf(self.size * self.percent / 100.0)
91
        ret = self.__curses_bars[8] * int(whole)
92
        if frac > 0:
93
            ret += self.__curses_bars[int(frac * 8)]
94
            whole += 1
95
        ret += self.__empty_char * int(self.size - whole)
96
        if self.__with_text:
97
            ret = '{}{:5.1f}%'.format(ret, self.percent)
98
        return ret
99
100
    def __str__(self):
101
        """Return the bars."""
102
        return self.get()
103