From 42789e07364cbb9826e27e928d8def51caae86fc Mon Sep 17 00:00:00 2001 From: Simon Vieille Date: Tue, 9 Mar 2021 15:10:39 +0100 Subject: [PATCH] move scripts to bin/, remove old i3status scripts --- i3_mouse_click.sh => bin/i3_mouse_click.sh | 0 i3_move_mouse.sh => bin/i3_move_mouse.sh | 0 .../i3_switch_focus_container.php | 0 .../i3_switch_monitor.sh | 0 .../i3_switch_monitor_wrapper.sh | 0 .../i3_switch_window.php | 0 .../i3_switch_workspace.php | 0 i3status.php | 442 ------------------ i3status.sh | 9 - i3status2.php | 376 --------------- i3status2.sh | 10 - 11 files changed, 837 deletions(-) rename i3_mouse_click.sh => bin/i3_mouse_click.sh (100%) rename i3_move_mouse.sh => bin/i3_move_mouse.sh (100%) rename i3_switch_focus_container.php => bin/i3_switch_focus_container.php (100%) rename i3_switch_monitor.sh => bin/i3_switch_monitor.sh (100%) rename i3_switch_monitor_wrapper.sh => bin/i3_switch_monitor_wrapper.sh (100%) rename i3_switch_window.php => bin/i3_switch_window.php (100%) rename i3_switch_workspace.php => bin/i3_switch_workspace.php (100%) delete mode 100755 i3status.php delete mode 100755 i3status.sh delete mode 100644 i3status2.php delete mode 100755 i3status2.sh diff --git a/i3_mouse_click.sh b/bin/i3_mouse_click.sh similarity index 100% rename from i3_mouse_click.sh rename to bin/i3_mouse_click.sh diff --git a/i3_move_mouse.sh b/bin/i3_move_mouse.sh similarity index 100% rename from i3_move_mouse.sh rename to bin/i3_move_mouse.sh diff --git a/i3_switch_focus_container.php b/bin/i3_switch_focus_container.php similarity index 100% rename from i3_switch_focus_container.php rename to bin/i3_switch_focus_container.php diff --git a/i3_switch_monitor.sh b/bin/i3_switch_monitor.sh similarity index 100% rename from i3_switch_monitor.sh rename to bin/i3_switch_monitor.sh diff --git a/i3_switch_monitor_wrapper.sh b/bin/i3_switch_monitor_wrapper.sh similarity index 100% rename from i3_switch_monitor_wrapper.sh rename to bin/i3_switch_monitor_wrapper.sh diff --git a/i3_switch_window.php b/bin/i3_switch_window.php similarity index 100% rename from i3_switch_window.php rename to bin/i3_switch_window.php diff --git a/i3_switch_workspace.php b/bin/i3_switch_workspace.php similarity index 100% rename from i3_switch_workspace.php rename to bin/i3_switch_workspace.php diff --git a/i3status.php b/i3status.php deleted file mode 100755 index 4ea407f..0000000 --- a/i3status.php +++ /dev/null @@ -1,442 +0,0 @@ -full_text = $full_text; - - $this->separator = $separator; - - if (null === $color) { - $color = self::NORMAL; - } - - $this->color = $color; - } - - public function setFullText($full_text) - { - $this->full_text = $full_text; - - return $this; - } - - public function getFullText() - { - return $this->full_text; - } - - public function setColor($color) - { - $this->color = $color; - - return $this; - } - - public function getColor() - { - return $this->color; - } - - public function setSeparator($separator) - { - $this->separator = $separator; - - return $this; - } - - public function getSeparator() - { - return $this->separator; - } -} - -class Element -{ - public $attributes = null; - - public function __construct() - { - $this->attributes = array(); - } - - public function setAttributes(array $attributes) - { - $this->attributes = $attributes; - - return $this; - } - - public function getAttributes() - { - return $this->attributes; - } - - public function addAttribute(Attribute $a) - { - $this->attributes[] = $a; - } -} - -class i3Bar -{ - protected $elements = null; - - public function __construct() - { - $this->protected = array(); - } - - public function setElements(array $elements) - { - $this->elements = $elements; - - return $this; - } - - public function getElements() - { - return $this->elements; - } - - public function addElement($e) - { - if (!empty($e)) { - $this->elements[] = $e; - } - - return $this; - } - - public function addElementIfNotEmtpy($e) - { - if (empty($e)) { - return $this; - } - - return $this->addElement($e); - } - - public function __toString() - { - $render = ',['; - - foreach ($this->getElements() as $element) { - $attributes = $element->getAttributes(); - - if (!is_array($attributes)) { - $attributes = array($attributes); - } - - if (empty($attributes)) { - continue; - } - - $attrs = array(); - - foreach ($attributes as $attr) { - $attr_render = '{'; - - if (!$attr->getSeparator()) { - $attr_render .= sprintf('"full_text": "%s",', $attr->getFullText()); - $attr_render .= '"separator": false,'; - $attr_render .= '"separator_block_width": 0,'; - } else { - $attr_render .= sprintf('"full_text": " %s ",', $attr->getFullText()); - $attr_render .= '"separator": true,'; - $attr_render .= '"separator_block_width": 15,'; - } - - $attr_render .= sprintf('"color": "%s"', $attr->getColor()); - $attr_render .= '}'; - - $attrs[] = $attr_render; - } - - $render .= count($element) > 1 ? '[' : ''; - $render .= implode(',', $attrs).','; - $render .= count($element) > 1 ? ']' : ''; - } - - $render .= ']'; - - $render = str_replace(',]', ']', $render)."\n"; - - return $render; - } -} - -/** - * IPS. - */ -function get_the_ips($ifaces = array()) -{ - $element = new Element(); - - foreach ($ifaces as $iface) { - preg_match('`inet ([^\s]+)`', shell_exec('ip addr show '.escapeshellcmd($iface)), $inet); - - if ($inet) { - $element->addAttribute(new Attribute('['.$iface.'] '.$inet[1], Attribute::INFO)); - } - } - - return $element; -} - -/** - * BANDWITH. - */ -function get_the_bandwith($ifaces = array()) -{ - $element = new Element(); - - foreach ($ifaces as $iface) { - preg_match('`inet ([^\s]+)`', shell_exec('ip addr show '.escapeshellcmd($iface)), $inet); - - if ($inet) { - $bwidth = explode(' ', preg_replace('/ +/', ' ', trim(shell_exec('ifstat -i '.escapeshellcmd($iface).' 1 1 | tail -n 1')))); - - //$dstats = trim(shell_exec('/sbin/ifconfig '.escapeshellcmd($iface).' | grep bytes | sed \'s/[()]//g\' | awk \'{print $3"Mo↓ "$7"Mo↑"}\'')); - //$ips[] = '['.$iface.'] '.$bwidth[0].'Ko/s↓ '.$bwidth[1].'Ko/s↑ ('.$dstats.')'; - - $element->addAttribute(new Attribute('['.$iface.'] '.$bwidth[0].'Ko/s↓ '.$bwidth[1].'Ko/s↑', Attribute::INFO)); - } - } - - return $element; -} - -/** - * SENSORS. - */ -function get_sensors() -{ - $sensors = array(); - - preg_match_all('/^([^:]+): +\+([0-9.]+)°C +\(high = \+([0-9.]+)°C, crit = \+([0-9.]+)°C\)/iU', shell_exec('sensors | grep "°C"'), $datas, PREG_SET_ORDER); - - var_dump($datas); - foreach ($datas as $data) { - } - - return $sensors; -} - -function get_sensor($item) -{ - get_sensors(); -} - -/** - * MOUNT. - */ -function get_the_mount_space_used($mount_point, $limits = array(70, 90)) -{ - $element = new Element(); - $mount_point = ' '.$mount_point.'$'; - $mount = shell_exec('df -h | egrep '.escapeshellcmd($mount_point).' | head -n 1'); - - if ($mount) { - list($dev, $size, $used, $free, $usedP, $point) = explode(' ', preg_replace('/\s+/', ' ', $mount)); - - $pourcent = intval($usedP); - - if ($pourcent < $limits[0]) { - $color = Attribute::NORMAL; - } elseif ($pourcent < $limits[1]) { - $color = Attribute::WARNING; - } else { - $color = Attribute::CRITICAL; - } - - $element->addAttribute(new Attribute('['.$point.'] '.$usedP, $color)); - } - - return $element; -} - -/** - * DATE. - */ -function get_the_date() -{ - $element = new Element(); - - $element->addAttribute(new Attribute(date('H:i:s'), Attribute::DATE)); - $element->addAttribute(new Attribute(date('d/m/Y'), Attribute::DATE)); - - return $element; -} - -/** - * ACPI. - */ -function get_the_acpi() -{ - $element = new Element(); - $acpi = shell_exec('acpi'); - - if (!preg_match('`Discharging`', $acpi)) { - preg_match('`([0-9]+%)`', $acpi, $values); - - if (!empty($values)) { - $charging = $values[1]; - - if ($charging < 20) { - $color = Attribute::CRITICAL; - } elseif ($charging < 50) { - $color = Attribute::WARNING; - } else { - $color = Attribute::NORMAL; - } - - $charging .= '↑'; - - $element->addAttribute(new Attribute($charging, $color)); - } - - return $element; - } - - preg_match('`([0-9]+%), ([0-9]+:[0-9]+)`', $acpi, $values); - - if ($values[1] < 20) { - $color = Attribute::CRITICAL; - } elseif ($values[1] < 50) { - $color = Attribute::WARNING; - } else { - $color = Attribute::INFO; - } - - $charging = $values[1].'↓'; - $times = str_replace(':', 'h', $values[2]); - - $element->addAttribute(new Attribute(implode(' ', array($charging, $times)), $color)); - - return $element; -} - -/** - * MUSIC. - */ -function get_the_master_volume() -{ - $element = new Element(); - $volume = shell_exec('amixer get Master | grep Mono:'); - - if ($volume) { - preg_match('/\[([0-9%]+)\]/', $volume, $mach); - - if (isset($mach[1])) { - $element->addAttribute(new Attribute($mach[1].' ♪', Attribute::MUSIC)); - } - } - - return $element; -} - -function get_the_public_ip($ipv6 = false) -{ - $element = new Element(); - $memcache = new Memcached(); - $key = $ipv6 ? 'ipv6' : 'ipv4'; - $cache = 60 * 5; - $memcache->addServer('localhost', 11211); - - $ip = $memcache->get($key); - - if (empty($ip)) { - $ip = trim(shell_exec('wget -O - -q '.($ipv6 ? '-6' : '').' ip.deblan.org | head -n 1 | cut -c1-50')); - - if ($ip === '192.168.0.254') { - $ip = trim(shell_exec('wget -O - -q '.($ipv6 ? '-6' : '').' "https://api.ipify.org/?format=text" | head -n 1 | cut -c1-50')); - } elseif (empty($ip)) { - $ip = '?'; - } - - $memcache->set($key, '[wan] '.$ip, $cache); - } - - $memcache->quit(); - - $element->addAttribute(new Attribute($ip, Attribute::INFO)); - - return $element; -} - -function get_the_public_ipv4() -{ - return get_the_public_ip(); -} - -function get_the_public_ipv6() -{ - return get_the_public_ip(true); -} - -function get_spotify_metadata() -{ - $status = trim(shell_exec('playerctl -p spotify status')); - - //if (!in_array($status, ['Playing', 'Paused'])) { - if (!in_array($status, ['Playing'])) { - return null; - } - - // $status = str_replace( - // ['Playing', 'Paused'], - // ['▮▮', '▶'], - // $status - // ); - - $cleanup = function($d) { - $string = trim(str_replace(['"', "'"], ' ', $d)); - - if (mb_strlen($string) > 15) { - $string = substr($string, 0, 15).'…'; - } - - return $string; - }; - - $title = $cleanup(shell_exec('playerctl -p spotify metadata xesam:title')); - $artist = $cleanup(shell_exec('playerctl -p spotify metadata xesam:artist')); - - $label = sprintf('%s – %s', $title, $artist); - - $element = new Element(); - $element->addAttribute(new Attribute($label, Attribute::MUSIC)); - - return $element; -} - -$bar = new i3Bar(); - -$bar - ->addElement(get_the_bandwith(array('eth0', 'eth1', 'wlan0', 'easytether0'))) - ->addElement(get_the_ips(array('eth0', 'eth1', 'wlan0', 'easytether0'))) - ->addElement(get_the_public_ipv4()) - ->addElement(get_the_mount_space_used('/')) - ->addElement(get_the_mount_space_used('/home', array(80, 90))) - ->addElement(get_the_mount_space_used('/share')) - ->addElement(get_the_mount_space_used('/secured')) - ->addElementIfNotEmtpy(get_spotify_metadata()) - ->addElement(get_the_master_volume()) - ->addElement(get_the_date()) - ->addElement(get_the_acpi()) -; - -echo $bar; diff --git a/i3status.sh b/i3status.sh deleted file mode 100755 index 7cfad1e..0000000 --- a/i3status.sh +++ /dev/null @@ -1,9 +0,0 @@ -#!/bin/sh - -echo '{"version":1}' - -echo '[[]' - -while true; do - php ~/.i3/i3status.php 2>/dev/null -done diff --git a/i3status2.php b/i3status2.php deleted file mode 100644 index 6e4a482..0000000 --- a/i3status2.php +++ /dev/null @@ -1,376 +0,0 @@ -full_text = $full_text; - - if (null === $color) { - $color = self::NORMAL; - } - - $this->color = $color; - } - - public function setFullText($full_text) - { - $this->full_text = $full_text; - - return $this; - } - - public function getFullText() - { - return $this->full_text; - } - - public function setColor($color) - { - $this->color = $color; - - return $this; - } - - public function getColor() - { - return $this->color; - } -} - -class Element -{ - public $attributes = null; - - public function __construct() - { - $this->attributes = array(); - } - - public function setAttributes(array $attributes) - { - $this->attributes = $attributes; - - return $this; - } - - public function getAttributes() - { - return $this->attributes; - } - - public function addAttribut(Attribute $a) - { - $this->attributes[] = $a; - } -} - -class i3Bar -{ - protected $elements = null; - - public function __construct() - { - $this->protected = array(); - } - - public function setElements(array $elements) - { - $this->elements = $elements; - - return $this; - } - - public function getElements() - { - return $this->elements; - } - - public function addElement($e) - { - if (!empty($e)) { - $this->elements[] = $e; - } - - return $this; - } - - public function __toString() - { - $render = ',['; - - foreach ($this->getElements() as $element) { - $attributes = $element->getAttributes(); - - if (!is_array($attributes)) { - $attributes = array($attributes); - } - - if (empty($attributes)) { - continue; - } - - $attrs = array(); - - foreach ($attributes as $attr) { - $attr_render = '{'; - $attr_render.= sprintf('"full_text": " %s ",', $attr->getFullText()); - $attr_render.= sprintf('"color": "%s"', $attr->getColor()); - $attr_render.= '}'; - - $attrs[] = $attr_render; - } - - $render.= count($element) > 1 ? '[' : ''; - $render.= implode(',', $attrs).','; - $render.= count($element) > 1 ? ']' : ''; - } - - $render.= ']'; - - $render = str_replace(',]', ']', $render)."\n"; - - return $render; - } -} - -/** - * - * IPS - * - */ -function get_the_ips($ifaces=array()) -{ - $element = new Element(); - - foreach ($ifaces as $iface) { - preg_match('`inet ([^\s]+)`', shell_exec('ip addr show '.escapeshellcmd($iface)), $inet); - - if ($inet) { - $element->addAttribut(new Attribute('['.$iface.'] '.$inet[1], Attribute::INFO)); - } - } - - return $element; -} - -/** - * - * BANDWITH - * - */ -function get_the_bandwith($ifaces=array()) -{ - $element = new Element(); - - foreach ($ifaces as $iface) { - preg_match('`inet ([^\s]+)`', shell_exec('ip addr show '.escapeshellcmd($iface)), $inet); - - if ($inet) { - $bwidth = explode(' ', preg_replace('/ +/', ' ', trim(shell_exec('ifstat -i '.escapeshellcmd($iface).' 1 1 | tail -n 1')))); - - //$dstats = trim(shell_exec('/sbin/ifconfig '.escapeshellcmd($iface).' | grep bytes | sed \'s/[()]//g\' | awk \'{print $3"Mo↓ "$7"Mo↑"}\'')); - //$ips[] = '['.$iface.'] '.$bwidth[0].'Ko/s↓ '.$bwidth[1].'Ko/s↑ ('.$dstats.')'; - - $element->addAttribut(new Attribute('['.$iface.'] '.$bwidth[0].'Ko/s↓ '.$bwidth[1].'Ko/s↑', Attribute::INFO)); - } - } - - return $element; -} - -/** - * - * SENSORS - * - */ -function get_sensors() { - $sensors = array(); - - preg_match_all('/^([^:]+): +\+([0-9.]+)°C +\(high = \+([0-9.]+)°C, crit = \+([0-9.]+)°C\)/iU', shell_exec('sensors | grep "°C"'), $datas, PREG_SET_ORDER); - - var_dump($datas); - foreach($datas as $data) { - } - - return $sensors; -} - -function get_sensor($item) { - get_sensors(); -} - -/** - * - * MOUNT - * - */ -function get_the_mount_space_used($mount_point) -{ - $element = new Element(); - $mount_point = ' '.$mount_point.'$'; - $mount = shell_exec('df -h | egrep '.escapeshellcmd($mount_point).' | head -n 1'); - - if ($mount) { - list($dev, $size, $used, $free, $usedP, $point) = explode(' ', preg_replace('/\s+/', ' ', $mount)); - - $pourcent = intval($usedP); - - if ($pourcent < 70) { - $color = Attribute::NORMAL; - } elseif ($pourcent < 90) { - $color = Attribute::WARNING; - } else { - $color = Attribute::CRITICAL; - } - - $element->addAttribut(new Attribute('['.$point.'] '.$usedP, $color)); - } - - return $element; -} - -/** - * - * DATE - * - */ -function get_the_date() -{ - $element = new Element(); - - $element->addAttribut(new Attribute(date('H:i'), Attribute::DATE)); - $element->addAttribut(new Attribute(date('d/m/Y'), Attribute::DATE)); - - return $element; -} - -/** - * - * ACPI - * - */ -function get_the_acpi() -{ - $element = new Element(); - $acpi = shell_exec('acpi'); - - if (!preg_match('`Discharging`', $acpi)) { - preg_match('`([0-9]+%)`', $acpi, $values); - - if (!empty($values)) { - $charging = $values[1]; - - if ($charging < 20) { - $color = Attribute::CRITICAL; - } elseif ($charging < 50) { - $color = Attribute::WARNING; - } else { - $color = Attribute::NORMAL; - } - - $charging.= '↑'; - - $element->addAttribut(new Attribute($charging, $color)); - } - - return $element; - } - - preg_match('`([0-9]+%), ([0-9]+:[0-9]+)`', $acpi, $values); - - if ($values[1] < 20) { - $color = Attribute::CRITICAL; - } elseif ($values[1] < 50) { - $color = Attribute::WARNING; - } else { - $color = Attribute::INFO; - } - - $charging = $values[1].'↓'; - $times = str_replace(':', 'h', $values[2]); - - $element->addAttribut(new Attribute(implode(' ', array($charging, $times)), $color)); - - return $element; -} - -/** - * - * VOLUME - * - */ -function get_the_master_volume() -{ - $element = new Element(); - $volume = shell_exec('amixer get Master | grep Mono:'); - - if ($volume) { - preg_match('/\[([0-9%]+)\]/', $volume, $mach); - - if (isset($mach[1])) { - $element->addAttribut(new Attribute($mach[1].' ♪', Attribute::VOLUME)); - } - } - - return $element; -} - -function get_the_public_ip($ipv6 = false) -{ - $element = new Element(); - $memcache = new Memcached(); - $key = $ipv6 ? 'ipv6' : 'ipv4'; - $cache = 60 * 5; - $memcache->addServer('localhost', 11211); - - $ip = $memcache->get($key); - - if (empty($ip)) { - $ip = shell_exec('wget -O - -q '.($ipv6 ? '-6' : '').' ip.deblan.org'); - - if (!$ip) { - $ip = '?'; - } - - $memcache->set($key, '[wan] '.$ip, $cache); - } - - $memcache->quit(); - - $element->addAttribut(new Attribute($ip, Attribute::INFO)); - - return $element; -} - -function get_the_public_ipv4() -{ - return get_the_public_ip(); -} - -function get_the_public_ipv6() -{ - return get_the_public_ip(true); -} - -$bar = new i3Bar(); - -$bar - ->addElement(get_the_ips(array('eth0', 'eth1', 'wlan0', 'easytether0'))) - #->addElement(get_the_public_ipv4()) - ->addElement(get_the_master_volume()) - ->addElement(get_the_date()) - ->addElement(get_the_acpi()) -; - -echo $bar; diff --git a/i3status2.sh b/i3status2.sh deleted file mode 100755 index 7318599..0000000 --- a/i3status2.sh +++ /dev/null @@ -1,10 +0,0 @@ -#!/bin/sh - -echo '{"version":1}' - -echo '[[]' - -while true; do - php ~/.i3/i3status2.php 2>/dev/null - sleep 10 -done