Test Failed
Push — master ( ee826a...d9056e )
by Nicolas
03:09
created

glances.outputs.glances_bars.Bar.__init__()   A

Complexity

Conditions 1

Size

Total Lines 41
Code Lines 21

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
cc 1
eloc 21
nop 10
dl 0
loc 41
rs 9.376
c 0
b 0
f 0

How to fix   Many Parameters   

Many Parameters

Methods with many parameters are not only hard to understand, but their parameters also often become inconsistent when you need more, or different data.

There are several approaches to avoid long parameter lists:

1
# -*- coding: utf-8 -*-
2
#
3
# This file is part of Glances.
4
#
5
# SPDX-FileCopyrightText: 2022 Nicolas Hennion <[email protected]>
6
#
7
# SPDX-License-Identifier: LGPL-3.0-only
8
#
9
10
"""Manage bars for Glances output."""
11
12
from __future__ import division
13
14
from math import modf
15
16
17
class Bar(object):
18
    """Manage bar (progression or status).
19
20
    import sys
21
    import time
22
    b = Bar(10)
23
    for p in range(0, 100):
24
        b.percent = p
25
        print("\r%s" % b),
26
        time.sleep(0.1)
27
        sys.stdout.flush()
28
    """
29
30
    def __init__(
31
        self,
32
        size,
33
        bar_char='|',
34
        empty_char=' ',
35
        pre_char='[',
36
        post_char=']',
37
        unit_char='%',
38
        display_value=True,
39
        min_value=0,
40
        max_value=100,
41
    ):
42
        """Init a bar (used in Quicllook plugin)
43
44
        Args:
45
            size (_type_): Bar size
46
            bar_char (str, optional): Bar character. Defaults to '|'.
47
            empty_char (str, optional): Empty character. Defaults to ' '.
48
            pre_char (str, optional): Display this char before the bar. Defaults to '['.
49
            post_char (str, optional): Display this char after the bar. Defaults to ']'.
50
            unit_char (str, optional): Unit char to be displayed. Defaults to '%'.
51
            display_value (bool, optional): Do i need to display the value. Defaults to True.
52
            min_value (int, optional): Minimum value. Defaults to 0.
53
            max_value (int, optional): Maximum value (percent can be higher). Defaults to 100.
54
        """
55
        # Build curses_bars
56
        self.__curses_bars = [empty_char] * 5 + [bar_char] * 5
57
        # Bar size
58
        self.__size = size
59
        # Bar current percent
60
        self.__percent = 0
61
        # Min and max value
62
        self.min_value = min_value
63
        self.max_value = max_value
64
        # Char used for the decoration
65
        self.__pre_char = pre_char
66
        self.__post_char = post_char
67
        self.__empty_char = empty_char
68
        self.__unit_char = unit_char
69
        # Value should be displayed ?
70
        self.__display_value = display_value
71
72
    @property
73
    def size(self, with_decoration=False):
74
        # Return the bar size, with or without decoration
75
        if with_decoration:
76
            return self.__size
77
        if self.__display_value:
78
            return self.__size - 6
79
80
    @property
81
    def percent(self):
82
        return self.__percent
83
84
    @percent.setter
85
    def percent(self, value):
86
        if value < self.min_value:
87
            value = self.min_value
88
        self.__percent = value
89
90
    @property
91
    def pre_char(self):
92
        return self.__pre_char
93
94
    @property
95
    def post_char(self):
96
        return self.__post_char
97
98
    def get(self, overlay: str = None):
99
        """Return the bars."""
100
        value = self.max_value if self.percent > self.max_value else self.percent
101
102
        # Build the bar
103
        frac, whole = modf(self.size * value / 100.0)
104
        ret = self.__curses_bars[8] * int(whole)
105
        if frac > 0:
106
            ret += self.__curses_bars[int(frac * 8)]
107
            whole += 1
108
        ret += self.__empty_char * int(self.size - whole)
109
110
        # Add the value
111
        if self.__display_value:
112
            if self.percent >= self.max_value:
113
                ret = '{} {}{:3.0f}{}'.format(
114
                    ret, '>' if self.percent > self.max_value else ' ', self.max_value, self.__unit_char
115
                )
116
            else:
117
                ret = '{}{:5.1f}{}'.format(ret, self.percent, self.__unit_char)
118
119
        # Add overlay
120
        if overlay and len(overlay) < len(ret) - 6:
121
            ret = overlay + ret[len(overlay) :]
122
123
        return ret
124
125
    def __str__(self):
126
        """Return the bars."""
127
        return self.get()
128