Skip to content

Instantly share code, notes, and snippets.

@Dobbie03
Created January 24, 2017 03:22
Show Gist options
  • Save Dobbie03/2c550b3333caca26cebdb065fb320185 to your computer and use it in GitHub Desktop.
Save Dobbie03/2c550b3333caca26cebdb065fb320185 to your computer and use it in GitHub Desktop.
#!/usr/bin/env perl
use strict;
use warnings;
use autodie;
use POSIX qw(strftime);
use XML::Simple;
use Data::Dumper;
# Distro -------------------------------------------------------------------
open (my $issue, "<", "/etc/issue");
my $distro;
while (<$issue>) {
if (/^[^\s]/) {
$distro = (split / /, ((split /\\/)[0]))[0];
last;
}
}
close $issue;
# Host ---------------------------------------------------------------------
my $host = `uname -n`;
chomp $host;
# Kernel -------------------------------------------------------------------
my $kernel = `uname -r`;
chomp $kernel;
# Load ---------------------------------------------------------------------
my $load = (split ' ', (split ':', `uptime`)[4])[0];
chop $load;
# Machine ------------------------------------------------------------------
my $machine = `uname -m`;
chomp $machine;
# Memory (active) ----------------------------------------------------------
open (my $meminfo, "<", "/proc/meminfo");
my $mem_act;
while (<$meminfo>) {
chomp;
if (/^Active:/) {
$mem_act = int(((split)[-2])/1024);
last;
}
}
close $meminfo;
# Openbox theme ------------------------------------------------------------
my $file = "$ENV{HOME}/.config/openbox/rc.xml";
my $xs1 = XML::Simple->new();
my $doc = $xs1->XMLin($file);
my $obtheme = $doc->{theme}->{'name'};
# OS -----------------------------------------------------------------------
my $os = `uname -o`;
chomp $os;
# Time ---------------------------------------------------------------------
my $time_date = strftime "%B, %d, %Y - %R", localtime;
# Uptime -------------------------------------------------------------------
my $uptime = (split ' ', `uptime`)[0];
# Writing the pipemenu -----------------------------------------------------
print "<?xml version=\"1.0\" encoding=\"UTF-8\"?>\n"
. "<openbox_pipe_menu>\n"
. "<item label=\"+ $ENV{USER}\@$host +\" />\n"
. "<separator />"
. "<item label=\"OS: $distro $os $machine \" />\n"
. "<item label=\"Kernel: $kernel \" />\n"
. "<item label=\"Uptime: $uptime \" />\n"
. "<item label=\"Load: $load \" />\n"
. "<item label=\"Mem: $mem_act MB\" />\n"
. "<item label=\"Theme: $obtheme \" />\n"
. "<separator />"
. "<item label=\"+ $time_date +\" />\n"
. "</openbox_pipe_menu>\n";
#!/usr/bin/env python
# Dynamic places menu generator for openbox
# Uses a list of parent directories to provide a menu of subdirectories
# After removing any entries found in an ignore list
#
#
# Originally created by: Kev at http://crunchbanglinux.org/forums/topic/373/dynamic-places-pipe-menu/
#
#
# 1. Save this script to a place you prefer (mine is in ~/.config/openbox/scripts/)
# 2. Make it executable
# 3. Insert a line into your ~/.config/openbox/menu.xml like:
# <menu execute="perl ~/.config/openbox/scripts/places.py menu" id="pipe-places" label="Places"/>
# 4. Call the pipe-menu in your menu by adding the following line to the same file:
# <menu id="pipe-places"/>
# 5. Reconfigure OpenBox
# 6. You're done
#
# Important variables if you want to customise the generated menu:
# manager - File manager which you want to use (nautilus, thunar, pcmanfm, rox-filer, konqueror, dolphin, etc).
# dirs - The script lists and displays the subdirectories of these directories
# ignore - Any item exactly matching an entry in this list will not be displayed
# items - If you want to add a single directory entry to the menu you can do it here
#
import glob
import fnmatch
from os.path import expanduser
# File manager you want to use to open directories
manager = "pcmanfm"
# User home directory. If you hard code this location instead of relying on
# expanduser() then you can remove its import above
home = expanduser('~')
# List of directories whose subdirectories we want to display
dirs = [home, '/mnt/server/']
# List of directories to ignore
# Shell-style wildcards as used by fnmatch are supported
ignore = ['/media/cdrom0']
# Our list of menu items
# If you want to add single directories to the menu without including
# all of their subdirs then put them in here
items = []
# Iterate through dirs
for dir in dirs:
# Get a list of subdirectories for each dir
subdirs = glob.glob(dir + '/*/')
# Alphabetise subdirs. Sorting here is less efficient but preserves
# directory order specified above. If this doesn't matter to you then
# remove this line and uncomment "items.sort(key=str.lower)" below
subdirs.sort(key=str.lower)
# Append each subdir to the items list
for sub in subdirs:
# Replace /home/user with ~ before appending to items (looks better)
sub = sub.replace(home, '~')
# Strip trailing / characters
sub = sub.rstrip('/')
items.append(sub)
# Iterate through ignore list and remove matches from items
for i in ignore:
matches = fnmatch.filter(items, i)
for m in matches:
try:
items.remove(m)
except ValueError:
pass
# Alphabetise directory list. Read comment for sort() above before uncommenting
#items.sort(key=str.lower)
# Output xml for the openbox menu
print('<openbox_pipe_menu>')
# Each item becomes a menu entry
for i in items:
print ('<item label="',i,'">')
print ('<action name="Execute">')
print ('<execute>')
print (manager,'" "',i)
print ('</execute>')
print ('</action>')
print ('</item>')
print ('</openbox_pipe_menu>')
#!/bin/sh
# recently_opened_menu.sh - a script to parse .recently-used.xbel
# and generate openbox pipe menu
# Copyright (C) 2010 John Crawley
#
# This program is free software: you can redistribute it and/or modify
# it under the terms of the GNU General Public License as published by
# the Free Software Foundation, either version 3 of the License, or
# (at your option) any later version.
#
# This program is distributed in the hope that it will be useful,
# but WITHOUT ANY WARRANTY; without even the implied warranty of
# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
# GNU General Public License for more details.
# version 2012/07/01
# Usage: add
# <menu id="recent" label="Recent Files" execute="/path/to/recently_opened_menu.sh" />
# to your .config/openbox/menu.xml, or use with dash_places_menu.sh (see comments there)
maximum_entries=15 # max. number of entries in menu
#######################################################################
# look for recently-used.xbel
if [ $XDG_DATA_HOME ] && [ -r "${XDG_DATA_HOME}/recently-used.xbel" ]
then
file_path="${XDG_DATA_HOME}/recently-used.xbel"
elif [ -r "${HOME}/.local/share/recently-used.xbel" ]
then
file_path="${HOME}/.local/share/recently-used.xbel"
elif [ -r "${HOME}/.recently-used.xbel" ]
then
file_path="${HOME}/.recently-used.xbel"
else
echo "$0: cannot find a readable recently-used.xbel file" >&2
echo '<openbox_pipe_menu>
<separator label="No recently-used.xbel file found." />
</openbox_pipe_menu>'
exit 1
fi
# if argument is --clear, empty .recently-used.xbel
[ "$1" = '--clear' ] && {
cat <<':EOF' > "${file_path}"
<?xml version="1.0" encoding="UTF-8"?>
<xbel version="1.0"
xmlns:bookmark="http://www.freedesktop.org/standards/desktop-bookmarks"
xmlns:mime="http://www.freedesktop.org/standards/shared-mime-info"
>
</xbel>
:EOF
exit
}
maximum_entries=$((maximum_entries+2))
pre=' <item label="'
mid='">
<action name="Execute"><command>'
post='</command></action>
</item>'
files=$( tac "${file_path}" | awk -v MAX="$maximum_entries" -v PR="$pre" -v MI="$mid" -v PO="$post" 'BEGIN {
RS="</bookmark>";
FS="<info>";
}
(NR == MAX) {exit}
!/<bookmark/ {next}
!/href=[\"'\'']file:\/\// {next}
# $1 is the command, $2 the file path
{
sub(/^.*exec=\"\&apos\;/,"",$1)
sub(/\&apos\;.*$/,"",$1)
sub(/ *%./,"",$1)
sub(/^.*file:\/\//,"",$2)
sub(/[\"'\''].*$/,"",$2)
gsub(/%22/,"\\&quot;",$2)
gsub(/%3C/,"\\&lt;",$2)
gsub(/%3E/,"\\&gt;",$2)
name=$2
sub(/^.*\//,"",name)
gsub(/_/,"__",name)
gsub(/\&apos;/,"\\&apos;\\&quot;\\&apos;\\&quot;\\&apos;",$2)
print (PR name MI $1 " '"'"'" $2 "'"'"'" PO)
}' )
# use perl to decode urlencoded characters
files=$(perl -MURI::Escape -e 'print uri_unescape($ARGV[0]);' "$files")
output='<openbox_pipe_menu>
'"$files"'
<separator />
<item label="Clear Recent Files">
<action name="Execute">
<command>
&apos;'"$0"'&apos; --clear
</command>
</action>
</item>
</openbox_pipe_menu>
'
printf '%s' "$output" # printf because echo sometimes eats backslashes
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment