diff --git a/README.md b/README.md index 18ffa07..7c2e3cc 100644 --- a/README.md +++ b/README.md @@ -222,7 +222,14 @@ Shows CPU load averaged across all cores. `cpu = Barr::Blocks::CPU.new` -There are no `CPU` block specific configurable options. +| Option | Value | Description | Default | +| --- | --- | --- | --- | +| `format` | string | Configurable format for showing which weather information is displayed. See table below for options. | `"${LOAD}"` | + +| Option | Description | +| --- | --- | +| `${LOAD}` | Current load in % | +| `${TEMP}` | Current temperature | #### HDD diff --git a/examples/all_in.rb b/examples/all_in.rb index 702460f..f5a2492 100644 --- a/examples/all_in.rb +++ b/examples/all_in.rb @@ -39,7 +39,7 @@ icon: "\uf0c2 Philadelphia: ", interval: 1500) -cpu = Barr::Blocks::CPU.new icon: "\uf1fe" +cpu = Barr::Blocks::CPU.new icon: "\uf1fe", format: "${LOAD}% ${TEMP}" mem = Barr::Blocks::Mem.new bgcolor: '#333333' diff --git a/examples/i3_cpu_mem.rb b/examples/i3_cpu_mem.rb index 50ab18b..14997d6 100644 --- a/examples/i3_cpu_mem.rb +++ b/examples/i3_cpu_mem.rb @@ -7,7 +7,7 @@ i3 = Barr::Blocks::I3.new icon: "\uf108", bgcolor: '#114152', fgcolor: '#DAC1DE', align: :l, focus_markers: ["| \uf0a4",' |'], invert_focus_colors: true, interval: 0.2 -cpu = Barr::Blocks::CPU.new icon: "\uf108 CPU:", bgcolor: '#491A5E', align: :r +cpu = Barr::Blocks::CPU.new icon: "\uf108 CPU:", bgcolor: '#491A5E', align: :r, format: "${LOAD}" mem = Barr::Blocks::Mem.new icon: 'RAM:', align: :r, bgcolor: '#2F113D' diff --git a/lib/barr/blocks/cpu.rb b/lib/barr/blocks/cpu.rb index 5c5a54a..fb847eb 100644 --- a/lib/barr/blocks/cpu.rb +++ b/lib/barr/blocks/cpu.rb @@ -1,19 +1,31 @@ +# coding: utf-8 require 'barr/block' module Barr module Blocks class CPU < Block + def initialize opts={} + super + @format = opts[:format] || "${LOAD}" + end + def update! - idle = sys_cmd.scan(/(\d{1,3}\.\d) id/).flatten.first.to_f + op = {} + op[:load] = load_sys_cmd.to_f.round(2).to_s + "%" + op[:temp] = (temp_sys_cmd.to_f.round(2) / 1000).to_s + "°" - @output = "#{(100 - idle).round(1)}%" + @output = format_string_from_hash(op) end - + private - def sys_cmd - `top -bn1 | grep 'Cpu(s)'`.chomp + def load_sys_cmd + `grep 'cpu ' /proc/stat | awk -v RS="" '{print ($13-$2+$15-$4)*100/($13-$2+$15-$4+$16-$5)}'` + end + + def temp_sys_cmd + `cat /sys/class/thermal/thermal_zone0/temp` end end diff --git a/spec/blocks/cpu_spec.rb b/spec/blocks/cpu_spec.rb index 92111d0..0c66d26 100644 --- a/spec/blocks/cpu_spec.rb +++ b/spec/blocks/cpu_spec.rb @@ -1,17 +1,27 @@ +# coding: utf-8 require 'barr/blocks/cpu' RSpec.describe Barr::Blocks::CPU do describe '#update!' do - let(:sys_cmd) { '%Cpu(s): 7.9 us, 1.2 sy, 1.7 ni, 88.6 id, 0.5 wa' } + let(:load_sys_cmd) { '6.7744' } + let(:temp_sys_cmd) { '28500' } before do - allow(subject).to receive(:sys_cmd).and_return(sys_cmd) + allow(subject).to receive(:load_sys_cmd).and_return(load_sys_cmd) + allow(subject).to receive(:temp_sys_cmd).and_return(temp_sys_cmd) + end + + it 'sets the load data correctly' do + subject.format = '${LOAD}' subject.update! + expect(subject.output).to eq '6.77%' end - it 'sets the data correctly' do - expect(subject.output).to eq '11.4%' + it 'sets the temp data correctly' do + subject.format = '${TEMP}' + subject.update! + expect(subject.output).to eq '28.5°' end end