29 February 2020

Lessons learnt using (fighting with) YAD in Linux


Lessons learnt using (fighting with) YAD in Linux
==================================================

When using buttons with actions
--button="ButtonName":'bash -c "list_of_commands"'

Use this format:
Enclose the whole bash command between ''
Enclose the commands to execute between ""
You can run any of them in background with &
example:
"list_of_commands"
" cmd1 ; cmd2 & cmd3" # cmd2 and cm3 will run in parallel


If your list_of_commands contains functions previously created, export them in advance with
# export -f func1 func2 ...

If your list_of_commands contains variables previously created, export them in advance with
# export VAR1 VAR2 ...


If you need any of the commands to stop YAD (as opposed to have a button just with Exit), then add this at the end of your commands.
Classic version:
--button="Do":'bash -c "do_something;"'
--button="Exit"
Economy version (save one button)
--button="Do and Exit":'bash -c "do_something; kill -SIGUSR1 $YAD_PID"'
or
--button="Do and Exit":'bash -c "do_something && kill -SIGUSR1 $YAD_PID"'


Changing the label to stock (gtk) buttons. Use:
yad --text "Simple dialog" --button='label!ButtonName!tooltip:bash -c "blahblah"'

Note:You can find icons on /usr/share/icons

Being Label and tooltip anything you want
yad --text "Simple dialog" --button='Record!gtk-stop!Press to start recording:bash -c "blahblah"'




If you want vertical buttons do like this (--form and fbtn on buttons is the trick)
yad --form --width=5  --field="Chromium":fbtn "chromium" --field="Firefox":fbtn "chromium2"




If you only want your buttons only (no Cancel / OK buttons), use the --no-buttons
yad --form --width=5  --field="Chromium":fbtn "chromium" --field="Firefox":fbtn "chromium2" --no-buttons


But if you want the vertical buttons to still use nice icons, you can.
yad --form --width=5 --text="Blah" --field='My own stop!stop!tooltip':fbtn "chromium2" --button='My other stop!stop!tooltip2':0
The format is (or seems to be, I just found it trying)
--field='Text on the button!SpecificButtonName!tooltip description':fbtn "the_commad_to_execute"

With a more visual example that you can test
myzen (){ zenity --info --width=100 --height=100 --text "Click me";}
export -f myzen

yad --form --width=5 --text="Blah" --field='My own stop!stop!tooltip':fbtn 'bash -c "myzen"' --button='My other stop!stop!tooltip2':'bash -c "myzen"'

Notice the difference between
--field='My own stop!stop!tooltip':fbtn 'bash -c "myzen"'
--button='My other stop!stop!tooltip2':'bash -c "myzen"'
Very important the location of single quotes '



A simple way to put all together (better ways are possible)

################## from https://github.com/cjungmann/yaddemo/blob/master/docs/yadbuttons.md

start_script () {
cmd1;
cmd2;
}
export -f start_script

# Idem for stop_script


# Collect the YAD options
cmd=(yad
--borders=20
--width 300
--height 100
--on-top
--skip-taskbar
--borders="30"
--undecorated
--columns 1
--no-focus
--mouse
    --title="Title of window"
    --button="Start":"bash -c start_script"
    --button="Stop":'bash -c "stop_script & kill -SIGUSR1 $YAD_PID"'
    --button="Zenity":'bash -c "zenity --info --width=100 --height=100 --text $YAD_PID"'
    --button="Kill":'bash -c "kill -SIGUSR1 $YAD_PID"'
    --button="Exit"
)

# Run it
"${cmd[@]}"
##################

21 February 2020

Text transformations.

I converted a pdf to txt  in order to feed it to a text-to-speech program in order to generate an audiobook.

The first conversion (using the tool from the link above) already removed the page numbers, footer, heading,  (other converters did not) but I had to do some extra cleaning.

Below are the terms
SEARCH
REPLACE
that can be used within Sublime text editor to easily clean the whole text.

Note: Whenever you see " (double quotes) below, remove it before inserting it in Sublime. I just left it to make sure the spaces are visible when is not obvious.

# remove - at the end of a line and join lines
-\n
""

# join lines
(\w)\n(\w)
\1 \2


(,)\n(\w)
\1 \2

# Note that here I am looking for lowercase on the second letter
(\w)\n\n([a-z])
\1 \2


(\w)\n\n\n([a-z])
\1 \2


", {2,9}\n"
", "


" {2,9}\n"
" "

Too Cool for Internet Explorer