Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Added format options to CPU block #35

Merged
merged 1 commit into from
Oct 28, 2016
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
9 changes: 8 additions & 1 deletion README.md
Original file line number Diff line number Diff line change
Expand Up @@ -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

Expand Down
2 changes: 1 addition & 1 deletion examples/all_in.rb
Original file line number Diff line number Diff line change
Expand Up @@ -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'

Expand Down
2 changes: 1 addition & 1 deletion examples/i3_cpu_mem.rb
Original file line number Diff line number Diff line change
Expand Up @@ -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'

Expand Down
22 changes: 17 additions & 5 deletions lib/barr/blocks/cpu.rb
Original file line number Diff line number Diff line change
@@ -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
Expand Down
18 changes: 14 additions & 4 deletions spec/blocks/cpu_spec.rb
Original file line number Diff line number Diff line change
@@ -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

Expand Down