Last active
March 9, 2025 20:11
-
-
Save boydaihungst/9666663f5a6c2d6a017e280767543825 to your computer and use it in GitHub Desktop.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
# REMOVE THIS LINE -- $HOME/.config/xdg-desktop-portal-termfilechooser/config | |
[filechooser] | |
cmd=$HOME/.config/xdg-desktop-portal-termfilechooser/yazi-wrapper.sh | |
default_dir=$HOME/Downloads |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
#!/bin/sh | |
set -x | |
# This wrapper script is invoked by xdg-desktop-portal-termfilechooser. | |
# | |
# Inputs: | |
# 1. "1" if multiple files can be chosen, "0" otherwise. | |
# 2. "1" if a directory should be chosen, "0" otherwise. | |
# 3. "0" if opening files was requested, "1" if writing to a file was | |
# requested. For example, when uploading files in Firefox, this will be "0". | |
# When saving a web page in Firefox, this will be "1". | |
# 4. If writing to a file, this is recommended path provided by the caller. For | |
# example, when saving a web page in Firefox, this will be the recommended | |
# path Firefox provided, such as "~/Downloads/webpage_title.html". | |
# Note that if the path already exists, we keep appending "_" to it until we | |
# get a path that does not exist. | |
# 5. The output path, to which results should be written. | |
# | |
# Output: | |
# The script should print the selected paths to the output path (argument #5), | |
# one path per line. | |
# If nothing is printed, then the operation is assumed to have been canceled. | |
multiple="$1" | |
directory="$2" | |
save="$3" | |
path="$4" | |
out="$5" | |
cmd="yazi" | |
# "wezterm start --always-new-process" if you use wezterm | |
termcmd="${TERMCMD:-kitty --title 'termfilechooser'}" | |
# change this to "/tmp/xxxxxxx/.last_selected" if you only want to save last selected location | |
# in session (flushed after reset device) | |
last_selected_path_cfg="${XDG_STATE_HOME:-$HOME/.local/state}/xdg-desktop-portal-termfilechooser/last_selected" | |
mkdir -p "$(dirname "$last_selected_path_cfg")" | |
if [ ! -f "$last_selected_path_cfg" ]; then | |
touch "$last_selected_path_cfg" | |
fi | |
last_selected="$(cat "$last_selected_path_cfg")" | |
# Restore last selected path | |
if [ -d "$last_selected" ]; then | |
save_to_file="" | |
if [ "$save" = "1" ]; then | |
save_to_file="$(basename "$path")" | |
path="${last_selected}/${save_to_file}" | |
else | |
path="${last_selected}" | |
fi | |
fi | |
if [ -z "$path" ]; then | |
path="$HOME" | |
fi | |
if [ "$save" = "1" ]; then | |
tmpfile=$(mktemp) | |
# Save/download file | |
printf '%s' 'xdg-desktop-portal-termfilechooser saving files tutorial | |
!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!! | |
!!! === WARNING! === !!! | |
!!! The contents of *whatever* file you open last in !!! | |
!!! yazi will be *overwritten*! !!! | |
!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!! | |
Instructions: | |
1) Move this file wherever you want. | |
2) Rename the file if needed. | |
3) Confirm your selection by opening the file, for | |
example by pressing <Enter>. | |
Notes: | |
1) This file is provided for your convenience. You | |
could delete it and choose another file to overwrite | |
that. | |
2) If you quit yazi without opening a file, this file | |
will be removed and the save operation aborted. | |
' >"$path" | |
set -- --chooser-file="$tmpfile" --cwd-file="$last_selected_path_cfg" "$path" | |
elif [ "$directory" = "1" ]; then | |
# upload files from a directory | |
set -- --chooser-file="$out" --cwd-file="$last_selected_path_cfg" "$path" | |
elif [ "$multiple" = "1" ]; then | |
# upload multiple files | |
set -- --chooser-file="$out" --cwd-file="$last_selected_path_cfg" "$path" | |
else | |
# upload only 1 file | |
set -- --chooser-file="$out" --cwd-file="$last_selected_path_cfg" "$path" | |
fi | |
command="$termcmd $cmd" | |
for arg in "$@"; do | |
# escape double quotes | |
escaped=$(printf "%s" "$arg" | sed 's/"/\\"/g') | |
# escape spaces | |
command="$command \"$escaped\"" | |
done | |
sh -c "$command" | |
# case save file | |
if [ "$save" = "1" ] && [ -s "$tmpfile" ]; then | |
selected_file=$(head -n 1 "$tmpfile") | |
# Check if selected file is placeholder file | |
if [ -f "$selected_file" ] && grep -qi "^xdg-desktop-portal-termfilechooser saving files tutorial" "$selected_file"; then | |
cat "$tmpfile" >"$out" | |
rm -f "$tmpfile" | |
fi | |
fi | |
# Remove file if the save operation aborted | |
if [ "$save" = "1" ] && [ ! -s "$out" ]; then | |
rm "$path" | |
rm -f "$tmpfile" | |
fi |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment