|
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
|
|
|
curses_bars = [' ', ' ', ' ', ' ', '|', '|', '|', '|', '|'] |
|
|
|
|
|
|
27
|
|
|
|
|
28
|
|
|
|
|
29
|
|
|
class Bar(object): |
|
|
|
|
|
|
30
|
|
|
|
|
31
|
|
|
r"""Manage bar (progression or status). |
|
32
|
|
|
|
|
33
|
|
|
import sys |
|
34
|
|
|
import time |
|
35
|
|
|
b = Bar(10) |
|
36
|
|
|
for p in range(0, 100): |
|
37
|
|
|
b.percent = p |
|
38
|
|
|
print("\r%s" % b), |
|
39
|
|
|
time.sleep(0.1) |
|
40
|
|
|
sys.stdout.flush() |
|
41
|
|
|
""" |
|
42
|
|
|
|
|
43
|
|
|
def __init__(self, size, pre_char='[', post_char=']', empty_char=' ', with_text=True): |
|
|
|
|
|
|
44
|
|
|
# Bar size |
|
45
|
|
|
self.__size = size |
|
46
|
|
|
# Bar current percent |
|
47
|
|
|
self.__percent = 0 |
|
48
|
|
|
# Min and max value |
|
49
|
|
|
self.min_value = 0 |
|
50
|
|
|
self.max_value = 100 |
|
51
|
|
|
# Char used for the decoration |
|
52
|
|
|
self.__pre_char = pre_char |
|
53
|
|
|
self.__post_char = post_char |
|
54
|
|
|
self.__empty_char = empty_char |
|
55
|
|
|
self.__with_text = with_text |
|
56
|
|
|
|
|
57
|
|
|
@property |
|
58
|
|
|
def size(self, with_decoration=False): |
|
|
|
|
|
|
59
|
|
|
# Return the bar size, with or without decoration |
|
60
|
|
|
if with_decoration: |
|
61
|
|
|
return self.__size |
|
62
|
|
|
if self.__with_text: |
|
63
|
|
|
return self.__size - 6 |
|
64
|
|
|
|
|
65
|
|
|
@property |
|
66
|
|
|
def percent(self): |
|
|
|
|
|
|
67
|
|
|
return self.__percent |
|
68
|
|
|
|
|
69
|
|
|
@percent.setter |
|
70
|
|
|
def percent(self, value): |
|
71
|
|
|
if value <= self.min_value: |
|
72
|
|
|
value = self.min_value |
|
73
|
|
|
if value >= self.max_value: |
|
74
|
|
|
value = self.max_value |
|
75
|
|
|
self.__percent = value |
|
76
|
|
|
|
|
77
|
|
|
@property |
|
78
|
|
|
def pre_char(self): |
|
|
|
|
|
|
79
|
|
|
return self.__pre_char |
|
80
|
|
|
|
|
81
|
|
|
@property |
|
82
|
|
|
def post_char(self): |
|
|
|
|
|
|
83
|
|
|
return self.__post_char |
|
84
|
|
|
|
|
85
|
|
|
def get(self): |
|
86
|
|
|
"""Return the bars.""" |
|
87
|
|
|
frac, whole = modf(self.size * self.percent / 100.0) |
|
88
|
|
|
ret = curses_bars[8] * int(whole) |
|
89
|
|
|
if frac > 0: |
|
90
|
|
|
ret += curses_bars[int(frac * 8)] |
|
91
|
|
|
whole += 1 |
|
92
|
|
|
ret += self.__empty_char * int(self.size - whole) |
|
93
|
|
|
if self.__with_text: |
|
94
|
|
|
ret = '{}{:5.1f}%'.format(ret, self.percent) |
|
95
|
|
|
return ret |
|
96
|
|
|
|
|
97
|
|
|
def __str__(self): |
|
98
|
|
|
"""Return the bars.""" |
|
99
|
|
|
return self.get() |
|
100
|
|
|
|