dotfiles

<-- duh.
git clone https://hhvn.uk/dotfiles
git clone git://hhvn.uk/dotfiles
Log | Files | Refs | Submodules | LICENSE

interactive (1936B)


      1 #!/bin/rc
      2 
      3 usage = (
      4 'usage: tag/interactive [-n] [-k] [-p metadata] [--artist artist]'
      5 '                       [--title title] [--album album]'
      6 '		        [--track track] [--comment comment]'
      7 '			[--date date] files...'
      8 ''
      9 'tag/interactive is an interactive interface to tag/set.'
     10 ''
     11 '-p Metadata to prompt the user for. Usuable multiple times.'
     12 '   Can contain: artist, title, album, track, comment'
     13 '-n Dry run. Prints commands to be executed.'
     14 '-k Preserve all metadata this is not specified.'
     15 ''
     16 '--artist, --title, --album, --track, --comment and --date'
     17 '  Specify default metadata to apply to all files.'
     18 )
     19 
     20 targs = ()
     21 
     22 fn usage {
     23 	for (line in $usage) {
     24 		printf '%s\n' $line >[1=2]
     25 	}
     26 	exit 2
     27 }
     28 
     29 fn addmd {
     30 	if (~ $2 ()) {
     31 		usage
     32 	}
     33 	targs = ($targs $1 $2)
     34 	have = ($have `$nl{echo $1 | sed 's/^--//'})
     35 }
     36 
     37 fn get {
     38 	if (~ $want $1) {
     39 		tput bold
     40 		printf '%s: ' $1
     41 		tput sgr0
     42 		curtargs = ($curtargs --$1 `$nl{head -n 1})
     43 	}
     44 }
     45 
     46 fn printcmd {
     47 	whatis $1 >[2=1] | sed 's/^[^=]*=(//;s/)$//'
     48 }
     49 
     50 while (!~ $#* 0) {
     51 	switch ($1) {
     52 	case --artist
     53 		addmd $*
     54 		shift
     55 	case --title
     56 		addmd $*
     57 		shift
     58 	case --album
     59 		addmd $*
     60 		shift
     61 	case --track
     62 		addmd $*
     63 		shift
     64 	case --comment
     65 		addmd $*
     66 		shift
     67 	case --date
     68 		addmd $*
     69 		shift
     70 	case -p
     71 		if (~ $2 ()) {
     72 			usage
     73 		}
     74 		if (!~ $2 artist title album track comment date) {
     75 			printf 'Unsupported prompt: %s\n' $2 >[1=2]
     76 		}
     77 		want = ($want $2)
     78 		shift
     79 	case -k
     80 		targs = (-k $targs)
     81 	case -n
     82 		dry = 1
     83 	case -*
     84 		printf 'No such option: %s\n' $1 >[1=2]
     85 		usage
     86 	case *
     87 		files = ($files $1)
     88 	}
     89 	shift
     90 }
     91 
     92 for (w in $want) {
     93 	if (~ $have $w) {
     94 		printf 'Cannot have both --%s and -p %s\n' $w $w >[1=2]
     95 	}
     96 }
     97 
     98 for (f in $files) {
     99 	curtargs = $targs
    100 	printf '==> %s <==\n' $f
    101 	get artist
    102 	get title
    103 	get album
    104 	get track
    105 	get comment
    106 	cmd = (tag/set $curtargs $f)
    107 	if (~ $dry 1) {
    108 		printcmd cmd
    109 		tag/set -n $curtargs $f >[2=1]
    110 	} else {
    111 		echo Running `$nl{printcmd cmd}
    112 		$cmd
    113 	}
    114 }