1# desksh utility functions.
2
3typeset -A text_output
4typeset -A text_destructors
5
6popup=(
7	t=(
8		text=( w=text o='-relief raised -bd 2 -yscrollcommand "${parwindow}.scroll set"' s='text_output[$winid]=${window}' )
9		scroll=( w=scrollbar c='${text_output[$winid]} yview' )
10
11		pack=( text scroll )
12		opts="-side left -fill y"
13		)
14	ok=( w=button t="Ok" c='unset text_output[$winid]; ${destructor:-destroy} ${root}' )
15
16	pack=( t ok )
17	opts="-side top"
18	framecmd=toplevel
19	)
20
21# expects:
22#	$1 - A window identifier (string) of a window set up by
23#		text_init_window
24#
25# action:
26#	Puts the window specified on the screen.
27#
28# returns:
29#	2 if the id given is already on the screen.
30#	1 if the id given was not set initialized previously
31#	0 upon success.
32#
33function text_open_window {
34	typeset winid=$1
35	[[ ${text_winnames[$winid]} == "" ]] && return 1
36	[[ ${text_output[$winid]} != "" ]] && return 2
37
38	generate ${text_winnames[$winid]} popup "" \
39		"destructor=${text_destructors[$winid]} winid=${winid}"
40
41	[[ ${text_initial[$winid]} != "" ]] && \
42		text_winput $winid "${text_initial[$winid]}"
43
44}
45
46# expects:
47#	$1 - A window identifier (string)
48#	$2 - The (tk) name of the window to associate with it
49#	$3 - Optional destructor to use in place of tk's "destroy."
50#	$4 - Optional inital text you want to appear in the window
51#		when it is finally mapped.
52#
53# action:
54#	if the given window doesn't exist, set it up so that will when
55#	text_open_window is called; if the given window is already
56#	displayed, clear the window and put in any initial text.
57#
58function text_init_window {
59	typeset winid=$1
60	text_initial[$winid]="$4"
61	if [[ ${text_output[$winid]} != "" ]]
62	then
63		${text_output[$winid]} delete 1.0 end
64
65		[[ $4 != "" ]] && text_winput $winid "${text_initial[$winid]}"
66	else
67		text_winnames[$winid]=$2
68		text_destructors[$winid]=${3:-destroy}
69	fi
70}
71
72
73# expects:
74#	$1 - A window identifier for a window that has been initialized
75#		with text_init_window
76#	$2 - Text to display.
77#
78# action:
79#	Outputs the text given to the window, automatically scrolling the
80#	window, unless the user moves the cursor.
81#
82# returns:
83#	1 if the window identifier is not valid
84#	0 upon success
85#
86function text_winput {
87	typeset winid=$1
88	if [[ ${text_output[$winid]} == "" ]]
89	then
90		text_open_window $winid || return 1
91	fi
92	${text_output[$winid]} insert end "${*:2}"$'\n'
93	${text_output[$winid]} yview insert
94}
95
96
97# does the obvious - convenient since ++ does not work with
98function addone { nameref var=$1; var=$((var+1)); }
99
100
101# expects:
102#	-p - An open pipe.
103#	$1 - A filter to apply to the data.  default: echo
104#	$2 - Command used to display the data default: echo
105#	$3 - Command to run upon termination of the pipe. default: ':'
106#	$4 - Command run before data is displayed.
107#
108# action:
109#	reads from the pipe, it filters each line read and if the filter
110#	returns success, executes
111#
112function text_read_pipe {
113	typeset result
114	while read -p -r var			# blocking on read lets tk do work
115	do
116		if result=$(${1:-echo} $var)	# filter out info not displayed
117		then
118			${4:-:}			# optional extra command
119			${2:-echo} $result	# display the result
120		fi
121
122	done
123
124	${3:-:}					# final cleanup
125}
126