Problem populating yad right pane with text after selection

57 views
Skip to first unread message

Stephan Schutter

unread,
Aug 10, 2024, 3:42:39 PM8/10/24
to yad-c...@googlegroups.com
I am trying to make a documentation module for a script library. The list_loaded_internal_functions --yformat will set a variable array with each function name in it. The works as you can see from the screenshot. However, making a selection in the left pane does nothing.

How can I make it so that the documentation text shows in the right pane?

Screenshot:
image.png
Relevant code:
# Ensure the temporary file exists before the YAD panes try to access it
touch /tmp/yad_doc.txt
echo "Documentation will appear here" > /tmp/yad_doc.txt

# Function to generate the list of functions and descriptions
list_loaded_internal_functions --yformat

# Define the key to connect the panes
YAD_KEY=$$

# Start the text-info pane (right side) with no-markup option
yad --text-info --no-markup --text="Documentation:" --filename=/tmp/yad_doc.txt --plug=$YAD_KEY --tabnum=2 --margins=10 --wrap --tail &

# Capture the process ID of the text pane
text_pane_pid=$!

# Start the list pane (left side)
yad --list --no-markup --title="Select Function" --text="Functions:" --column="Number" --column="Function" --column="Description" \
--button="Quit:1" --button="Select:0" --center --print-column=2 --plug=$YAD_KEY --tabnum=1 \
"${functions_list[@]}" | while IFS='|' read -r selected_function; do
echo "Selected function: $selected_function"
if [ -z "$selected_function" ]; then
echo "No function selected, continuing..."
continue
fi
# Clean up the selected function name
selected_function="${selected_function%%|*}" # Strip any trailing '|' and spaces
echo "Cleaned function name: $selected_function"
# Fetch the documentation for the selected function using `sed`
result=$(sed -n "/^${selected_function}() {/,/# End of documentation/p" "${0}")

echo "Fetched documentation: $result"
# Send the documentation to the text-info pane by writing it to the file
echo "$result" > /tmp/yad_doc.txt
touch /tmp/yad_doc.txt # Force refresh the text-info pane
done &

# Start the paned window to combine the above two
yad --paned --title="Select a function below to see the function's documentation and usage:" \
--key=$YAD_KEY --orient=hor --width="${ZWIDTH:-1200}" --height="${ZHEIGHT:-600}" --splitter=300 --listen

# Clean up on exit
kill $text_pane_pid




Kind regards
___________________
) 
¸.·´¸.·*´¨) 
(¸.·´ (¸. 
Stephan

 
Stephan Schutter

Cell:     +46 70 641 3983



This message is for the designated recipient only and may contain privileged, proprietary, or otherwise private information. If you have received it in error, please notify the sender immediately and delete the original. Any other use of the email by you is prohibited.

Robert Cooper

unread,
Aug 10, 2024, 4:47:44 PM8/10/24
to yad-common
just preceed with tail command and filename to your first paned yad window like this:

tail -f /tmp/yad_doc.txt | yad --text-info --no-markup --text="Documentation:" --filename=/tmp/yad_doc.txt --plug=$YAD_KEY --tabnum=2 --margins=10 --wrap --tail &

Then when selection is made, text should be written to /tmp/yad_doc.txt which would then be displayed in your paned "yad --text-info window"

hope this helps.

Stephan Schutter

unread,
Aug 10, 2024, 5:55:26 PM8/10/24
to yad-c...@googlegroups.com
Well  i have been working on this and have gotten to a point where it almost works... only I must make the selection twice for anything to show in the documentation pane.

screenshot:
image.png
current code (ugly):

touch /tmp/yad_doc.txt
echo "Documentation will appear here" > /tmp/yad_doc.txt

list_loaded_internal_functions --yformat

# Define the key to connect the panes
YAD_KEY=$$

display_documentation() {
local selected_function="$1"

# Capture the documentation based on your working code
result=$(while read -r line; do
# Flip the printline flag if the function is found...
if [[ "$line" == "${selected_function}() {"* || "$line" == "${selected_function}()"* || "$line" == "${selected_function}()"* ]]; then
printline="yes"
fi

# If the printline flag is set, print the line...
if [[ "$printline" == "yes" ]]; then
# Flip the success flag if the function was found...
local _req_success=true
if [[ "$line" == "# End of documentation"* || "$line" == *"# End of documentation"* || "$line" == *End*of*Documentation* ]]; then
# If the end of documentation is found, stop printing...
printline="no"
else
# Print the line...
echo "$line" | grep -E '(^|\s*)#\s*' | sed 's/#//'
fi
fi
done < "${0}")

# If no documentation found
if [ -z "$result" ]; then
result="No documentation found for ${selected_function}."
fi

# Clear the file and write the new documentation
: > /tmp/yad_doc.txt
echo "$result" > /tmp/yad_doc.txt
sleep 1
# Manually refresh the right pane content
yad --text-info --no-markup --text="Documentation:" --filename=/tmp/yad_doc.txt --plug=$YAD_KEY --tabnum=2 --margins=10 --wrap --tail &
}

# Start the loop to handle interactions
while true; do
# Start the text-info pane (right side) with no-markup option
yad --text-info --no-markup --text="Documentation:" --filename=/tmp/yad_doc.txt --plug=$YAD_KEY --tabnum=2 --margins=10 --wrap --tail &

# Start the list pane (left side)
yad --list --no-markup --title="Select Function" --text="Functions:" --column="Number" --column="Function" --column="Description" \
--button="Quit:1" --center --print-column=2 --plug=$YAD_KEY --tabnum=1 \
"${functions_list[@]}" | while IFS='|' read -r selected_function; do

if [ -z "$selected_function" ]; then
echo "No function selected, continuing..."
continue
fi

# Clean up the selected function name
selected_function="${selected_function%%|*}" # Strip any trailing '|' and spaces

# Display the selected function's documentation
display_documentation "$selected_function"

done &

# Start the paned window to combine the above two
yad --paned --title="Select a function below to see the function's documentation and usage:" \
--key=$YAD_KEY --orient=hor --width="${ZWIDTH:-1200}" --height="${ZHEIGHT:-600}" --splitter=300 --listen

# Check if the user selected "Quit" or closed the window
if [ $? -ne 0 ]; then
break
fi
done



--
You received this message because you are subscribed to the Google Groups "yad-common" group.
To unsubscribe from this group and stop receiving emails from it, send an email to yad-common+...@googlegroups.com.
To view this discussion on the web visit https://groups.google.com/d/msgid/yad-common/606158ad-882d-475c-85e6-227f6722c1d8n%40googlegroups.com.

Jerry Davis

unread,
Aug 10, 2024, 9:32:15 PM8/10/24
to yad-c...@googlegroups.com

Not meaning to hijack this thread as I am a yad user as well, but an alternative to using yad is to use fzf with preview.


Stephan Schutter

unread,
Aug 11, 2024, 5:00:08 AM8/11/24
to yad-c...@googlegroups.com
that looks like a terminal program? I want a proper GUI. I have a way to do this somewhat in a terminal only with dialog (more limited).

while true; do
exec 3>&1
selection=$(dialog --clear --no-collapse --backtitle "${BRANDING:-" Function library : Script Development Support"}" --title "RTD Library Loaded Functions:" --menu "\n Chose a function below to see more details about the function." "${HEIGHT:-0}" "${WIDTH:-90}" "${LIST_HEIGHT:-0}" $(list_loaded_internal_functions) 2>&1 1>&3)
exit_status=$?
exec 3>&-

case $exit_status in
1) clear && echo [Cancel] && exit ;;
255) clear && echo [ESC] && exit ;;
esac

case $selection in
$selection )
result=$(while IFS= read -r 'line'; do
[[ "$line" == "$selection() {" ]] && printline="yes"
[[ "$line" == "$selection() { " ]] && printline="yes"
[[ "$line" == "# End of documentation"* ]] && printline="no"
[[ "$line" == *"# End of documentation"* ]] && printline="no"
[[ "$line" == *End*of*Documentation* ]] && printline="no"
[[ "$printline" == "yes" ]] && echo "$line" | grep -E '(^|\s*)#\s*' | sed 's/#//'
done < $0 )
dialog --clear --no-collapse --backtitle "${0##*/} :: Library HELP ::" --title "RTD Functon: $selection" --msgbox "$result" "${HEIGHT:-25}" "${WIDTH:-110}"
;;
* ) exit ;;
esac
done
return

Stephan Schutter

unread,
Aug 11, 2024, 6:35:13 AM8/11/24
to yad-c...@googlegroups.com
Ok guys, this code works as intended:

Few minor issues:
- Because window needs relaunch it resizes and returns to the default location on screen after selection
- I must click on the "Refresh" button for the text to appear

i hope I can work this out later.



Screenshot:

image.png


Code:
touch /tmp/yad_doc.txt
echo "Documentation will appear here" > /tmp/yad_doc.txt

# Function to generate the list of functions and descriptions
list_loaded_internal_functions --yformat

# Define the key to connect the panes
YAD_KEY=$$

function display_documentation () {
local selected_function="$1"

# Capture the documentation based on your working code
result=$(while read -r line; do
# Flip the printline flag if the function is found...
if [[ "$line" == "${selected_function}() {"* || "$line" == "${selected_function}()"* || "$line" == "${selected_function}()"* ]]; then
printline="yes"
fi

# If the printline flag is set, print the line...
if [[ "$printline" == "yes" ]]; then
# Flip the success flag if the function was found...
local _req_success=true
if [[ "$line" == "# End of documentation"* || "$line" == *"# End of documentation"* || "$line" == *End*of*Documentation* ]]; then
# If the end of documentation is found, stop printing...
printline="no"
else
# Print the line...
echo "$line" | grep -E '(^|\s*)#\s*' | sed 's/#//'
fi
fi
done < "${0}")

# If no documentation found
if [ -z "$result" ]; then
result="No documentation found for ${selected_function}."
fi

# Write the new documentation to the file
echo "$result" > /tmp/yad_doc.txt
}

# Start the loop to handle interactions
while true; do
# Start the paned window in the background
yad --paned --title="Select a function below to see the function's documentation and usage:" \
--key=$YAD_KEY --orient=hor --width="${ZWIDTH:-1200}" --height="${ZHEIGHT:-600}" --splitter=300 --listen --button="Quit:1" --button="Refresh:0" &

# Capture the YAD process ID
YAD_PID=$!

# Start the text-info pane (right side) with no-markup option
yad --text-info --no-markup --filename=/tmp/yad_doc.txt --plug=$YAD_KEY --tabnum=2 --margins=10 --wrap --no-buttons &

# Start the list pane (left side)
yad --list --no-markup --title="Select Function" --text="Functions:" --column="Number" --column="Function" --column="Description" \
--center --print-column=2 --plug=$YAD_KEY --tabnum=1 \
"${functions_list[@]}" | while IFS='|' read -r selected_function; do

if [ -z "$selected_function" ]; then
echo "No function selected, continuing..."
continue
fi

# Clean up the selected function name
selected_function="${selected_function%%|*}" # Strip any trailing '|' and spaces

# Display the selected function's documentation
display_documentation "$selected_function"

done

# Capture the exit status of the YAD process
wait $YAD_PID
exit_status=$?

# Check if the user selected "Quit" or closed the window
if [ $exit_status -eq 1 ] || [ $exit_status -eq 252 ]; then
# Kill all YAD processes
pkill yad
break
fi
done



On Sun, Aug 11, 2024 at 3:32 AM Jerry Davis <wf5...@gmail.com> wrote:

Ananas

unread,
Aug 11, 2024, 7:04:22 AM8/11/24
to yad-c...@googlegroups.com
hi

1. use named pipes and stdin instead of regular files in text dialog
2. add double-click or select handler to list dialog

here is the similar example of what you need - https://github.com/v1cont/yad/wiki/Man-pages-browser

сб, 10 серп. 2024 р. о 22:42 Stephan Schutter <stephan....@gmail.com> пише:
--
You received this message because you are subscribed to the Google Groups "yad-common" group.
To unsubscribe from this group and stop receiving emails from it, send an email to yad-common+...@googlegroups.com.


--
wbr,

Victor "Ananas" Ananjevsky
Reply all
Reply to author
Forward
0 new messages