-
Notifications
You must be signed in to change notification settings - Fork 4
/
Copy pathindex.jsx
76 lines (65 loc) · 1.59 KB
/
index.jsx
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
// Update every second for the clock. Expensive elements should
// throttle themselves
export const refreshFrequency = 5000 // ms
const USE_BASE_TEN = 10
const theme = {
borderSize: 10,
thickness: '2px',
green: '#97c475',
green_threshold: 80,
yellow: '#e5c07b',
yellow_threshold: 55,
orange: '#d09a6a',
orange_threshold: 30,
red: '#e06c75',
screenSize: window.innerWidth
}
const computeUsedBattery = usedPercentage => {
const paddingPercent = (100 - usedPercentage)/2
return theme.screenSize * (paddingPercent / 100)
}
const computeBatteryColor = level => {
const {
green,
green_threshold,
yellow,
yellow_threshold,
orange,
orange_threshold,
red
} = theme
if (level > green_threshold)
return green
if (level > yellow_threshold)
return yellow
if (level > orange_threshold)
return orange
return theme.red
}
const getBarStyle = (batteryPercentage) => {
const height = theme.thickness
const background = computeBatteryColor(batteryPercentage)
const borderSize = theme.borderSize + computeUsedBattery(batteryPercentage)
return {
top: 25,
right: borderSize,
left: borderSize,
position: 'fixed',
background,
overflow: 'hidden',
height,
}
}
export const command = `pmset -g batt | egrep '(\\d+)\%' -o | cut -f1 -d%`
export const render = ({ output, error }) => {
const batteryPercentage = parseInt(output, USE_BASE_TEN)
if(error) {
console.log(new Date())
console.log(error)
console.log(String(error))
}
const barStyle = getBarStyle(batteryPercentage)
return (
<div style={barStyle}/>
)
}