Including battery and AC adapter health in Glances #1049

This commit is contained in:
nicolargo 2022-01-23 10:42:43 +01:00
parent 19eea2a0c1
commit f9697e8b8e
6 changed files with 83 additions and 8 deletions

View File

@ -451,6 +451,13 @@ Examples of use:
dest='sparkline',
help='display sparklines instead of bar in the curses interface',
)
parser.add_argument(
'--disable-unicode',
action='store_true',
default=False,
dest='disable_unicode',
help='disable unicode characters in the curses interface',
)
parser.add_argument(
'--theme-white',
action='store_true',

View File

@ -0,0 +1,37 @@
# -*- coding: utf-8 -*-
#
# This file is part of Glances.
#
# Copyright (C) 2022 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 unicode message for Glances output."""
_unicode_message = {
'ARROW_LEFT': [u'\u2190', u'<'],
'ARROW_RIGHT': [u'\u2192', u'>'],
'ARROW_UP': [u'\u2191', u'^'],
'ARROW_DOWN': [u'\u2193', u'v'],
'CHECK': [u'\u2713', u''],
'PROCESS_SELECTOR': [u'>', u'>'],
}
def unicode_message(key, args=None):
"""Return the unicode message for the given key."""
if args and hasattr(args, 'disable_unicode') and args.disable_unicode:
return _unicode_message[key][1]
else:
return _unicode_message[key][0]

View File

@ -28,13 +28,14 @@ import json
import copy
from operator import itemgetter
from glances.compat import iterkeys, itervalues, listkeys, map, mean, nativestr
from glances.compat import iterkeys, itervalues, listkeys, map, mean, nativestr, u
from glances.actions import GlancesActions
from glances.history import GlancesHistory
from glances.logger import logger
from glances.events import glances_events
from glances.thresholds import glances_thresholds
from glances.timer import Counter, Timer
from glances.outputs.glances_unicode import unicode_message
fields_unit_short = {'percent': '%'}
@ -1137,9 +1138,9 @@ class GlancesPlugin(object):
if trend is None:
ret = ' '
elif trend > significant:
ret = '/'
ret = unicode_message('ARROW_UP', self.args)
elif trend < -significant:
ret = '\\'
ret = unicode_message('ARROW_DOWN', self.args)
return ret
def _check_decorator(fct):

View File

@ -26,6 +26,7 @@ from glances.logger import logger
from glances.globals import WINDOWS
from glances.compat import key_exist_value_not_none_not_v
from glances.processes import glances_processes, sort_stats
from glances.outputs.glances_unicode import unicode_message
from glances.plugins.glances_core import Plugin as CorePlugin
from glances.plugins.glances_plugin import GlancesPlugin
@ -363,7 +364,8 @@ class Plugin(GlancesPlugin):
# * display a special character at the beginning of the line
# * underline the command name
if args.is_standalone:
ret.append(self.curse_add_line('>' if selected else ' ', 'SELECTED'))
ret.append(self.curse_add_line(unicode_message('PROCESS_SELECTOR') if selected else ' ',
'SELECTED'))
# CPU
ret.append(self._get_process_curses_cpu(p, selected, args))

View File

@ -27,6 +27,7 @@ from glances.compat import iteritems, to_fahrenheit
from glances.timer import Counter
from glances.plugins.sensors.glances_batpercent import Plugin as BatPercentPlugin
from glances.plugins.sensors.glances_hddtemp import Plugin as HddTempPlugin
from glances.outputs.glances_unicode import unicode_message
from glances.plugins.glances_plugin import GlancesPlugin
SENSOR_TEMP_UNIT = 'C'
@ -187,6 +188,18 @@ class Plugin(GlancesPlugin):
# Set the alert in the view
self.views[i[self.get_key()]]['value']['decoration'] = alert
def battery_trend(self, stats):
"""Return the trend characterr for the battery"""
if 'status' not in stats:
return ''
if stats['status'].startswith('Charg'):
return unicode_message('ARROW_UP')
elif stats['status'].startswith('Discharg'):
return unicode_message('ARROW_DOWN')
elif stats['status'].startswith('Full'):
return unicode_message('CHECK')
return ''
def msg_curse(self, args=None, max_width=None):
"""Return the dict to display in the curse interface."""
# Init the return message
@ -213,19 +226,22 @@ class Plugin(GlancesPlugin):
msg = '{:{width}}'.format(i["label"][:name_max_width], width=name_max_width)
ret.append(self.curse_add_line(msg))
if i['value'] in (b'ERR', b'SLP', b'UNK', b'NOS'):
msg = '{:>13}'.format(i['value'])
msg = '{:>14}'.format(i['value'])
ret.append(
self.curse_add_line(msg, self.get_views(item=i[self.get_key()], key='value', option='decoration'))
)
else:
if args.fahrenheit and i['type'] != 'battery' and i['type'] != 'fan_speed':
trend = ''
value = to_fahrenheit(i['value'])
unit = 'F'
else:
trend = self.battery_trend(i)
value = i['value']
unit = i['unit']
try:
msg = '{:>13.0f}{}'.format(value, unit)
msg = '{:.0f}{}{}'.format(value, unit, trend)
msg = '{:>14}'.format(msg)
ret.append(
self.curse_add_line(
msg, self.get_views(item=i[self.get_key()], key='value', option='decoration')

View File

@ -119,12 +119,24 @@ class GlancesGrabBat(object):
# 'unit': '%'}]
for b in self.bat.stat:
self.bat_list.append(
{'label': 'BAT {}'.format(b.path.split('/')[-1]), 'value': b.capacity, 'unit': '%'}
{
'label': 'BAT {}'.format(b.path.split('/')[-1]),
'value': b.capacity,
'unit': '%',
'status': b.status
}
)
elif psutil_tag and hasattr(self.bat.sensors_battery(), 'percent'):
# Use psutil to grab the stats
# Give directly the battery percent
self.bat_list = [{'label': 'Battery', 'value': int(self.bat.sensors_battery().percent), 'unit': '%'}]
self.bat_list = [
{
'label': 'Battery',
'value': int(self.bat.sensors_battery().percent),
'unit': '%',
'status': 'Charging' if self.bat.sensors_battery().power_plugged else 'Discharging'
}
]
def get(self):
"""Get the stats."""