File: example.pl

package info (click to toggle)
stfl 0.22-3
  • links: PTS
  • area: main
  • in suites: bookworm
  • size: 408 kB
  • sloc: ansic: 3,344; makefile: 109; perl: 95; ruby: 83; python: 73
file content (135 lines) | stat: -rw-r--r-- 3,308 bytes parent folder | download | duplicates (8)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
#!/usr/bin/perl -w
#
#  STFL - The Structured Terminal Forms Language/Library
#  Copyright (C) 2006, 2007  Clifford Wolf <clifford@clifford.at>
#
#  This library is free software; you can redistribute it and/or
#  modify it under the terms of the GNU Lesser General Public
#  License as published by the Free Software Foundation; either
#  version 3 of the License, or (at your option) any later version.
#  
#  This library is distributed in the hope that it will be useful,
#  but WITHOUT ANY WARRANTY; without even the implied warranty of
#  MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
#  Lesser General Public License for more details.
#  
#  You should have received a copy of the GNU Lesser General Public
#  License along with this library; if not, write to the Free Software
#  Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston,
#  MA 02110-1301 USA
#
#  example.pl: A little STFL Perl example
#

use strict;
use stfl;

my @files;

{
	my $dirlist = join " ", @ARGV;
	open(F, "find $dirlist -iname '*.mp3' -o -iname '*.ogg' |");
	while (<F>) { chomp; push @files, $_; }
	close F;
}

if ($#files == -1) {
	print STDERR "Usage: $0 <directories-with-mp3s-and-or-oggs>\n";
	exit 1
}

my $f = stfl::create( <<EOT );
table
  label
    .colspan:2 .expand:0 .tie:c
    text:"My little jukebox"
  tablebr
  label
    .border:ltb .expand:0
    text:"Search: "
  input[search]
    .border:rtb .expand:h
    style_normal:attr=underline
    style_focus:fg=red,attr=underline
    text[searchterm]:
  tablebr
  !list[filelist]
    .colspan:2 .border:rltb
    style_focus:fg=red
    pos_name[listposname]:
    pos[listpos]:0
  tablebr
  label
    .colspan:2 .expand:0 .tie:r
    text[helpmsg]:
EOT

sub newlist()
{
	my @templist;
	my $searchterm = $f->get("searchterm");

	for (my $i = 0; $i <= $#files; $i++) {
		push @templist, $i if $files[$i] =~ /$searchterm/i;
	}

	$f->run(-3);
	my $w = $f->get("filelist:w") - 4;

	my $code = "{list";
	for (1..$f->get("filelist:h")) {
		last if $#templist < 0;
		my $tempid = int(rand($#templist + 1));
		my $id = $templist[$tempid];
		splice @templist, $tempid, 1;
		my $filename = $files[$id];
		$filename =~ s/.*\/(.*?\/.*?)$/$1/;
		$filename =~ s/.{4,}(.{$w})/...$1/;
		$filename = stfl::quote($filename);
		$code .= "{listitem[file_$id] text:$filename}";
	}
	$code .= "}";

	$f->modify("filelist", "replace_inner", $code);
	$f->set("listpos", "0");
}

sub play
{
	$f->get("listposname") =~ /(\d+)/;
	my $filename = $files[$1];
	$filename =~ s/'/'\\''/g;
	if (system("pidof xmms > /dev/null") != 0) {
		system("xmms -p '$filename' > /dev/null 2>&1 &");
	} else {
		system("xmms -e '$filename' > /dev/null 2>&1 &");
	}
}

sub helpmsg
{
	my $focus = $f->get_focus();

	$f->set("helpmsg", "[ F1 or ENTER = regenerate list | ESC = exit ]")
		if defined $focus and $focus eq "search";

	$f->set("helpmsg", "[ F1 = regenerate list | ENTER = send to xmms | ESC = exit ]")
		if defined $focus and $focus eq "filelist";
}

newlist;
helpmsg;

while (1)
{
	my $event = $f->run(0);
	my $focus = $f->get_focus();
	helpmsg;

	next unless defined $event;
	newlist if $event eq "F1";
	newlist if $event eq "ENTER" and defined $focus and $focus eq "search";
	play if $event eq "ENTER" and defined $focus and $focus eq "filelist";
	last if $event eq "ESC";
}