How to use same function to Entry and Tree with different input?

For discussions about programming, and for programming questions and advice


Moderator: Forum moderators

Post Reply
recobayu
Posts: 64
Joined: Tue Jul 14, 2020 9:34 am
Has thanked: 11 times
Been thanked: 8 times

How to use same function to Entry and Tree with different input?

Post by recobayu »

Hi All.
I have a simple script to search applications. It use function infoku, infoku-pilih, jalan, and jalan-tree.
Actually, infoku and infoku-pilih is information about Comment and Name of application, but different on the input. infoku just read top of tree, but infoku-pilih read from selection of tree.
jalan and jalan-tree is to run the exec, but jalan read the top of tree, and jalan-tree read from selection of tree.
Is it possible to just use two function info and jalan? How? Thank you.
This is my code

Code: Select all

#!/bin/bash
# 211015 simple muksearch
export wd=/tmp/sku	#working directory

mkdir -p "$wd"
ls /usr/share/applications/*.desktop>"$wd"/apps_file
cut -d\/ -f5 "$wd"/apps_file|sed 's/\.desktop//' > "$wd"/apps_tree

cari(){
	grep -ie "Name=.*$e" -e "Comment=.*$e" -e "Exec=.*$e" /usr/share/applications/* -l > "$wd"/apps_cari
	cut -d\/ -f5 "$wd"/apps_cari |sed 's/\.desktop//' > "$wd"/apps_tree
	
	app_top=`head -1 "$wd"/apps_cari`
	infoku "$app_top"
}
export -f cari

infoku(){
	app_name=`grep -ie "^Name=" "$1"|cut -d\= -f2`
	printf "<b>$app_name</b>" > "$wd"/app_name
	grep -ie "^Comment=" "$1"|cut -d\= -f2|tr -d '\n' > "$wd"/app_comment
	icon=`grep -ie "^Icon=" "$1"|cut -d\= -f2`
	#~ echo icon = "$icon"
	if [[ "$icon" == *\/* ]];then
		ln -sf "$icon" "$wd"/app_icon
	else
		ic=`find /usr/share/pixmaps/ /usr/share/icons/ -type f -name "$icon*" -print|tail -1`
		#~ echo "$ic"
		if [ "$ic" == "" ];then
			ln -sf /usr/share/pixmaps/deb_logo.png "$wd"/app_icon
		else
			ln -sf "$ic" "$wd"/app_icon
		fi
	fi
}
export -f infoku

infoku-pilih(){
	app=/usr/share/applications/"$t".desktop
	app_name=`grep -ie "^Name=" "$app"|cut -d\= -f2`
	printf "<b>$app_name</b>" > "$wd"/app_name
	grep -ie "^Comment=" "$app"|cut -d\= -f2|tr -d '\n' > "$wd"/app_comment
	icon=`grep -ie "^Icon=" "$app"|cut -d\= -f2`
	#~ echo icon = "$icon"
	if [[ "$icon" == *\/* ]];then
		ln -sf "$icon" "$wd"/app_icon
	else
		ic=`find /usr/share/pixmaps/ /usr/share/icons/ -type f -name "$icon*" -print|tail -1`
		#~ echo "$ic"
		if [ "$ic" == "" ];then
			ln -sf /usr/share/pixmaps/deb_logo.png "$wd"/app_icon
		else
			ln -sf "$ic" "$wd"/app_icon
		fi
	fi
}
export -f infoku-pilih

jalan(){
	app=`head -1 "$wd"/apps_cari`
	#~ echo "$app"
	di_terminal=`grep -ie "^Terminal=true" "$app"`
	exe=`grep -e "Exec=" "$app"|cut -d\= -f2|sed 's/%.*//'`
	if [ "$di_terminal" ];then
		x-terminal-emulator -e "$exe"
	else
		eval "$exe"&
	fi
}
export -f jalan

jalan-tree(){
	app=/usr/share/applications/"$t".desktop
	#~ echo "$app"
	di_terminal=`grep -ie "^Terminal=true" "$app"`
	exe=`grep -e "Exec=" "$app"|cut -d\= -f2|sed 's/%.*//'`
	if [ "$di_terminal" ];then
		x-terminal-emulator -e "$exe"
	else
		eval "$exe"&
	fi
}
export -f jalan-tree

export guisku='
<window height-request="400" width-request="400" title="Search app" \
icon-name="gtk-find" window_position="1" skip_taskbar_hint="true">
<vbox>
	<entry>
		<action>cari</action>
		<action>refresh:t</action>
		<action>refresh:txt_name</action>
		<action>refresh:txt_comment</action>
		<action signal="activate">jalan</action>
		<action signal="activate">EXIT:exit</action>
		<variable>e</variable>
	</entry>
	<tree rules-hint="true" headers-visible="false" hover-selection="true">
		<action signal="button-release-event">jalan-tree</action>
		<action signal="button-release-event">EXIT:exit</action>
		<action signal="cursor_changed">infoku-pilih</action>
		<action signal="cursor_changed">refresh:txt_name</action>
		<action signal="cursor_changed">refresh:txt_comment</action>
		<action signal="cursor_changed">refresh:b</action>
		<action signal="button-release-event">jalan-tree</action>
		<action>jalan-tree</action>
		<action>EXIT:exit</action>
		<input file>'"$wd"'/apps_tree</input>
		<variable>t</variable>
	</tree>
	<hbox>
		
		<vbox>
			<text xalign="1" use-markup="true">
				<input file>'"$wd"'/app_name</input>
				<variable>txt_name</variable>
			</text>
			<text xalign="1">
				<input file>'"$wd"'/app_comment</input>
				<variable>txt_comment</variable>
			</text>
		</vbox>
		<button relief="2">
			<width>32</width>
			<input file>'"$wd"'/app_icon</input>
			<variable>b</variable>
		</button>
	</hbox>
</vbox>
<action signal="key-press-event" condition="command_is_true([ $KEY_SYM = Escape ] && echo true )">exit:EXIT</action>
</window>'

aktif=`pgrep -f guisku`
if [ "$aktif" != "" ];then 
	kill $aktif
else
	gtkdialog -p guisku
fi

Attachments
sku.gz
remove fake .gz
(3.78 KiB) Downloaded 46 times
User avatar
snoring_cat
Posts: 206
Joined: Tue Sep 21, 2021 3:40 pm
Location: Earth
Has thanked: 24 times
Been thanked: 46 times

Re: How to use same function to Entry and Tree with different input?

Post by snoring_cat »

It looks like you are working on an alternate version of pRun.

I think you should keep the separate functions, in case you want to set more variables. However, redundant code was put into new functions, "dostart" and "doinfo"

Code: Select all

#!/bin/bash
# 211015 simple muksearch
export wd=/tmp/sku	#working directory

mkdir -p "$wd"
ls /usr/share/applications/*.desktop>"$wd"/apps_file
cut -d\/ -f5 "$wd"/apps_file|sed 's/\.desktop//' > "$wd"/apps_tree

cari(){
	grep -ie "Name=.*$e" -e "Comment=.*$e" -e "Exec=.*$e" /usr/share/applications/* -l > "$wd"/apps_cari
	cut -d\/ -f5 "$wd"/apps_cari |sed 's/\.desktop//' > "$wd"/apps_tree

	app_top=`head -1 "$wd"/apps_cari`
	infoku "$app_top"
}
export -f cari

doinfo(){
	app_name=`grep -ie "^Name=" "$1"|cut -d\= -f2`
	printf "<b>$app_name</b>" > "$wd"/app_name
	grep -ie "^Comment=" "$1"|cut -d\= -f2|tr -d '\n' > "$wd"/app_comment
	icon=`grep -ie "^Icon=" "$1"|cut -d\= -f2`
	#~ echo icon = "$icon"
	if [[ "$icon" == *\/* ]];then
		ln -sf "$icon" "$wd"/app_icon
	else
		ic=`find /usr/share/pixmaps/ /usr/share/icons/ -type f -name "$icon*" -print|tail -1`
		#~ echo "$ic"
		if [ "$ic" == "" ];then
			ln -sf /usr/share/pixmaps/deb_logo.png "$wd"/app_icon
		else
			ln -sf "$ic" "$wd"/app_icon
		fi
	fi
}
export -f doinfo

infoku(){
	doinfo $1
}
export -f infoku

infoku-pilih(){
	app=/usr/share/applications/"$t".desktop
	doinfo $app
}
export -f infoku-pilih

RUNNING=0
export RUNNING

function dostart(){
	if [ $RUNNING -eq 0 ]; then
		RUNNING=1

		di_terminal=`grep -ie "^Terminal=true" "$1"`
		exe=`grep -e "Exec=" "$1"|cut -d\= -f2|sed 's/%.*//'`

		if [ "$di_terminal" ];then
			x-terminal-emulator -e "$exe"
		else
			eval "$exe"&
			exit
		fi
	fi
}
export -f dostart

jalan(){
	app=`head -1 "$wd"/apps_cari`
	dostart $app
}
export -f jalan

jalan-tree(){
	app=/usr/share/applications/"$t".desktop
	dostart $app
}
export -f jalan-tree
export guisku='
<window height-request="400" width-request="400" title="Search app" \
icon-name="gtk-find" window_position="1" skip_taskbar_hint="true">
<vbox>
	<entry>
		<action>cari</action>
		<action>refresh:t</action>
		<action>refresh:txt_name</action>
		<action>refresh:txt_comment</action>
		<action signal="activate">jalan</action>
		<action signal="activate">EXIT:exit</action>
		<variable>e</variable>
	</entry>
	<tree rules-hint="true" headers-visible="false" hover-selection="true">
		<action signal="cursor_changed">infoku-pilih</action>
		<action signal="button-release-event">jalan-tree</action>
		<action signal="button-release-event">EXIT:exit</action>
		<action signal="cursor_changed">refresh:txt_name</action>
		<action signal="cursor_changed">refresh:txt_comment</action>
		<action signal="cursor_changed">refresh:b</action>
		<action>jalan-tree</action>
		<action>EXIT:exit</action>
		<input file>'"$wd"'/apps_tree</input>
		<variable>t</variable>
	</tree>
	<hbox>

		<vbox>
			<text xalign="1" use-markup="true">
				<input file>'"$wd"'/app_name</input>
				<variable>txt_name</variable>
			</text>
			<text xalign="1">
				<input file>'"$wd"'/app_comment</input>
				<variable>txt_comment</variable>
			</text>
		</vbox>
		<button relief="2">
			<width>32</width>
			<input file>'"$wd"'/app_icon</input>
			<variable>b</variable>
		</button>
	</hbox>
</vbox>
<action signal="key-press-event" condition="command_is_true([ $KEY_SYM = Escape ] && echo true )">exit:EXIT</action>
</window>'

aktif=`pgrep -f guisku`
if [ "$aktif" != "" ];then
	kill $aktif
else
	gtkdialog -p guisku
fi

Meeeooow!

-- substance over noise, since 5 minutes in the future --

Mia_White
Posts: 8
Joined: Fri Apr 01, 2022 5:46 pm

Re: How to use same function to Entry and Tree with different input?

Post by Mia_White »

snoring_cat wrote: Sun Oct 17, 2021 4:10 pm

It looks like you are working on an alternate version of pRun.

I think you should keep the separate functions, in case you want to set more variables. However, redundant code was put into new functions, "dostart" and "doinfo"

Code: Select all

#!/bin/bash
# 211015 simple muksearch
export wd=/tmp/sku	#working directory

mkdir -p "$wd"
ls /usr/share/applications/*.desktop>"$wd"/apps_file
cut -d\/ -f5 "$wd"/apps_file|sed 's/\.desktop//' > "$wd"/apps_tree

cari(){
	grep -ie "Name=.*$e" -e "Comment=.*$e" -e "Exec=.*$e" /usr/share/applications/* -l > "$wd"/apps_cari
	cut -d\/ -f5 "$wd"/apps_cari |sed 's/\.desktop//' > "$wd"/apps_tree

	app_top=`head -1 "$wd"/apps_cari`
	infoku "$app_top"
}
export -f cari

doinfo(){
	app_name=`grep -ie "^Name=" "$1"|cut -d\= -f2`
	printf "<b>$app_name</b>" > "$wd"/app_name
	grep -ie "^Comment=" "$1"|cut -d\= -f2|tr -d '\n' > "$wd"/app_comment
	icon=`grep -ie "^Icon=" "$1"|cut -d\= -f2`
	#~ echo icon = "$icon"
	if [[ "$icon" == *\/* ]];then
		ln -sf "$icon" "$wd"/app_icon
	else
		ic=`find /usr/share/pixmaps/ /usr/share/icons/ -type f -name "$icon*" -print|tail -1`
		#~ echo "$ic"
		if [ "$ic" == "" ];then
			ln -sf /usr/share/pixmaps/deb_logo.png "$wd"/app_icon
		else
			ln -sf "$ic" "$wd"/app_icon
		fi
	fi
}
export -f doinfo

infoku(){
	doinfo $1
}
export -f infoku

infoku-pilih(){
	app=/usr/share/applications/"$t".desktop
	doinfo $app
}
export -f infoku-pilih

RUNNING=0
export RUNNING

function dostart(){
	if [ $RUNNING -eq 0 ]; then
		RUNNING=1

		di_terminal=`grep -ie "^Terminal=true" "$1"`
		exe=`grep -e "Exec=" "$1"|cut -d\= -f2|sed 's/%.*//'`

		if [ "$di_terminal" ];then
			x-terminal-emulator -e "$exe"
		else
			eval "$exe"&
			exit
		fi
	fi
}
export -f dostart

jalan(){
	app=`head -1 "$wd"/apps_cari`
	dostart $app
}
export -f jalan

jalan-tree(){
	app=/usr/share/applications/"$t".desktop
	dostart $app
}
export -f jalan-tree
export guisku='
<window height-request="400" width-request="400" title="Search app" \
icon-name="gtk-find" window_position="1" skip_taskbar_hint="true">
<vbox>
	<entry>
		<action>cari</action>
		<action>refresh:t</action>
		<action>refresh:txt_name</action>
		<action>refresh:txt_comment</action>
		<action signal="activate">jalan</action>
		<action signal="activate">EXIT:exit</action>
		<variable>e</variable>
	</entry>
	<tree rules-hint="true" headers-visible="false" hover-selection="true">
		<action signal="cursor_changed">infoku-pilih</action>
		<action signal="button-release-event">jalan-tree</action>
		<action signal="button-release-event">EXIT:exit</action>
		<action signal="cursor_changed">refresh:txt_name</action>
		<action signal="cursor_changed">refresh:txt_comment</action>
		<action signal="cursor_changed">refresh:b</action>
		<action>jalan-tree</action>
		<action>EXIT:exit</action>
		<input file>'"$wd"'/apps_tree</input>
		<variable>t</variable>
	</tree>
	<hbox>

		<vbox>
			<text xalign="1" use-markup="true">
				<input file>'"$wd"'/app_name</input>
				<variable>txt_name</variable>
			</text>
			<text xalign="1">
				<input file>'"$wd"'/app_comment</input>
				<variable>txt_comment</variable>
			</text>
		</vbox>
		<button relief="2">
			<width>32</width>
			<input file>'"$wd"'/app_icon</input>
			<variable>b</variable>
		</button>
	</hbox>
</vbox>
<action signal="key-press-event" condition="command_is_true([ $KEY_SYM = Escape ] && echo true )">exit:EXIT</action>
</window>'

aktif=`pgrep -f guisku`
if [ "$aktif" != "" ];then
	kill $aktif
else
	gtkdialog -p guisku
fi

i agree with this

recobayu
Posts: 64
Joined: Tue Jul 14, 2020 9:34 am
Has thanked: 11 times
Been thanked: 8 times

Re: How to use same function to Entry and Tree with different input?

Post by recobayu »

Alhamdulillah finally i can use a function (info) in another function (search). And I use $1 as input of function. like this:

Code: Select all

#!/bin/bash

# startm by recobayu
# start menu like in cinnamon
# 230630 start and stop using same key, does not implement history yet
#        support in terminal application like python

export location="/usr/share/applications"
export working_directory="$HOME/.config/startm"

[ ! -d "$working_directory" ] && mkdir -p "$working_directory"


close(){
  pids=`ps ax|grep startm|awk '{print $1}'`
  kill $pids 2> /dev/null
  exit 0
}
export -f close


info(){
  name="$1"
  printf "<b>$name</b>" > "$working_directory/name"
  if [ ! -z "$name" ];then
    filename=`grep -rle "^Name=$name$" "$location"`
    
    grep -e "^Comment=" "$filename"|cut -d\= -f2 |tr -d '\n'> "$working_directory/comment"
    exec=`grep -e "^Exec=" "$filename"|cut -d\= -f2|sed 's/%.//'|head -1`
    echo "$exec" > "$working_directory/exec"
    
    
    in_terminal=`grep -i "^Terminal=true" "$filename"`
    [ ! -z "$in_terminal" ] && echo terminator -e "$exec" > "$working_directory/exec"
    
  else
    echo > "$working_directory/comment"
    echo > "$working_directory/exec"
  fi
}
export -f info

search(){
  word="$1"
  grep -irle "^Name=.*$word.*" -e "^Exec=.*$word.*" -e "^Comment=.*$word.*" "$location"|xargs -d '\n' grep -m 1 -i "^Name="|cut -d \= -f2|sort > "$working_directory/names"
  
  #~ head -10 "$working_directory/history" > "$working_directory/names_temp"
  #~ grep -vf "$working_directory/names_temp" "$working_directory/names" >> "$working_directory/names_temp"
  #~ mv "$working_directory/names_temp" "$working_directory/names"
  
  name_top=`head -1 "$working_directory/names"`
  info "$name_top"
}
export -f search
search "*"



run(){
  
  `cat "$working_directory/exec"` &
  

  #~ name=`cut -d\> -f2 "$working_directory/name"|cut -d\< -f1`
  #~ [ ! -f "$working_directory/history" ] && touch "$working_directory/history"
  #~ echo "$name" > "$working_directory/history_temp"
  #~ grep -v "$name" "$working_directory/history">> "$working_directory/history_temp"
  #~ head -10 "$working_directory/history_temp" > "$working_directory/history"
  
}
export -f run

# gui
echo '
<window title="startm" window_position="1" icon-name="bookmark" decorated="false" skip_taskbar_hint="true">
  <vbox>
    <entry>
      <action>search "$e"</action>
      <action>refresh:t</action>
      <action>refresh:n</action>
      <action>refresh:c</action>
      <action signal="activate">run</action>
      <action signal="activate">EXIT:exit</action>
      <variable>e</variable>
    </entry>
    <tree rules-hint="true" headers-visible="false" hover-selection="true">
      <width>400</width>
      <height>400</height>
      <input file>'"$working_directory/names"'</input>
      <action signal="cursor_changed">info "$t"</action>
      <action signal="cursor_changed">refresh:n</action>
      <action signal="cursor_changed">refresh:c</action>
      <action signal="button-release-event">run</action>
      <action signal="button-release-event">EXIT:exit</action>
      <action>run</action>
      <action>EXIT:exit</action>
      <variable>t</variable>
    </tree>
    <text xalign="0" use-markup="true">
      <input file>'"$working_directory/name"'</input>
      <variable>n</variable>
    </text>
    <text xalign="0" wrap="false" max-width-chars="20">
      <input file>'"$working_directory/comment"'</input>
      <variable>c</variable>
    </text>
  </vbox>
  <action signal="key-press-event" condition="command_is_true([ $KEY_SYM = Escape ] && echo true )">exit:esc</action>
  <action signal="focus-out-event">close</action>
  <variable>startm_window</variable>
</window>' > "$working_directory/gui_startm"

gtkdialog -f "$working_directory/gui_startm"



Aah.. it's been a long time huh.. :D

Attachments
startm.gz
remove the fake ".gz"
(3.57 KiB) Downloaded 17 times
Post Reply

Return to “Programming”