From b81fe3adfa01a879efc5b6eae2fed0860097c2ec Mon Sep 17 00:00:00 2001 From: Cristiano Nunes Date: Thu, 12 Dec 2024 10:33:18 -0300 Subject: [PATCH] Add script 'List symbolic links' --- Directories and files/Open item location | 2 +- Link operations/List broken links | 2 +- Link operations/List symbolic links | 26 ++++++++++++++++++++++++ common-functions.sh | 15 +++++++++++--- 4 files changed, 40 insertions(+), 5 deletions(-) create mode 100755 Link operations/List symbolic links diff --git a/Directories and files/Open item location b/Directories and files/Open item location index 7a530d25..a2726f7a 100755 --- a/Directories and files/Open item location +++ b/Directories and files/Open item location @@ -14,7 +14,7 @@ _main() { input_files=$(_get_files "par_type=all; par_max_items=20; par_get_pwd=true") # Run the main process. - _open_items_locations "$input_files" + _open_items_locations "$input_files" "true" } _main "$@" diff --git a/Link operations/List broken links b/Link operations/List broken links index e74c0d26..29197bda 100755 --- a/Link operations/List broken links +++ b/Link operations/List broken links @@ -20,7 +20,7 @@ _main() { std_output=$(_text_remove_pwd "$std_output") - _display_list_box "$std_output" "--column=Link" "broken links" + _display_list_box "$std_output" "--column=Link" "broken links" "false" } _main "$@" diff --git a/Link operations/List symbolic links b/Link operations/List symbolic links new file mode 100755 index 00000000..a1448bee --- /dev/null +++ b/Link operations/List symbolic links @@ -0,0 +1,26 @@ +#!/usr/bin/env bash + +# Source the script 'common-functions.sh'. +SCRIPT_DIR=$(cd -- "$(dirname -- "${BASH_SOURCE[0]}")" &>/dev/null && pwd) +ROOT_DIR=$(grep --only-matching "^.*scripts[^/]*" <<<"$SCRIPT_DIR") +source "$ROOT_DIR/common-functions.sh" + +_main() { + local input_files="" + local std_output="" + + # Execute initial checks. + _check_dependencies "" + _display_wait_box "2" + input_files=$(_get_files "par_type=all; par_get_pwd=true") + + # Run the main process. + # shellcheck disable=SC2086 + std_output=$(find $input_files -type l) + + std_output=$(_text_remove_pwd "$std_output") + + _display_list_box "$std_output" "--column=Link" "symbolic links" "false" +} + +_main "$@" diff --git a/common-functions.sh b/common-functions.sh index 694ed993..2131c8fc 100644 --- a/common-functions.sh +++ b/common-functions.sh @@ -339,10 +339,15 @@ _display_list_box() { # format "--column=;--column=". # - $3 (item_name): A string representing the name of the items in the # list. If not provided, the default value is "items". + # - $4 (resolve_links): A boolean-like string ("true" or "false") + # indicating whether symbolic links in item paths should be resolved to + # their target locations when opening the item's location. Defaults to + # "true". local message=$1 local columns=$2 local item_name=${3:-"items"} + local resolve_links=${4:-"true"} local columns_count=0 local items_count=0 local selected_item="" @@ -376,7 +381,7 @@ _display_list_box() { if ((items_count != 0)) && [[ -n "$selected_item" ]]; then # Open the directory of the clicked item in the list. - _open_items_locations "$selected_item" + _open_items_locations "$selected_item" "$resolve_links" fi elif _command_exists "kdialog"; then columns=$(sed "s|--column=||g" <<<"$columns") @@ -1382,8 +1387,12 @@ _open_items_locations() { # Parameters: # - $1: (items): A space-separated list of file or directory paths whose # locations will be opened. Paths can be relative or absolute. + # - $2: (resolve_links): A boolean-like string ("true" or "false") + # indicating whether symbolic links in the provided paths should be + # resolved to their target locations before opening. local items=$1 + local resolve_links=$2 local dir="" if [[ -z "$items" ]]; then @@ -1416,7 +1425,7 @@ _open_items_locations() { continue fi - if [[ -L "$item" ]] && [[ -e "$item" ]]; then + if [[ "$resolve_links" == "true" ]] && [[ -L "$item" ]]; then item=$(readlink -f "$item") fi items_open+="$item$FIELD_SEPARATOR" @@ -1925,7 +1934,7 @@ _validate_file_mime() { # MIME type pattern. # # Parameters: - # - $1 (input_file): The path to the file that is being validated. This + # - $1 (input_file): The path to the file that is being validated. This # is the file whose MIME type will be checked. # - $2 (par_select_mime): The MIME type pattern (or regular expression) # to compare the file's MIME type against. If no MIME type pattern is