A variety of simple automation scripts and enhancements to Sway and i3.
You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

160 lines
5.2KB

  1. #!/usr/bin/perl
  2. my $output;
  3. my ($bar, $pretty) = (0, 0);
  4. if ( defined($ARGV[0]) && $ARGV[0] ne '-b' && $ARGV[0] ne '--bar' && $ARGV[0] ne '-p' && $ARGV[0] ne '--pretty' ) {
  5. print'
  6. pow.pl - Power Status Script
  7. Usage: pow.pl
  8. Prints information about all power devices as JSON.
  9. -p --pretty Display as human-readable
  10. -b --bar Waybar simplified output
  11. -h --help Display help. This is the only option.
  12. ';
  13. exit();
  14. } elsif ($ARGV[0] eq '-b' || $ARGV[0] eq '--bar') {
  15. $bar = 1;
  16. } elsif ($ARGV[0] eq '-p' || $ARGV[0] eq '--pretty') {
  17. $pretty = 1;
  18. }
  19. my %battery_total = (
  20. 'current' => 0,
  21. 'max' => 0,
  22. 'percentage' => 0
  23. );
  24. my @devices = <"/sys/class/power_supply/*">;
  25. $output .= "{";
  26. foreach (@devices) {
  27. my $path = $_;
  28. my $name = $_;
  29. $name =~ s/.*\///;
  30. $output .= '"' . $name . '":{';
  31. open(my $t,'<',"$_/type");
  32. my $type = <$t>;
  33. chomp $type;
  34. close $t;
  35. $output .= '"Type":"' . $type . '"';
  36. if ($name =~ /BAT[0-9]+/) {
  37. open(my $s,'<',"$_/status");
  38. my $status = <$s>;
  39. chomp $status;
  40. $output .= ',"Status":"' . $status . '"';
  41. } else {
  42. open(my $s,'<',"$_/online");
  43. my $status = <$s>;
  44. chomp $status;
  45. if ($status) {
  46. $status = "Plugged-In";
  47. } else {
  48. $status = "Unplugged";
  49. }
  50. $output .= ',"Status":"' . $status . '"';
  51. }
  52. close $s;
  53. if ($name =~ /BAT[0-9]+/) {
  54. open(my $m,'<',"$_/energy_full");
  55. my $max = <$m>;
  56. close $m;
  57. chomp $max;
  58. $battery_total{'max'} += $max;
  59. open(my $c,'<',"$_/energy_now");
  60. my $current = <$c>;
  61. chomp $current;
  62. #$current =~ s/\n//;
  63. $battery_total{'current'} += $current;
  64. close $c;
  65. $output .= ',"Current":"'
  66. . $current
  67. . '","Max":"'
  68. . $max
  69. . '","Percentage":"'
  70. . int($current/$max*100)
  71. . '"';
  72. }
  73. $output .= "},";
  74. }
  75. $battery_total{'percentage'} = sprintf("%0d",
  76. $battery_total{'current'} / $battery_total{'max'} * 100
  77. );
  78. $output .= '"Total":{"Current":"'
  79. . $battery_total{'current'}
  80. . '","Max":"'
  81. . $battery_total{'max'}
  82. . '","Percentage":"'
  83. . $battery_total{'percentage'}
  84. . '"}}';
  85. if ($bar) {
  86. use JSON::XS;
  87. my $json = JSON::XS->new();
  88. my $powref = $json->decode($output);
  89. my $icon;
  90. my $class = 'discharging';
  91. $output = '{"text": "' . $powref->{Total}->{Percentage} . '% ';
  92. if ($powref->{AC}->{Status} eq "Plugged-In") {
  93. $class = 'charging';
  94. $output .= "";
  95. } elsif ($powref->{Total}->{Percentage} le 10) {
  96. $class = 'critical';
  97. $output .= "";
  98. } elsif ($powref->{Total}->{Percentage} le 35) {
  99. $class = 'low';
  100. $output .= "";
  101. } elsif ($powref->{Total}->{Percentage} le 60) {
  102. $output .= "";
  103. } elsif ($powref->{Total}->{Percentage} le 85) {
  104. $output .= "";
  105. } else {
  106. $output .= "";
  107. }
  108. $output .= '", "tooltip": "' . $class . '", "class": "' . $class . '"}';
  109. } elsif ($pretty) {
  110. use JSON::XS;
  111. my $json = JSON::XS->new();
  112. my $powref = $json->decode($output);
  113. my $total = '';
  114. $output = "Device Status Percentage\n";
  115. foreach my $device (keys %{$powref}) {
  116. my $status;
  117. if ($powref->{$device}->{Status} eq "Plugged-In") {
  118. $status = "";
  119. } elsif (!defined($powref->{$device}->{Percentage}) || $powref->{$device}->{Percentage} == 0) {
  120. $status = " ";
  121. } elsif ($powref->{$device}->{Percentage} <= 10) {
  122. $status = " " . $powref->{$device}->{'Percentage'} . "%";
  123. } elsif ($powref->{$device}->{Percentage} <= 35) {
  124. $status = " " . $powref->{$device}->{'Percentage'} . "%";
  125. } elsif ($powref->{$device}->{Percentage} <= 60) {
  126. $status = " " . $powref->{$device}->{'Percentage'} . "%";
  127. } elsif ($powref->{$device}->{Percentage} <= 85) {
  128. $status = " " . $powref->{$device}->{'Percentage'} . "%";
  129. } else {
  130. $status = " " . $powref->{$device}->{'Percentage'} . "%";
  131. }
  132. if ($device eq 'Total') {
  133. $total .= sprintf("%-24s %-12s", $device, $status);
  134. } else {
  135. #$output .= sprintf("%".length($powref->{$device->{'Type'}})."s (%".length($powref->{$device->{'Type'}})."s): %3d\%\n", $device, $device->{'Type'}, $device->{'Percentage'});
  136. my $name = ' ('.$powref->{$device}->{'Type'}.')';
  137. if ((length($device)+length($name)) >= 24) {
  138. $name = substr($device, 0, (21-length($name))) . '...' . $name;
  139. } else {
  140. $name = substr(($device.$name), 0, 24);
  141. }
  142. $output .= sprintf("%-24s %-12s %-12s\n", $name, $powref->{$device}->{Status}, $status);
  143. }
  144. }
  145. $output .= $total;
  146. }
  147. print $output . "\n";
  148. exit();