Re: Random wallpaper with WM - a quick hack :)

2015-03-20 Thread Chris F.A. Johnson

On Fri, 20 Mar 2015, Martin Dietze wrote:


On 20 March 2015 at 01:12, Chris F.A. Johnson  wrote:


...

Why are you using an external (i.e. slow) command. Use shell arithmetic:

w=$(( w + 1 ))


Because that's a bashism. I do shell scripting a lot, mostly for
automatiing deployments.


It is not a bashism; it is part of the standard Unix shell.

--
Chris F.A. Johnson, 


--
To unsubscribe, send mail to [email protected].


Re: Random wallpaper with WM - a quick hack :)

2015-03-20 Thread Martin Dietze
On 20 March 2015 at 19:06, Kalin KOZHUHAROV  wrote:
> I thought that I missed something here, so I downloaded the catrand
> source and had a look (didn't bother with publib).
> But isn't that all achieved with `sort -R`?

Quick look - yes. No idea when they introduced this flag :)
Therefore one can live quite well without catrand. It's something I
wrote long ago.
However 'sort -R' is GNU-only which is fine for my Linux box.

> While I agree somewhat of trying to stay "independent" of things like
> bashisms, I would never choose to depend on a piece of software that I
> wrote, that needs to be compiled and further depends on non-standard
> library... what is the chance of that being on random system X
> (assuming /me != Linus_Torvalds||Randal_Swartz, LoL)
> Or do you have this randcat installed everywhere you login?

No, of course not. I generally avoid bashisms. In this case of course
it would not have made any difference.

> A quick and dirty one-liner, with bashism, disregarding
> stretching/cropping, for my 2 desktops (change head -n part):
>
> i=0; for D in $(find ~GNUstep/Library/WindowMaker/Backgrounds/ -type f
> |sort -R |head -n2); do wmsetbg -w ${i} "${D}"; i=$(($i+1)); done
>
> (if one wants to use symlinks, -type l instead)

Yep, that, too, would do the trick :)


-- 
-- [email protected] --/-- [email protected] 
- / http://herbert.the-little-red-haired-girl.org / -


-- 
To unsubscribe, send mail to [email protected].


Re: Random wallpaper with WM - a quick hack :)

2015-03-20 Thread John H. Robinson, IV
Also, one can use wdread to get the number of workspaces that currently exist:

wdread WMState Workspaces | grep -c '^Name'

I'm not terribly happy about the four-spaces-then-Name part, but I
believe the format to be relatively consistent.

$(( 1 + 1)) is supported in dash, so don't need to worry about using
bashisms to get simple arithmetic. If you are happy to use bashisms,
instead of using a counter one can use for i in {1..10}. zsh lets you
use {1..$n}. for i in $(seq 1 $(wdread WMState Workspaces | grep -c '^
   Name')) also works, and avoids all the bashisms.

I did not know about sort -R. However, we still have the problem with
embedded newlines, unless you use -z:

find -L /path/to/backgrounds -type f -print0 | sort -z -R

Now you have a new problem of head not accepting null terminated
lines. Additionally, while -z is in BSD's sort, -R is a GNUism.

Use bash:

typeset -a backgrounds
backgrounds=(/path/to/backgrounds/*); # this sets the array
backgrounds to each file in /path/to/backgrounds
number_of_pictures="${#backgrounds[@]}"
random_picture_index=$(( RANDOM % number_of_pictures )); # Random
number, 0 to total number of files minus one
random_picture="${backgrounds[$random_picture_index]}"; # quotes, to
protect whitespace including newlines
wmsetbg $WHATEVER_OPTIONS_YOU_WANT "$random_picture"; # again, quotes
to protect whitespace

zsh makes this even easier, since arrays are first class citizens:

backgrounds=(/path/to/backgrounds/*); # zsh knows () creates arrays
number_of_pictures=$#backgrounds; # no need for weird array
specification. zsh knows arrays.
random_picture_index=$(( RANDOM % number_of_picutres + 1 )); # zsh
arrays are 1 indexed; bash are 0 indexed.
random_picture=$backgrounds[$random_picture_index]; # zsh knows
scalars too. no need to quote.
wmsetbg $OPTS $random_picture; # $OPTS is an array, right? ${=OPTS}
does $IFS splitting if not

Or more succinctly:

backgrounds=(/path/to/backgrounds/*)
random_picture=$backgrounds[$(( RANDOM % $#backgrounds + 1 ))]
# the succinct bash version is random_picture="${backgrounds[$((
RANDOM % ${#backgrounds[@]} ))]}"

This is why I love zsh.

-john


-- 
To unsubscribe, send mail to [email protected].


Re: Random wallpaper with WM - a quick hack :)

2015-03-20 Thread Kalin KOZHUHAROV
Hello Martin,

On Sat, Mar 21, 2015 at 12:44 AM, Martin Dietze  wrote:
> On 20 March 2015 at 01:12, Chris F.A. Johnson  wrote:
>
>> What does catrand do? Can it not be done with standard tools, e.g. awk?
>>
>> And that is almost always the wrong way to loop through filenames.
>> Done properly, you don't need to worry about spaces in filenames:
>
> Oh, I answered this quesion already in my initial posting:
>
I thought that I missed something here, so I downloaded the catrand
source and had a look (didn't bother with publib).
But isn't that all achieved with `sort -R`?

While I agree somewhat of trying to stay "independent" of things like
bashisms, I would never choose to depend on a piece of software that I
wrote, that needs to be compiled and further depends on non-standard
library... what is the chance of that being on random system X
(assuming /me != Linus_Torvalds||Randal_Swartz, LoL)
Or do you have this randcat installed everywhere you login?

A quick and dirty one-liner, with bashism, disregarding
stretching/cropping, for my 2 desktops (change head -n part):

i=0; for D in $(find ~GNUstep/Library/WindowMaker/Backgrounds/ -type f
|sort -R |head -n2); do wmsetbg -w ${i} "${D}"; i=$(($i+1)); done

(if one wants to use symlinks, -type l instead)

Cheers,
Kalin.


-- 
To unsubscribe, send mail to [email protected].


Re: Random wallpaper with WM - a quick hack :)

2015-03-20 Thread Martin Dietze
On 20 March 2015 at 01:12, Chris F.A. Johnson  wrote:

> What does catrand do? Can it not be done with standard tools, e.g. awk?
>
> And that is almost always the wrong way to loop through filenames.
> Done properly, you don't need to worry about spaces in filenames:

Oh, I answered this quesion already in my initial posting:

-- snipp --
For randomization I use a little CLI utility that I wrote ages ago:
http://www.the-little-red-haired-girl.org/pub/linux/catrand-1.2-1.src.rpm
- as it is the script will not work without it, but you may want to
replace this by something else to create a shuffled list of the file
names.

Since it replaces newlines by spaces I can't really do any of those
neat things allowing me to iterate over file names containing white
spaces in my script, therefore all symlinks have normalised file
names, i.e. without white spaces.
-- snapp --

Please note what is said in the second paragraph.


> Why are you using an external (i.e. slow) command. Use shell arithmetic:
>
> w=$(( w + 1 ))

Because that's a bashism. I do shell scripting a lot, mostly for
automatiing deployments.
That's one reason I avoid bashisms - you don't want to check for the
correct bash version each time you deploy your stuff somewhere.
Since running bashisms on wrong bash versions can lead to things
failing silently, my usual strategy is writing stuff that will even
run o a Bourne shell.
Unless you are writing code for embedded environments where process
creation overhead can really hurt, the above code is totally fine.
When doing real arithmetics you can always use bc :)

Cheers,

m.

-- 
-- [email protected] --/-- [email protected] 
- / http://herbert.the-little-red-haired-girl.org / -


-- 
To unsubscribe, send mail to [email protected].


Re: Random wallpaper with WM - a quick hack :)

2015-03-20 Thread Chris F.A. Johnson

On Thu, 19 Mar 2015, Martin Dietze wrote:


Just thought I'd share this with you.

I played around with random wallpapers with desktops other than WM.
Since in my new job I'm back on a Linux box again (and thus back to
WM) I wanted something similar.

I found that tools like 'variety' do not seem to work. But there's wmsetbg.
And though you can't set different backgrounds per workspace in
WPrefs, wmsetbg can do it quite nicely.

So I did the following:
- created a directory for my wallpaper images
- symlinked all backgrounds I wanted to choose from into it
- wrote a little shell script :)

The script that does the work looks like this:

md@Paulina:[ ~/ ] % cat bin/random-backgrounds.sh
#!/bin/sh

cd /usr/local/share/backgrounds/Variety/
while true; do
 w=0
 date
 for i in `ls *.* | catrand`; do


What does catrand do? Can it not be done with standard tools, e.g. awk?

And that is almost always the wrong way to loop through filenames.
Done properly, you don't need to worry about spaces in filenames:

printf * | catrand | ## better would be to have catrand take arguments: catrand 
*
while read file
do
  : whatever
done


   case $i in
 cat*)
   # cat photos as backgrounds won't be stretched but rather
cropped to fit to my screen
   CMD="wmsetbg -f -w $w $i"
   ;;
 *)
   # all other photos will be stretched to fit
   CMD="wmsetbg -w $w $i"
   ;;
   esac
   echo "$CMD"
   $CMD
   w=`expr $w + 1`


Why are you using an external (i.e. slow) command. Use shell arithmetic:

w=$(( w + 1 ))


   # I've got 7 desktops I use regularly
   if [ $w -gt 6 ]; then
 break
   fi
 done
 sleep 10m
done


--
Chris F.A. Johnson, 


--
To unsubscribe, send mail to [email protected].


Re: Random wallpaper with WM - a quick hack :)

2015-03-19 Thread Martin Dietze
Opps, I found that copy-pasting went wrong, and there was some
information missing.
Le'ts complete things:

On 19 March 2015 at 10:01, Martin Dietze  wrote:

> http://www.the-little-red-haired-girl.org/pub/linux/catrand-1.2-1.src.rpm

Here I forgot to mention, that for compiling this utility you'll need
the publib library.
You can download a src.rpm here:
http://rpmfind.net//linux/RPM/mandriva/9.2/i586/Mandrake/RPMS/publib-devel-0.31-5mdk.i586.html


> My actual $HOME/.xapps script:
>
> md@Paulina:[ ~/ ] % cat /etc/X11/xinit/xinitrc.d/70-xapps.sh
> #!/bin/sh
>
> if [ -x "$HOME/.xapps" ]; then
>   "$HOME/.xapps"
> fi

Here I had the wrong stuff in my clipboard. It was meant to look like this:

md@Paulina:[ ~/ ] % cat .xapps
#!/bin/sh
cd /tmp
rm -f nohup.out
nohup $HOME/bin/random-backgrounds.sh &


Cheers,

m'bert

-- 
-- [email protected] --/-- [email protected] 
- / http://herbert.the-little-red-haired-girl.org / -


-- 
To unsubscribe, send mail to [email protected].


Random wallpaper with WM - a quick hack :)

2015-03-19 Thread Martin Dietze
Just thought I'd share this with you.

I played around with random wallpapers with desktops other than WM.
Since in my new job I'm back on a Linux box again (and thus back to
WM) I wanted something similar.

I found that tools like 'variety' do not seem to work. But there's wmsetbg.
And though you can't set different backgrounds per workspace in
WPrefs, wmsetbg can do it quite nicely.

So I did the following:
- created a directory for my wallpaper images
- symlinked all backgrounds I wanted to choose from into it
- wrote a little shell script :)

The script that does the work looks like this:

md@Paulina:[ ~/ ] % cat bin/random-backgrounds.sh
#!/bin/sh

cd /usr/local/share/backgrounds/Variety/
while true; do
  w=0
  date
  for i in `ls *.* | catrand`; do
case $i in
  cat*)
# cat photos as backgrounds won't be stretched but rather
cropped to fit to my screen
CMD="wmsetbg -f -w $w $i"
;;
  *)
# all other photos will be stretched to fit
CMD="wmsetbg -w $w $i"
;;
esac
echo "$CMD"
$CMD
w=`expr $w + 1`
# I've got 7 desktops I use regularly
if [ $w -gt 6 ]; then
  break
fi
  done
  sleep 10m
done

This script changes wallpapers for desktops #0 .. #6 every 10 minutes.
You will have to change between desktops to get to see this.

For randomization I use a little CLI utility that I wrote ages ago:
http://www.the-little-red-haired-girl.org/pub/linux/catrand-1.2-1.src.rpm
- as it is the script will not work without it, but you may want to
replace this by something else to create a shuffled list of the file
names.

Since it replaces newlines by spaces I can't really do any of those
neat things allowing me to iterate over file names containing white
spaces in my script, therefore all symlinks have normalised file
names, i.e. without white spaces.

The calls to wmsetbg are echoed. I use this for identifying wallpapers
to blacklist (i.e. remove the symlinks from my wallpaper directory) if
they don't come out good.

I run this script automatically in the background. Depending on Linux
distros there's different places in $HOME where you can put stuff like
this. I use a fairly low-level approach:

Support for a script $HOME/.xapps to execute at the start of an xsession:

md@Paulina:[ ~/ ] % cat /etc/X11/xinit/xinitrc.d/70-xapps.sh
#!/bin/sh

if [ -x "$HOME/.xapps" ]; then
  "$HOME/.xapps"
fi

My actual $HOME/.xapps script:

md@Paulina:[ ~/ ] % cat /etc/X11/xinit/xinitrc.d/70-xapps.sh
#!/bin/sh

if [ -x "$HOME/.xapps" ]; then
  "$HOME/.xapps"
fi


Note that I don't really care if any other program runs nohup'ed in
/tmp on my box. Since I use it exclusively I am fairly sure there
isn't :)

That's it. Of course, it's nothing more than a quick hack, but it does
the job quite well. Integration of this kind of functionality into WM
would be nice someday, but in the meantime this will do :)

Cheers,

m'bert

-- 
-- [email protected] --/-- [email protected] 
- / http://herbert.the-little-red-haired-girl.org / -


-- 
To unsubscribe, send mail to [email protected].