Browse Source

clean git status

master
Simon Vieille 1 year ago
parent
commit
9b654ba474
No known key found for this signature in database
3 changed files with 502 additions and 57 deletions
  1. 116
    57
      i3status.php
  2. 376
    0
      i3status2.php
  3. 10
    0
      i3status2.sh

+ 116
- 57
i3status.php View File

@@ -4,18 +4,21 @@ class Attribute
{
protected $full_text;
protected $color;
protected $separator = true;

const NORMAL = '#B3FF6C';
const WARNING = '#FF6836';
const NORMAL = '#B3FF6C';
const WARNING = '#FF6836';
const CRITICAL = '#FF474A';
const INFO = '#E9F1FF';
const DATE = '#98A6C7';
const VOLUME = '#C3D4FF';
const INFO = '#E9F1FF';
const DATE = '#98A6C7';
const MUSIC = '#C3D4FF';

public function __construct($full_text, $color = null)
public function __construct($full_text, $color = null, $separator = true)
{
$this->full_text = $full_text;

$this->separator = $separator;

if (null === $color) {
$color = self::NORMAL;
}
@@ -46,6 +49,18 @@ class Attribute
{
return $this->color;
}

public function setSeparator($separator)
{
$this->separator = $separator;

return $this;
}

public function getSeparator()
{
return $this->separator;
}
}

class Element
@@ -69,7 +84,7 @@ class Element
return $this->attributes;
}

public function addAttribut(Attribute $a)
public function addAttribute(Attribute $a)
{
$this->attributes[] = $a;
}
@@ -105,6 +120,15 @@ class i3Bar
return $this;
}

public function addElementIfNotEmtpy($e)
{
if (empty($e)) {
return $this;
}

return $this->addElement($e);
}

public function __toString()
{
$render = ',[';
@@ -124,19 +148,29 @@ class i3Bar

foreach ($attributes as $attr) {
$attr_render = '{';
$attr_render.= sprintf('"full_text": " %s ",', $attr->getFullText());
$attr_render.= sprintf('"color": "%s"', $attr->getColor());
$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 .= count($element) > 1 ? '[' : '';
$render .= implode(',', $attrs).',';
$render .= count($element) > 1 ? ']' : '';
}

$render.= ']';
$render .= ']';

$render = str_replace(',]', ']', $render)."\n";

@@ -145,11 +179,9 @@ class i3Bar
}

/**
*
* IPS
*
* IPS.
*/
function get_the_ips($ifaces=array())
function get_the_ips($ifaces = array())
{
$element = new Element();

@@ -157,7 +189,7 @@ function get_the_ips($ifaces=array())
preg_match('`inet ([^\s]+)`', shell_exec('ip addr show '.escapeshellcmd($iface)), $inet);

if ($inet) {
$element->addAttribut(new Attribute('['.$iface.'] '.$inet[1], Attribute::INFO));
$element->addAttribute(new Attribute('['.$iface.'] '.$inet[1], Attribute::INFO));
}
}

@@ -165,11 +197,9 @@ function get_the_ips($ifaces=array())
}

/**
*
* BANDWITH
*
* BANDWITH.
*/
function get_the_bandwith($ifaces=array())
function get_the_bandwith($ifaces = array())
{
$element = new Element();

@@ -182,7 +212,7 @@ function get_the_bandwith($ifaces=array())
//$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));
$element->addAttribute(new Attribute('['.$iface.'] '.$bwidth[0].'Ko/s↓ '.$bwidth[1].'Ko/s↑', Attribute::INFO));
}
}

@@ -190,9 +220,7 @@ function get_the_bandwith($ifaces=array())
}

/**
*
* SENSORS
*
* SENSORS.
*/
function get_sensors()
{
@@ -200,7 +228,7 @@ function get_sensors()

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);
var_dump($datas);
foreach ($datas as $data) {
}

@@ -213,15 +241,13 @@ function get_sensor($item)
}

/**
*
* MOUNT
*
* MOUNT.
*/
function get_the_mount_space_used($mount_point, $limits = array(70, 90))
{
$element = new Element();
$element = new Element();
$mount_point = ' '.$mount_point.'$';
$mount = shell_exec('df -h | egrep '.escapeshellcmd($mount_point).' | head -n 1');
$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));
@@ -236,36 +262,32 @@ function get_the_mount_space_used($mount_point, $limits = array(70, 90))
$color = Attribute::CRITICAL;
}

$element->addAttribut(new Attribute('['.$point.'] '.$usedP, $color));
$element->addAttribute(new Attribute('['.$point.'] '.$usedP, $color));
}

return $element;
}

/**
*
* DATE
*
* DATE.
*/
function get_the_date()
{
$element = new Element();

$element->addAttribut(new Attribute(date('H:i:s'), Attribute::DATE));
$element->addAttribut(new Attribute(date('d/m/Y'), Attribute::DATE));
$element->addAttribute(new Attribute(date('H:i:s'), Attribute::DATE));
$element->addAttribute(new Attribute(date('d/m/Y'), Attribute::DATE));

return $element;
}

/**
*
* ACPI
*
* ACPI.
*/
function get_the_acpi()
{
$element = new Element();
$acpi = shell_exec('acpi');
$acpi = shell_exec('acpi');

if (!preg_match('`Discharging`', $acpi)) {
preg_match('`([0-9]+%)`', $acpi, $values);
@@ -281,9 +303,9 @@ function get_the_acpi()
$color = Attribute::NORMAL;
}

$charging.= '↑';
$charging .= '↑';

$element->addAttribut(new Attribute($charging, $color));
$element->addAttribute(new Attribute($charging, $color));
}

return $element;
@@ -300,17 +322,15 @@ function get_the_acpi()
}

$charging = $values[1].'↓';
$times = str_replace(':', 'h', $values[2]);
$times = str_replace(':', 'h', $values[2]);

$element->addAttribut(new Attribute(implode(' ', array($charging, $times)), $color));
$element->addAttribute(new Attribute(implode(' ', array($charging, $times)), $color));

return $element;
}

/**
*
* VOLUME
*
* MUSIC.
*/
function get_the_master_volume()
{
@@ -321,7 +341,7 @@ function get_the_master_volume()
preg_match('/\[([0-9%]+)\]/', $volume, $mach);

if (isset($mach[1])) {
$element->addAttribut(new Attribute($mach[1].' ♪', Attribute::VOLUME));
$element->addAttribute(new Attribute($mach[1].' ♪', Attribute::MUSIC));
}
}

@@ -330,18 +350,20 @@ function get_the_master_volume()

function get_the_public_ip($ipv6 = false)
{
$element = new Element();
$element = new Element();
$memcache = new Memcached();
$key = $ipv6 ? 'ipv6' : 'ipv4';
$cache = 60 * 5;
$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');
$ip = trim(shell_exec('wget -O - -q '.($ipv6 ? '-6' : '').' ip.deblan.org | head -n 1 | cut -c1-50'));

if (!$ip) {
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 = '?';
}

@@ -350,7 +372,7 @@ function get_the_public_ip($ipv6 = false)

$memcache->quit();

$element->addAttribut(new Attribute($ip, Attribute::INFO));
$element->addAttribute(new Attribute($ip, Attribute::INFO));

return $element;
}
@@ -365,6 +387,42 @@ 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
@@ -375,6 +433,7 @@ $bar
->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())

+ 376
- 0
i3status2.php View File

@@ -0,0 +1,376 @@
<?php

class Attribute
{
protected $full_text;
protected $color;

const NORMAL = '#B3FF6C';
const WARNING = '#FF6836';
const CRITICAL = '#FF474A';
const INFO = '#E9F1FF';
const DATE = '#98A6C7';
const VOLUME = '#C3D4FF';

public function __construct($full_text, $color = null)
{
$this->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;

+ 10
- 0
i3status2.sh View File

@@ -0,0 +1,10 @@
#!/bin/sh

echo '{"version":1}'

echo '[[]'

while true; do
php ~/.i3/i3status2.php 2>/dev/null
sleep 10
done

Loading…
Cancel
Save