summaryrefslogtreecommitdiffstats
diff options
context:
space:
mode:
authornicolargo <nicolas@nicolargo.com>2024-01-28 16:06:56 +0100
committernicolargo <nicolas@nicolargo.com>2024-01-28 16:06:56 +0100
commitab5bf02e6b29ea398b187517d5da1670fab8aa51 (patch)
tree1ef5aa54fb14a202b74289b89470a947c5089f4b
parent7f2e8525b46f60cbaaa23d1f6b6e932096e964a6 (diff)
Add unittest for Bar
-rw-r--r--glances/outputs/glances_bars.py35
-rw-r--r--glances/plugins/mem/__init__.py1
-rw-r--r--glances/plugins/plugin/model.py8
-rw-r--r--glances/plugins/quicklook/__init__.py6
-rwxr-xr-xunitest.py21
5 files changed, 52 insertions, 19 deletions
diff --git a/glances/outputs/glances_bars.py b/glances/outputs/glances_bars.py
index 171099bf..632efed9 100644
--- a/glances/outputs/glances_bars.py
+++ b/glances/outputs/glances_bars.py
@@ -34,6 +34,19 @@ class Bar(object):
unit_char='%',
display_value=True,
min_value=0, max_value=100):
+ """Init a bar (used in Quicllook plugin)
+
+ Args:
+ size (_type_): Bar size
+ bar_char (str, optional): Bar character. Defaults to '|'.
+ empty_char (str, optional): Empty character. Defaults to ' '.
+ pre_char (str, optional): Display this char before the bar. Defaults to '['.
+ post_char (str, optional): Display this char after the bar. Defaults to ']'.
+ unit_char (str, optional): Unit char to be displayed. Defaults to '%'.
+ display_value (bool, optional): Do i need to display the value. Defaults to True.
+ min_value (int, optional): Minimum value. Defaults to 0.
+ max_value (int, optional): Maximum value (percent can be higher). Defaults to 100.
+ """
# Build curses_bars
self.__curses_bars = [empty_char] * 5 + [bar_char] * 5
# Bar size
@@ -77,26 +90,34 @@ class Bar(object):
def post_char(self):
return self.__post_char
- def get(self, overwrite=''):
+ def get(self, overlay: str = None):
"""Return the bars."""
value = self.max_value if self.percent > self.max_value else self.percent
+
+ # Build the bar
frac, whole = modf(self.size * value / 100.0)
ret = self.__curses_bars[8] * int(whole)
if frac > 0:
ret += self.__curses_bars[int(frac * 8)]
whole += 1
ret += self.__empty_char * int(self.size - whole)
+
+ # Add the value
if self.__display_value:
- if self.percent > self.max_value:
- ret = '{}>{:4.0f}{}'.format(ret,
- self.max_value,
- self.__unit_char)
+ if self.percent >= self.max_value:
+ ret = '{} {}{:3.0f}{}'.format(ret,
+ '>' if self.percent > self.max_value else ' ',
+ self.max_value,
+ self.__unit_char)
else:
ret = '{}{:5.1f}{}'.format(ret,
self.percent,
self.__unit_char)
- if overwrite and len(overwrite) < len(ret) - 6:
- ret = overwrite + ret[len(overwrite):]
+
+ # Add overlay
+ if overlay and len(overlay) < len(ret) - 6:
+ ret = overlay + ret[len(overlay):]
+
return ret
def __str__(self):
diff --git a/glances/plugins/mem/__init__.py b/glances/plugins/mem/__init__.py
index 52ceed0c..b44181b1 100644
--- a/glances/plugins/mem/__init__.py
+++ b/glances/plugins/mem/__init__.py
@@ -11,6 +11,7 @@
from glances.globals import iterkeys
from glances.plugins.plugin.model import GlancesPluginModel
+from glances.logger import logger
import psutil
diff --git a/glances/plugins/plugin/model.py b/glances/plugins/plugin/model.py
index cba16dac..b75390e1 100644
--- a/glances/plugins/plugin/model.py
+++ b/glances/plugins/plugin/model.py
@@ -777,8 +777,12 @@ class GlancesPluginModel(object):
else:
return stat_name + '_' + criticality in self._limits
- def get_limit(self, criticality, stat_name=""):
- """Return the limit value for the alert."""
+ def get_limit(self, criticality=None, stat_name=""):
+ """Return the limit value for the given criticality.
+ If criticality is None, return the dict of all the limits."""
+ if criticality is None:
+ return self._limits
+
# Get the limit for stat + header
# Example: network_wlan0_rx_careful
try:
diff --git a/glances/plugins/quicklook/__init__.py b/glances/plugins/quicklook/__init__.py
index 5fc46ac2..2d0e8e70 100644
--- a/glances/plugins/quicklook/__init__.py
+++ b/glances/plugins/quicklook/__init__.py
@@ -238,14 +238,10 @@ class PluginModel(GlancesPluginModel):
def _msg_create_line(self, msg, data, key):
"""Create a new line to the Quick view."""
- # if key == 'mem' and self.get_alert(self.stats['swap'], header='swap') != 'DEFAULT':
- # overwrite = 'SWAP'
- # else:
- overwrite = ''
return [
self.curse_add_line(msg),
self.curse_add_line(data.pre_char, decoration='BOLD'),
- self.curse_add_line(data.get(overwrite), self.get_views(key=key, option='decoration')),
+ self.curse_add_line(data.get(), self.get_views(key=key, option='decoration')),
self.curse_add_line(data.post_char, decoration='BOLD'),
self.curse_add_line(' '),
]
diff --git a/unitest.py b/unitest.py
index eb35b13d..a83c32b2 100755
--- a/unitest.py
+++ b/unitest.py
@@ -371,7 +371,7 @@ class TestGlances(unittest.TestCase):
self.assertEqual(len(h.get()), 2)
self.assertEqual(len(h.get()['a']), 0)
- def test_099_output_bars_must_be_between_0_and_100_percent(self):
+ def test_099_output_bars(self):
"""Test quick look plugin.
> bar.min_value
@@ -381,16 +381,27 @@ class TestGlances(unittest.TestCase):
> bar.percent = -1
> bar.percent
0
- > bar.percent = 101
- > bar.percent
- 100
"""
print('INFO: [TEST_099] Test progress bar')
+
bar = Bar(size=1)
+ # Percent value can not be lower than min_value
bar.percent = -1
self.assertLessEqual(bar.percent, bar.min_value)
+ # but... percent value can be higher than max_value
bar.percent = 101
- self.assertGreaterEqual(bar.percent, bar.max_value)
+ self.assertLessEqual(bar.percent, 101)
+
+ # Test display
+ bar = Bar(size=50)
+ bar.percent = 0
+ self.assertEqual(bar.get(), ' 0.0%')
+ bar.percent = 70
+ self.assertEqual(bar.get(), '||||||||||||||||||||||||||||||| 70.0%')
+ bar.percent = 100
+ self.assertEqual(bar.get(), '|||||||||||||||||||||||||||||||||||||||||||| 100%')
+ bar.percent = 110
+ self.assertEqual(bar.get(), '|||||||||||||||||||||||||||||||||||||||||||| >100%')
def test_100_secure(self):
"""Test secure functions"""