]> ruderich.org/simon Gitweb - config/dotfiles.git/blob - bin/temperature.pl
shell/dircolors: support README.adoc
[config/dotfiles.git] / bin / temperature.pl
1 #!/usr/bin/perl
2
3 # Copyright (C) 2011-2012  Simon Ruderich
4 #
5 # This program is free software: you can redistribute it and/or modify
6 # it under the terms of the GNU General Public License as published by
7 # the Free Software Foundation, either version 3 of the License, or
8 # (at your option) any later version.
9 #
10 # This program is distributed in the hope that it will be useful,
11 # but WITHOUT ANY WARRANTY; without even the implied warranty of
12 # MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
13 # GNU General Public License for more details.
14 #
15 # You should have received a copy of the GNU General Public License
16 # along with this program.  If not, see <http://www.gnu.org/licenses/>.
17
18
19 use strict;
20 use warnings;
21
22
23 if (scalar @ARGV != 1 and (scalar @ARGV != 2 or not $ARGV[0] =~ /-[st]/)) {
24     print STDERR "Usage: $0 [-s | -t] <path/to/device>\n";
25     print STDERR "\n";
26     print STDERR "Normally this is /sys/devices/platform/*.\n";
27     exit 1;
28 }
29 if (scalar @ARGV == 1) {
30     @ARGV = ('', $ARGV[0]);
31 }
32
33 my $temperature_path = "$ARGV[1]/temp1_input";
34 my $critical_path    = "$ARGV[1]/temp1_crit";
35
36 # No temperature information available.
37 if (not -e $temperature_path or not -e $critical_path) {
38     print STDERR "'$temperature_path' or '$critical_path' not found.\n";
39     exit 1;
40 }
41
42 my $screen_mode = ($ARGV[0] eq '-s');
43 my $tmux_mode   = ($ARGV[0] eq '-t');
44
45 my $temperature;
46 my $critical;
47
48 my $file;
49 open $file, '<', $temperature_path or die $!;
50 $temperature = <$file>;
51 close $file or die $!;
52 open $file, '<', $critical_path or die $!;
53 $critical = <$file>;
54 close $file or die $!;
55
56 my $value = int($temperature / 1000);
57 my $risk  = ($critical - $temperature)/$critical;
58
59 $value .= "\xb0"; # degree symbol in UTF-8
60
61 # GNU screen mode with colors.
62 if ($screen_mode) {
63     my $color;
64     if ($risk < 0) {
65         $color = 'br r'; # bold reverse
66     } elsif ($risk < 0.1) {
67         $color = 'b r';
68     } elsif ($risk < 0.2) {
69         $color = 'b y';
70     } else {
71         $color = 'b g';
72     }
73
74     print "\005{+$color}$value\005{-}\n";
75
76 # Same in tmux mode.
77 } elsif ($tmux_mode) {
78     my $color;
79     my $style = 'bold';
80     if ($risk < 0) {
81         $color = 'red';
82         $style = 'reverse'; # blink doesn't work for me
83     } elsif ($risk < 0.1) {
84         $color = 'red';
85     } elsif ($risk < 0.2) {
86         $color = 'yellow';
87     } else {
88         $color = 'green';
89     }
90
91     print "#[fg=$color,$style]$value#[default]\n";
92
93 # Plain text output.
94 } else {
95     print "$value\n";
96 }