You are not logged in.

#1 2014-02-02 18:58:17

Rasi
Member
From: Germany
Registered: 2007-08-14
Posts: 1,914
Website

how to do this zsh script in bash?

Some things I really like about this:

a) the flexible case. it doesnt matter how your menu called $pmt looks like, it will always match by numbers (this is what the prompt function does)
b) the pid file for screencast and stopRecording
c) the dynamic entry for Screencast. It will change its name/action, if a screencast is already in place.
d) the really short delay prompt.

Can bash do these things too in an effecient way?

#!/bin/zsh

function prompt {
    PROMPT_RESULT=$(echo ${(j:\n:)@} | dmenu -l 10 -p ${prompt_txt:-Teiler})
    [[ "$PROMPT_RESULT" =~ "[0-9]+" ]] && echo $MATCH
}

function mainMenu {
    prompt_txt='Teiler: '
    isRecording && STATE_RECORDING=" 2. Stop recording screencast"
    pmt=(" 0: Cancel"
         " 1. Take screenshot"
         ${STATE_RECORDING:-" 2. Record screencast"}
         "---"
         " 3. Quick Screenshot (Fullscreen)"
         " 4. Quick Screenshot (Area)"
         "---"
         " 5. Upload clipboard content (Delayed)"
         " 6. Copy last image to clipboard"
         " 7. Upload a file"
         ${CUSTOM_MENUS:+' 9. More menus!'})
    case $(prompt $pmt) in
        1) snapMenu ;;
        2) isRecording && stopRecording || screencast ;;
        3) screenshot ;;
        4) screenshot -s ;;
        5) notImplemented ;;
        6) notImplemented ;;
        7) notImplemented ;;
        *) exit
    esac
}

function snapMenu {
    pmt=("<< Back to main menu"
         " 1. Screenshot (Fullscreen)"
         " 2. Screenshot (Area)"
         " 3. Delayed Screenshot (Fullscreen)"
         " 4. Delayed Screenshot (Area)")
    case $(prompt $pmt) in
        1) screenshot ;;
        2) screenshot -s ;;
        3) delayPrompt screenshot -d ;;
        4) delayPrompt screenshot -s -d ;;
        *) mainMenu
    esac
}

function screencast {
    (( $+commands[ffmpeg] && $+commands[xdotool] && $+commands[xwininfo] )) \
        || die "Missing dependencies!"
    isRecording && {
        notify "$time" 'Screencast already in progress'
        die 'Already recording screeen'
    }
    [[ -f "${img_path}/lastvid.webm" ]] && rm "${img_path}/lastvid.webm"
    ffmpeg $encopts "${img_path}/lastvid.webm" &
    >$SCREENCAST_PIDFILE <<< $!
    notify "$time" "Screencast started"
}


function stopRecording       {
    local pid
    [[ -f $SCREENCAST_PIDFILE ]] && {
        pid=$(<$SCREENCAST_PIDFILE)
        isRecording && kill "$pid"
        rm "$SCREENCAST_PIDFILE"
    }
    notify "$time" "Stopped recording"
    archiveVid
    return 0
}

function delayPrompt {
    prompt_txt='Delay: '
    t=$(prompt 2 5 10 20 30 "Cancel")
    [[ "$t" =~ "[0-9]+" ]] || die "Canceled"
    [[ $t* -gt 2 ]] && notify $time "Screenshot will be taken in $t seconds"
    $@ $[$t+1]
}

function isRecording {
    [[ -f $SCREENCAST_PIDFILE ]] && \
        [[ "$(pgrep -l -F $SCREENCAST_PIDFILE)" =~ "[0-9]+ ffmpeg" ]]
}

function die { echo "$1"; exit ${2:-1} }


mainMenu

Last edited by Rasi (2014-02-02 19:04:03)


He hoped and prayed that there wasn't an afterlife. Then he realized there was a contradiction involved here and merely hoped that there wasn't an afterlife.

Douglas Adams

Offline

#2 2014-02-22 16:49:37

kerolasa
Member
From: London, UK
Registered: 2014-02-21
Posts: 6
Website

Re: how to do this zsh script in bash?

Looks a lt like a job for an array of descriptions, select for choosing what to do, and functions to do the thing requested. See the dining script below for inspiration.

#!/bin/bash
alacarte=(
        'fish'
        'eggs and ham'
        'desert and leave'
)
eat() {
        echo "nom nom $1"
}
select choise in "${alacarte[@]}"; do
        case "$choise" in
        desert)
                eat "$choise"
                break
                ;;   
        *)
                eat "$choise"
                ;;
        esac
done
exit 0

Offline

Board footer

Powered by FluxBB