LinuxQuestions.org
Download your favorite Linux distribution at LQ ISO.
Go Back   LinuxQuestions.org > Blogs > linux-related notes
User Name
Password

Notices


Just annotations of little "how to's", so I know I can find how to do something I've already done when I need to do it again, in case I don't remember anymore, which is not unlikely. Hopefully they can be useful to others, but I can't guarantee that it will work, or that it won't even make things worse.
Rate this Entry

Timeline / "slider" for Openbox' MPC pipe menu (obmpc.sh)

Posted 08-17-2015 at 08:27 PM by the dsc
Updated 03-18-2016 at 01:20 PM by the dsc

My most recent customization/tweak to a script that isn't really mine:

https://github.com/leomeloxp/dotfile...enbox/obmpc.sh

Change the denominator of "slice1" and the put the same number in the loop in order to have more or less time slices. It seems there may be something somewhat wrong with the maths, but seems to work nevertheless.

Code:
  echo "  <separator />"
    dur=$(mpc -f %time% | head -n 1) 
    time=$(echo $dur | awk -F':' '{if (NF == 2) {print $1 * 60 + $2} else {print $1 * 60 * 60 + $2 * 60 + $3}}')
    nowsec=$(mpc | grep "playing" | grep  % | awk '{print $3}' | sed 's|/.*||' | awk -F':' '{if (NF == 2) {print $1 * 60 + $2} else {print $1 * 60 * 60 + $2 * 60 + $3}}')
    slice1=$((time/25))
    slice=0
         for n in {1..25} ; do
    slice=$((slice+slice1))
    if [[ $nowsec -ge $slice ]] && [[ $nowsec -lt $((slice+slice1)) ]] ; then
    echo "  <separator label=\"      $(date -u -d @${slice} +"%T"| sed 's|^00:||' )\"/>" ;
    else
    echo "  <item label=\"      $(date -u -d @${slice} +"%T"| sed 's|^00:||' )\">"
    echo "    <action name=\"Execute\"><execute>mpc seek $slice</execute></action>"
    echo "  </item>"      ; fi
    done
echo "  <separator />"

EDIT: printf is faster than this "date" hack:

Code:
   echo "  <separator label=\"      $(printf "\n%02d:%02d:%02d\n" $((slice/3600)) $((slice/60)) $((slice % 60)) | sed 's|^00:||' )\"/>" ;
    else
    echo "  <item label=\"      $(printf "\n%02d:%02d:%02d\n" $((slice/3600)) $((slice/60)) $((slice % 60)) | sed 's|^00:||' )\">"

And by the way, no need to store and echo the "dur" variable:

Code:
    time=$(mpc -f %time% | head -n 1 | awk -F':' '{if (NF == 2) {print $1 * 60 + $2} else {print $1 * 60 * 60 + $2 * 60 + $3}}')



___________________________________________________

Yet another thing. It doesn't need an echo for each line of xml. Not only the xml can be a single line, with no newlines ever between each tag, but, in order to make the code more readable, it still can be just:

Code:
echo "<xml blabla>
<xml tag>whatever $VAR whatnot</xml tag>
</xml this>
</xml that>
</close all xml>" # notice that the echo quote is closed only here. 
# A comment in any other line before that would then obviously be printed as bad XML.
It possibly can be just one printf, if it makes a difference. But may be harder to do that, I don't quite get the "syntax".


It's valid for all xml, or almost anything really (but not python, at least), not just for a bash-generated on the fly thing, of course. But an actual single-huge-line file can be way slower to read in an editor, even if it's some half nanoseconds faster for processing elsewhere, if it really is. The point here is more that it can be just a single "echo" whenever there are no "if" alternatives or something like that.


__________________________

A second minor addition is to have it somewhere in the very start:

Code:
if  [[ -z $(pidof mpd ) ]] ; then

echo "<?xml version=\"1.0\" encoding=\"UTF-8\"?><openbox_pipe_menu><separator label=\"mpd not running\" />    </openbox_pipe_menu>"
exit 0
fi
So if MPD isn't running yet, it won't hang your openbox menu/openbox itself until mpd starts to run.
Posted in Uncategorized
Views 739 Comments 0
« Prev     Main     Next »
Total Comments 0

Comments

 

  



All times are GMT -5. The time now is 02:45 AM.

Main Menu
Advertisement
Advertisement
My LQ
Write for LQ
LinuxQuestions.org is looking for people interested in writing Editorials, Articles, Reviews, and more. If you'd like to contribute content, let us know.
Main Menu
Syndicate
RSS1  Latest Threads
RSS1  LQ News
Twitter: @linuxquestions
Facebook: linuxquestions Google+: linuxquestions
Open Source Consulting | Domain Registration