├── README.md ├── Screenshots └── power_consumption.png ├── extension.js ├── metadata.json └── stylesheet.css /README.md: -------------------------------------------------------------------------------- 1 | # Power Consumption Extension 2 | 3 | Having a laptop with not much battery and a backlit keyboard made me wonder how much power it was spending at each moment. Being able to gather that information from the system and putting it into a shell script was the first thing to do, but as I use Gnome on my daily basis, I just created this small extension to show up the power consumption. 4 | 5 | Currently it refreshes each second 6 | 7 | ## Wishlist: 8 | 9 | - Preferences to set the refresh rate 10 | - Detect more than one battery 11 | - Detect if there's no battery and show just a `-` 12 | - Select Battery to take power consumption from 13 | 14 | ## Changelog: 15 | 16 | ### 0.1 17 | 18 | - First Version, just showing power consumption -------------------------------------------------------------------------------- /Screenshots/power_consumption.png: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/ferdef/power_consumption/10e489d1a01c1a13f82f541b5153c773b439b6ad/Screenshots/power_consumption.png -------------------------------------------------------------------------------- /extension.js: -------------------------------------------------------------------------------- 1 | 'use strict'; 2 | 3 | // imports.gi.versions.Gtk = '3.0'; 4 | // imports.gi.versions.WebKit2 = '4.0'; 5 | 6 | const Gio = imports.gi.Gio; 7 | const GLib = imports.gi.GLib; 8 | const GObject = imports.gi.GObject; 9 | const Clutter = imports.gi.Clutter; 10 | const St = imports.gi.St; 11 | 12 | const ExtensionUtils = imports.misc.extensionUtils; 13 | const Me = ExtensionUtils.getCurrentExtension(); 14 | const Main = imports.ui.main; 15 | const PanelMenu = imports.ui.panelMenu; 16 | 17 | const Lang = imports.lang; 18 | const Mainloop = imports.mainloop; 19 | 20 | const Config = imports.misc.config; 21 | const SHELL_MINOR = parseInt(Config.PACKAGE_VERSION.split('.')[1]); 22 | 23 | var label = null; 24 | var indicator = null; 25 | 26 | var PowerConsumption = class PowerConsumption extends PanelMenu.Button { 27 | _init() { 28 | super._init(0.0, `${Me.metadata.name} Indicator`, false); 29 | 30 | label = new St.Label({ 31 | text: get_data(), 32 | y_align: Clutter.ActorAlign.CENTER, 33 | style_class: 'power_consumption_label' 34 | }); 35 | 36 | this.add_child(label); 37 | 38 | this._update(); 39 | } 40 | 41 | _update() { 42 | label.set_text(get_data()); 43 | Mainloop.timeout_add_seconds(1, Lang.bind(this, this._update)); 44 | } 45 | } 46 | 47 | if (SHELL_MINOR > 30) { 48 | PowerConsumption = GObject.registerClass( 49 | {GTypeName: 'PowerConsumption'}, 50 | PowerConsumption 51 | ); 52 | } 53 | 54 | function get_current_path() { 55 | return "/sys/class/power_supply/BAT0/current_now"; 56 | } 57 | 58 | function get_voltage_path() { 59 | return "/sys/class/power_supply/BAT0/voltage_now"; 60 | } 61 | 62 | function get_current() { 63 | var filepath = get_current_path(); 64 | if(GLib.file_test (filepath, GLib.FileTest.EXISTS)) { 65 | return parseInt(GLib.file_get_contents(filepath)[1]); 66 | } 67 | 68 | return null; 69 | } 70 | 71 | function get_voltage() { 72 | var filepath = get_voltage_path(); 73 | if(GLib.file_test (filepath, GLib.FileTest.EXISTS)) { 74 | return parseInt(GLib.file_get_contents(filepath)[1]); 75 | } 76 | } 77 | 78 | function get_data() { 79 | var power_str = "N/A"; 80 | var current = get_current(); 81 | var voltage = get_voltage(); 82 | 83 | if (current && voltage) { 84 | var raw_power = (current * voltage) / 1000000000000; 85 | 86 | var power = (Math.round(raw_power * 100) / 100).toFixed(2); 87 | 88 | power_str = `${String(power)} W`; 89 | } 90 | 91 | return(power_str); 92 | } 93 | 94 | function init() { 95 | log(`initializing ${Me.metadata.name} version ${Me.metadata.version}`); 96 | } 97 | 98 | function enable() { 99 | 100 | indicator = new PowerConsumption(); 101 | 102 | log(`Enabling ${Me.metadata.name} version ${Me.metadata.version}`); 103 | 104 | Main.panel.addToStatusArea(`${Me.metadata.name}`, indicator); 105 | } 106 | 107 | function disable() { 108 | log(`Disabling ${Me.metadata.name} version ${Me.metadata.version}`); 109 | 110 | if (indicator !== null) { 111 | indicator.destroy(); 112 | indicator = null; 113 | } 114 | } 115 | -------------------------------------------------------------------------------- /metadata.json: -------------------------------------------------------------------------------- 1 | { 2 | "name": "Power Consumption", 3 | "description": "Show power consumption in a laptop", 4 | "uuid": "power_consumption@f3rd3f", 5 | "shell-version": [ 6 | "3.34" 7 | ], 8 | "version": 0.1, 9 | "url": "https://github.com/ferdef/power_consumption" 10 | } 11 | -------------------------------------------------------------------------------- /stylesheet.css: -------------------------------------------------------------------------------- 1 | .power_consumption_label { 2 | font-size: smaller; 3 | } --------------------------------------------------------------------------------