From f9930b146fc5cb2323767e370bc805b8cf4ee24a Mon Sep 17 00:00:00 2001 From: Mattia Mascarello Date: Sat, 27 Apr 2024 22:26:00 +0200 Subject: [PATCH] Added script, license and updated readme --- LICENSE | 21 ++++++++++++ README.md | 20 +++++++++-- nautilus.sh | 96 +++++++++++++++++++++++++++++++++++++++++++++++++++++ 3 files changed, 135 insertions(+), 2 deletions(-) create mode 100644 LICENSE create mode 100644 nautilus.sh diff --git a/LICENSE b/LICENSE new file mode 100644 index 0000000..63bff2d --- /dev/null +++ b/LICENSE @@ -0,0 +1,21 @@ +MIT License + +Copyright (c) 2024 MatMasIt + +Permission is hereby granted, free of charge, to any person obtaining a copy +of this software and associated documentation files (the "Software"), to deal +in the Software without restriction, including without limitation the rights +to use, copy, modify, merge, publish, distribute, sublicense, and/or sell +copies of the Software, and to permit persons to whom the Software is +furnished to do so, subject to the following conditions: + +The above copyright notice and this permission notice shall be included in all +copies or substantial portions of the Software. + +THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR +IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, +FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE +AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER +LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, +OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE +SOFTWARE. diff --git a/README.md b/README.md index 89a7391..4e04232 100644 --- a/README.md +++ b/README.md @@ -1,2 +1,18 @@ -# nautilus_bookmarks_cli -Bashrc script for accessing nautilus bookmarks in the shell +# Nautilus Bookmarks Cli + +The Nautilus Bookmarks Manager is a Bashrc script that allows you to +quickly access Nautilus bookmarks from the command line, by just typing the bookmark name. + +Nautilus is the default file manager for GNOME desktop environments. + +## Usage + +1. **Installation**: + - Clone this repository or download the `nautilus_bookmarks.sh` script. + - Copy the contents of the script to your `.bashrc` file, or source the script in your `.bashrc` file. + - Restart your terminal or run `source ~/.bashrc` to apply the changes. + +2. **Usages**: + - Use the `nbk` or `nautilus_list_bookmarks` command to list all bookmarks. + - Type the name of the bookmark to open the bookmarked directory in Nautilus. + - **Note**: bookmarks that could shadow existing commands will be ignored. diff --git a/nautilus.sh b/nautilus.sh new file mode 100644 index 0000000..9aee1ee --- /dev/null +++ b/nautilus.sh @@ -0,0 +1,96 @@ +#!/bin/bash + +# Location of the Nautilus bookmarks file +bookmark_file="$HOME/.config/gtk-3.0/bookmarks" + +# Function to check if alias name conflicts with any existing commands or aliases +check_alias_conflict() { + local alias_name="$1" + if command -v "$alias_name" >/dev/null 2>&1; then + return 0 + else + return 1 + fi +} + + +# table to store bookmark aliases (alias_name -> bookmark_path) +declare -A bookmark_aliases + +# Function to list Nautilus bookmarks +nautilus_list_bookmarks() { + + # Check if the bookmarks file exists + if [ ! -f "$bookmark_file" ]; then + echo "Nautilus bookmarks file not found." + exit 1 + fi + + #loop through bookmark_aliases + + # Define colors and styling + bold=$(tput bold) + normal=$(tput sgr0) + cyan=$(tput setaf 6) + yellow=$(tput setaf 3) + + # Print header with styling + echo "${bold}${cyan}Nautilus Bookmarks:${normal}" + + # Loop through bookmark aliases and print each with styling + for alias_name in "${!bookmark_aliases[@]}"; do + bookmark_path="${bookmark_aliases[$alias_name]}" + printf " ${yellow}%s${normal} -> ${cyan}%s${normal}\n" "$alias_name" "$bookmark_path" + done +} + +# Check if the bookmarks file exists +if [ ! -f "$bookmark_file" ]; then + exit 1 +fi + +# Loop through each line in the bookmarks file +while IFS= read -r line; do + + bookmark_path=$(echo "$line" | awk -F ' ' '{print $1}') + bookmark_name=$(echo "$line" | awk '{$1=""; print $0}' | xargs) + + folder_name=$(basename "$bookmark_path") + + + if [ -z "$folder_name" ]; then + continue + fi + + + alias_name="" + if [ -n "$bookmark_name" ]; then + # use bookmark name if available, otherwise use folder name + alias_name=$(echo "$bookmark_name" | sed 's/[^a-zA-Z0-9]/_/g') + if check_alias_conflict "$alias_name"; then + alias_name=$(echo "$folder_name" | sed 's/[^a-zA-Z0-9]/_/g') + fi + else + alias_name=$(echo "$folder_name" | sed 's/[^a-zA-Z0-9]/_/g') + fi + + + if check_alias_conflict "$alias_name"; then + # no way to solve the conflict + continue + fi + + + # Check if the bookmark starts with "file://" + if [[ $bookmark_path == file://* ]]; then + bookmark_aliases["$alias_name"]="${bookmark_path#file://}" + alias "$alias_name"="cd '${bookmark_path#file://}'" + else + bookmark_aliases["$alias_name"]="$bookmark_path" + alias "$alias_name"="nautilus '$bookmark_path'" + fi +done < "$bookmark_file" + +# Alias to list Nautilus bookmarks +alias nbk="nautilus_list_bookmarks" +