From d6e257584a0a35a3c85eb2cc4d54c52100712cb8 Mon Sep 17 00:00:00 2001 From: johnmarco123 Date: Sun, 3 Nov 2024 15:45:34 -0500 Subject: [PATCH 01/17] added dynamic addition and removal of filepaths, as well as basic usage in readme --- README.md | 42 ++++++++++++++++++++++++++++++++---- tmux-sessionizer | 55 ++++++++++++++++++++++++++++++++++++++++++++---- 2 files changed, 89 insertions(+), 8 deletions(-) diff --git a/README.md b/README.md index ee449b9..eaf1dae 100644 --- a/README.md +++ b/README.md @@ -1,12 +1,46 @@ -## tmux sessionizer +# tmux sessionizer its a script that does everything awesome at all times -## Usage +## Basic usage ```bash tmux-sessionizer [] ``` +By default, tmux-sessionizer holds its state in .tmux-sessionizer.txt in your +$HOME directory, but this can be changed within the top of tmux-sessionizer +itself + +## Options + -a, -add + Adds full filepaths to your tmux-sessionizer list via stdin or + interactive mode. It will automatically reject duplicates, and will + only accept valid absolute filepaths to folders that exist. + + -d, -delete + Opens a multi-select fzf of your current tmux-sessionizer list which + can be used to delete files in your list by selecting either one file + to delete using the enter key, or multiple files using tab to select + multiple files and then enter to delete those files. CTRL-C to exit + + -l, -list + Lists the contents of the tmux-sessionizer list. + +### Example commands +Add all directories within the current working directory to your list. +```bash +find $(pwd) -maxdepth 1 -type d | tmux-sessionizer -a +``` + +Enter fzf and delete folders in your tmux-sessionizer list by selecting them +with tab and then clicking enter +```bash +tmux-sessionizer -d +``` + +List all the directories in your tmux-sessionizer list +```bash +tmux-sessionizer -l +``` + if you execute tmux-sessionizer without any parameters it will assume FZF and try to fuzzy find over a set of directories. - -TODO: waiting on that directory list to be dynamic :) (go a head make pr if you want) diff --git a/tmux-sessionizer b/tmux-sessionizer index fa1bec5..8a71854 100755 --- a/tmux-sessionizer +++ b/tmux-sessionizer @@ -1,4 +1,53 @@ -#!/usr/bin/env bash +#/usr/bin/env bash +#script_dir="$(dirname "$(realpath "$0")")" +script_dir="$HOME" +file_name=".tmux-sessionizer.txt" +full_path="$script_dir/$file_name" + +if [[ ! -e "$full_path" ]]; then + touch "$full_path" +fi + +# Add flags for adding, deleting or listing files +if [[ $# -eq 1 ]]; then + + # add a path to our tmux save file, this accepts stdin and also prevents + # duplicates + if [[ $1 == -add || $1 == -a ]]; then + while read line; do + # ensure that the filepath is absolute and it exists + if [[ -d $line && "$line" = /* ]]; then + if ! grep -qFx $line $full_path; then + echo "$line" >> $full_path + fi + fi + done < "${2:-/dev/stdin}" + + # delete a path from our tmux save file. You can select multiple files to + # delete using the tab key in fzf to select multiple + elif [[ $1 == -delete || $1 == -d ]]; then + while true; do + selected=$(cat "$full_path" | fzf -m) + + if [[ -z "$selected" ]]; then + exit 0 + fi + + count=$(echo "$selected" | wc -l) + + if [[ $count -gt 0 ]]; then + temp_file=$(mktemp) + grep -v -F -x -e "$selected" "$full_path" > "$temp_file" + mv "$temp_file" "$full_path" + fi + done + elif [[ $1 == -list || $1 == -l ]]; then + cat $full_path + fi + exit 0 +fi + +# switching tmux to the files switch_to() { if [[ -z $TMUX ]]; then tmux attach-session -t $1 @@ -22,9 +71,7 @@ hydrate() { if [[ $# -eq 1 ]]; then selected=$1 else - # If someone wants to make this extensible, i'll accept - # PR - selected=$(find ~/ ~/personal ~/personal/dev/env/.config -mindepth 1 -maxdepth 1 -type d | fzf) + selected=$(cat $full_path| fzf) fi if [[ -z $selected ]]; then From a71f9b98e08182456806ec13c95f5e1657ffe251 Mon Sep 17 00:00:00 2001 From: johnmarco123 Date: Sun, 3 Nov 2024 16:52:39 -0500 Subject: [PATCH 02/17] Can now have commands that dynamically populate the list on each call of tmux-sessionizer --- tmux-sessionizer | 24 +++++++++++++++++++++--- 1 file changed, 21 insertions(+), 3 deletions(-) diff --git a/tmux-sessionizer b/tmux-sessionizer index 8a71854..2e851b9 100755 --- a/tmux-sessionizer +++ b/tmux-sessionizer @@ -4,11 +4,25 @@ script_dir="$HOME" file_name=".tmux-sessionizer.txt" full_path="$script_dir/$file_name" +command_created_paths=$(mktemp) +# Any paths that you want to dynamically update on their own, add with +# commands. These paths are not shown in the tmux-sesionizer list, but they +# are selectable with tmux-sessionizer. +commands=( + #example command, add all directories from your home always. + "find $HOME -maxdepth 1 -type d" +); + +for cmd in "${commands[@]}"; do + eval "$cmd" >> $command_created_paths +done + + if [[ ! -e "$full_path" ]]; then touch "$full_path" fi -# Add flags for adding, deleting or listing files +# ADDING OR DELETING FILES if [[ $# -eq 1 ]]; then # add a path to our tmux save file, this accepts stdin and also prevents @@ -17,7 +31,7 @@ if [[ $# -eq 1 ]]; then while read line; do # ensure that the filepath is absolute and it exists if [[ -d $line && "$line" = /* ]]; then - if ! grep -qFx $line $full_path; then + if ! grep -qFx $line $full_path $command_created_paths; then echo "$line" >> $full_path fi fi @@ -46,6 +60,8 @@ if [[ $# -eq 1 ]]; then fi exit 0 fi +# REGULAR USAGE OF TMUX-SESSIONIZER PAST THIS POINT + # switching tmux to the files switch_to() { @@ -71,7 +87,9 @@ hydrate() { if [[ $# -eq 1 ]]; then selected=$1 else - selected=$(cat $full_path| fzf) + # list all filepaths in the tmux-sessionizer list as well as any filepaths + # generated by commands + selected=$(cat $full_path $command_created_paths| fzf) fi if [[ -z $selected ]]; then From 0feb51d8071ecbf59d37a9c243a924d5ce7a7bb8 Mon Sep 17 00:00:00 2001 From: johnmarco123 Date: Sun, 3 Nov 2024 16:56:20 -0500 Subject: [PATCH 03/17] oops forgot to update a comment --- tmux-sessionizer | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/tmux-sessionizer b/tmux-sessionizer index 2e851b9..1b10407 100755 --- a/tmux-sessionizer +++ b/tmux-sessionizer @@ -22,7 +22,7 @@ if [[ ! -e "$full_path" ]]; then touch "$full_path" fi -# ADDING OR DELETING FILES +#adding, deleting or listing the tmux-sessionizer list if [[ $# -eq 1 ]]; then # add a path to our tmux save file, this accepts stdin and also prevents From 9aad501eae5d7a463003892bcdf948026382eba4 Mon Sep 17 00:00:00 2001 From: johnmarco123 Date: Sun, 3 Nov 2024 16:59:42 -0500 Subject: [PATCH 04/17] Added some explanation of how commands can be used with tmux-sessionizer in the readme --- README.md | 4 ++++ 1 file changed, 4 insertions(+) diff --git a/README.md b/README.md index eaf1dae..5a27ede 100644 --- a/README.md +++ b/README.md @@ -10,6 +10,10 @@ By default, tmux-sessionizer holds its state in .tmux-sessionizer.txt in your $HOME directory, but this can be changed within the top of tmux-sessionizer itself +Inside of tmux-sessionizer you can also have commands that will automatically +add filepaths for you on each execution. These files never get saved in the +tmux-sessionizer list as they are dynamically created with the commands. + ## Options -a, -add Adds full filepaths to your tmux-sessionizer list via stdin or From e227b2a689c6fc4118b883e066d075148e63aadc Mon Sep 17 00:00:00 2001 From: johnmarco123 Date: Sun, 3 Nov 2024 17:05:06 -0500 Subject: [PATCH 05/17] fixed bug where you could add the same directory if one had a trailing slash and one did not. --- tmux-sessionizer | 4 +++- 1 file changed, 3 insertions(+), 1 deletion(-) diff --git a/tmux-sessionizer b/tmux-sessionizer index 1b10407..6b1d4aa 100755 --- a/tmux-sessionizer +++ b/tmux-sessionizer @@ -29,7 +29,9 @@ if [[ $# -eq 1 ]]; then # duplicates if [[ $1 == -add || $1 == -a ]]; then while read line; do - # ensure that the filepath is absolute and it exists + # remove trailing slashes before comparing + line="${line%/}" + # ensure that the filepath is absolute and it exists if [[ -d $line && "$line" = /* ]]; then if ! grep -qFx $line $full_path $command_created_paths; then echo "$line" >> $full_path From 6581aaad3544754fccbde35900aaade24b099761 Mon Sep 17 00:00:00 2001 From: johnmarco123 Date: Sun, 3 Nov 2024 17:06:54 -0500 Subject: [PATCH 06/17] updated comment explaining the commands in tmux-sessionizer --- tmux-sessionizer | 3 ++- 1 file changed, 2 insertions(+), 1 deletion(-) diff --git a/tmux-sessionizer b/tmux-sessionizer index 6b1d4aa..3219865 100755 --- a/tmux-sessionizer +++ b/tmux-sessionizer @@ -7,7 +7,8 @@ full_path="$script_dir/$file_name" command_created_paths=$(mktemp) # Any paths that you want to dynamically update on their own, add with # commands. These paths are not shown in the tmux-sesionizer list, but they -# are selectable with tmux-sessionizer. +# are selectable with tmux-sessionizer. Any directories shown from here cannot +# be added with tmux-sessionizer -a commands=( #example command, add all directories from your home always. "find $HOME -maxdepth 1 -type d" From b2d2c55fc5475f85d60a80c48f9f0f3056d5ac04 Mon Sep 17 00:00:00 2001 From: johnmarco123 Date: Sun, 3 Nov 2024 18:38:19 -0500 Subject: [PATCH 07/17] prevention of duplicate entries from commands was added --- tmux-sessionizer | 60 ++++++++++++++++++++++++++++++++++-------------- 1 file changed, 43 insertions(+), 17 deletions(-) diff --git a/tmux-sessionizer b/tmux-sessionizer index 3219865..8efacc3 100755 --- a/tmux-sessionizer +++ b/tmux-sessionizer @@ -1,28 +1,45 @@ #/usr/bin/env bash -#script_dir="$(dirname "$(realpath "$0")")" + +# the directory in which the tmux-sessionizer state will be stored script_dir="$HOME" +# the file where the tmux-sessionizer data will be stored file_name=".tmux-sessionizer.txt" full_path="$script_dir/$file_name" -command_created_paths=$(mktemp) -# Any paths that you want to dynamically update on their own, add with -# commands. These paths are not shown in the tmux-sesionizer list, but they -# are selectable with tmux-sessionizer. Any directories shown from here cannot -# be added with tmux-sessionizer -a +# create the tmux-sessionizer state file that remembers what directories to +# show +if [[ ! -e "$full_path" ]]; then + touch "$full_path" +fi + +# Write any commands here which will add directories that you want to stay +# up-to-date. Since directories are usually added to $HOME it is probably a +# good idea to keep the initial example commands=( - #example command, add all directories from your home always. + # Example command "find $HOME -maxdepth 1 -type d" -); +) + +combined_results="" +# Gather the results of all the commands for cmd in "${commands[@]}"; do - eval "$cmd" >> $command_created_paths + combined_results+=$(eval "$cmd"; echo) done - -if [[ ! -e "$full_path" ]]; then - touch "$full_path" +# if there were any results for the commands we add it to the tmux-sessionizer +# list, but we also ensure no duplicates are added +if [[ -n "$combined_results" ]]; then + temp_file=$(mktemp) + if { echo "$combined_results"; cat "$full_path"; } | sort -u > "$temp_file"; then + mv "$temp_file" "$full_path" + else + echo "Error: Failed to create sorted output." + rm -f "$temp_file" + fi fi + #adding, deleting or listing the tmux-sessionizer list if [[ $# -eq 1 ]]; then @@ -34,7 +51,7 @@ if [[ $# -eq 1 ]]; then line="${line%/}" # ensure that the filepath is absolute and it exists if [[ -d $line && "$line" = /* ]]; then - if ! grep -qFx $line $full_path $command_created_paths; then + if ! grep -qFx $line $full_path ; then echo "$line" >> $full_path fi fi @@ -56,14 +73,25 @@ if [[ $# -eq 1 ]]; then temp_file=$(mktemp) grep -v -F -x -e "$selected" "$full_path" > "$temp_file" mv "$temp_file" "$full_path" + rm $temp_file fi done + # simply lists all paths in your tmux-sessionizer list to standard out elif [[ $1 == -list || $1 == -l ]]; then cat $full_path fi exit 0 fi -# REGULAR USAGE OF TMUX-SESSIONIZER PAST THIS POINT + + +# remove any directories in the tmux-sessionizer list that no longer exist +valid_paths=$(mktemp) +while read -r line; do + if [[ -d "$line" ]]; then + echo "$line" >> $valid_paths + fi +done < "$full_path" +mv "$valid_paths" "$full_path" # switching tmux to the files @@ -90,9 +118,7 @@ hydrate() { if [[ $# -eq 1 ]]; then selected=$1 else - # list all filepaths in the tmux-sessionizer list as well as any filepaths - # generated by commands - selected=$(cat $full_path $command_created_paths| fzf) + selected=$(cat $full_path | fzf) fi if [[ -z $selected ]]; then From bb231ec849be09a9c1809f5a8979bef1f0968a31 Mon Sep 17 00:00:00 2001 From: johnmarco123 Date: Sun, 3 Nov 2024 18:46:03 -0500 Subject: [PATCH 08/17] enabled --cycle mode in all fzf for tmux-sessionizer --- README.md | 2 +- tmux-sessionizer | 4 ++-- 2 files changed, 3 insertions(+), 3 deletions(-) diff --git a/README.md b/README.md index 5a27ede..7f9ff87 100644 --- a/README.md +++ b/README.md @@ -8,7 +8,7 @@ tmux-sessionizer [] By default, tmux-sessionizer holds its state in .tmux-sessionizer.txt in your $HOME directory, but this can be changed within the top of tmux-sessionizer -itself +itself. Inside of tmux-sessionizer you can also have commands that will automatically add filepaths for you on each execution. These files never get saved in the diff --git a/tmux-sessionizer b/tmux-sessionizer index 8efacc3..ceb922f 100755 --- a/tmux-sessionizer +++ b/tmux-sessionizer @@ -61,7 +61,7 @@ if [[ $# -eq 1 ]]; then # delete using the tab key in fzf to select multiple elif [[ $1 == -delete || $1 == -d ]]; then while true; do - selected=$(cat "$full_path" | fzf -m) + selected=$(cat "$full_path" | fzf -m --cycle) if [[ -z "$selected" ]]; then exit 0 @@ -118,7 +118,7 @@ hydrate() { if [[ $# -eq 1 ]]; then selected=$1 else - selected=$(cat $full_path | fzf) + selected=$(cat $full_path | fzf --cycle) fi if [[ -z $selected ]]; then From dc632fff1c94ae87f8752fbd923927e8cabaa0be Mon Sep 17 00:00:00 2001 From: johnmarco123 Date: Sun, 3 Nov 2024 18:49:06 -0500 Subject: [PATCH 09/17] added SAFE COPY --- tmux-sessionizer | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/tmux-sessionizer b/tmux-sessionizer index ceb922f..326101a 100755 --- a/tmux-sessionizer +++ b/tmux-sessionizer @@ -61,7 +61,7 @@ if [[ $# -eq 1 ]]; then # delete using the tab key in fzf to select multiple elif [[ $1 == -delete || $1 == -d ]]; then while true; do - selected=$(cat "$full_path" | fzf -m --cycle) + selected=$(cat "$full_path" | fzf -m --cycle --scroll-off 10) if [[ -z "$selected" ]]; then exit 0 @@ -118,7 +118,7 @@ hydrate() { if [[ $# -eq 1 ]]; then selected=$1 else - selected=$(cat $full_path | fzf --cycle) + selected=$(cat $full_path | fzf --cycle --scroll-off 10) fi if [[ -z $selected ]]; then From 3465f75173b4466ba2747f77a7882eea438512a0 Mon Sep 17 00:00:00 2001 From: johnmarco123 Date: Wed, 6 Nov 2024 21:00:49 -0500 Subject: [PATCH 10/17] oops had a random file... --- TODO.md | 8 ++++++++ tmux-sessionizer | 1 + 2 files changed, 9 insertions(+) create mode 100644 TODO.md diff --git a/TODO.md b/TODO.md new file mode 100644 index 0000000..6ce0292 --- /dev/null +++ b/TODO.md @@ -0,0 +1,8 @@ +* [ ] Make quick command to add the current working directory +* [ ] Make quick command to add all directories in the current working + directory +* [ ] make a separate list for wildcard dirs such as /home/user/* These will + get expanded upon running tmux-sessionizer so they stay up to date. +* [ ] Allow tmux-sessionizer -a to accept a second argument being a path, if it + is a relative path, we simply add it to the relative path list, if it + matches multiple directories we can add it to the wildcard list diff --git a/tmux-sessionizer b/tmux-sessionizer index 326101a..1a5c1a0 100755 --- a/tmux-sessionizer +++ b/tmux-sessionizer @@ -4,6 +4,7 @@ script_dir="$HOME" # the file where the tmux-sessionizer data will be stored file_name=".tmux-sessionizer.txt" +wildcards=".tmux-sessionizer_wildcards.txt" full_path="$script_dir/$file_name" # create the tmux-sessionizer state file that remembers what directories to From 9503a6d215cd12b874ed05bc468f8d17dd927c52 Mon Sep 17 00:00:00 2001 From: johnmarco123 Date: Thu, 7 Nov 2024 07:06:14 -0500 Subject: [PATCH 11/17] rewrite --- README.md | 1 + TODO.md | 24 +++++++++- tmux-sessionizer | 111 ++++++++++++++++++++++------------------------- 3 files changed, 75 insertions(+), 61 deletions(-) diff --git a/README.md b/README.md index 7f9ff87..3be037a 100644 --- a/README.md +++ b/README.md @@ -48,3 +48,4 @@ tmux-sessionizer -l if you execute tmux-sessionizer without any parameters it will assume FZF and try to fuzzy find over a set of directories. +/home/john/personal/tmux-sessionizer/direct_paths_fullpath diff --git a/TODO.md b/TODO.md index 6ce0292..3621966 100644 --- a/TODO.md +++ b/TODO.md @@ -1,8 +1,28 @@ -* [ ] Make quick command to add the current working directory -* [ ] Make quick command to add all directories in the current working +* [X] Make quick command to add the current working directory + +* [X] Make quick command to add all directories in the current working directory + +* [ ] After the quick commands, resolve how to do this when piping an entire + file + +* [ ] handle duplicate entries, maybe do a check before inserting another path + * [ ] make a separate list for wildcard dirs such as /home/user/* These will get expanded upon running tmux-sessionizer so they stay up to date. + +* [ ] Get rid of creating and removing files, do everything with bash arrays + and only use files when it is the only other way + * [ ] Allow tmux-sessionizer -a to accept a second argument being a path, if it is a relative path, we simply add it to the relative path list, if it matches multiple directories we can add it to the wildcard list + +* [ ] Eventually i don't want the storage files to clutter the home, and + instead have a better place to hold their data. Unless maybe the $HOME is + ideal...? idk + +* [ ] Eventually even later, add a separate file for specific commands to run + that are even more specific past wildcards, such as a find command for + example. Or maybe its better to just simply pipe find into + tmux-sessionizer -a and deal with it that way? diff --git a/tmux-sessionizer b/tmux-sessionizer index 1a5c1a0..15904f3 100755 --- a/tmux-sessionizer +++ b/tmux-sessionizer @@ -1,68 +1,49 @@ #/usr/bin/env bash - # the directory in which the tmux-sessionizer state will be stored script_dir="$HOME" # the file where the tmux-sessionizer data will be stored -file_name=".tmux-sessionizer.txt" -wildcards=".tmux-sessionizer_wildcards.txt" -full_path="$script_dir/$file_name" - -# create the tmux-sessionizer state file that remembers what directories to -# show -if [[ ! -e "$full_path" ]]; then - touch "$full_path" -fi - -# Write any commands here which will add directories that you want to stay -# up-to-date. Since directories are usually added to $HOME it is probably a -# good idea to keep the initial example -commands=( - # Example command - "find $HOME -maxdepth 1 -type d" -) - -combined_results="" - -# Gather the results of all the commands -for cmd in "${commands[@]}"; do - combined_results+=$(eval "$cmd"; echo) -done - -# if there were any results for the commands we add it to the tmux-sessionizer -# list, but we also ensure no duplicates are added -if [[ -n "$combined_results" ]]; then - temp_file=$(mktemp) - if { echo "$combined_results"; cat "$full_path"; } | sort -u > "$temp_file"; then - mv "$temp_file" "$full_path" - else - echo "Error: Failed to create sorted output." - rm -f "$temp_file" - fi -fi +direct_paths_filename=".tmux-sessionizer.txt" +direct_paths_fullpath="$script_dir/$direct_paths_filename" +wild_cards_filename=".tmux-sessionizer_wild.txt" +wild_cards_fullpath="$script_dir/$wild_cards_filename" -#adding, deleting or listing the tmux-sessionizer list -if [[ $# -eq 1 ]]; then +if [[ $# -gt 0 ]]; then # If any flags, or extra params were given - # add a path to our tmux save file, this accepts stdin and also prevents - # duplicates if [[ $1 == -add || $1 == -a ]]; then - while read line; do - # remove trailing slashes before comparing - line="${line%/}" - # ensure that the filepath is absolute and it exists - if [[ -d $line && "$line" = /* ]]; then - if ! grep -qFx $line $full_path ; then - echo "$line" >> $full_path + if [[ $# -eq 2 ]]; then # if a filepath was given + input_path=$2; + # If the user wants to add their current path + path=$(pwd) + if [[ "$input_path" == "." ]]; then + if ! grep -qxF $path $direct_paths_fullpath; then + echo "$path" >> "$direct_paths_fullpath" + fi + elif [[ "$input_path" == "all" ]]; then + path_with_star="${path%/}/*" + if ! grep -qxF "$path_with_star" $wild_cards_fullpath; then + echo "$path_with_star" >> "$wild_cards_fullpath" fi fi - done < "${2:-/dev/stdin}" + fi + + # TODO DEAL WITH PIPED DATA + #while read line; do + # # remove trailing slashes before comparing + # line="${line%/}" + # # ensure that the filepath is absolute and it exists + # if [[ -d $line && "$line" = /* ]]; then + # if ! grep -qFx $line $direct_paths_fullpath ; then + # echo "$line" >> $direct_paths_fullpath + # fi + # fi + #done < "${2:-/dev/stdin}" # delete a path from our tmux save file. You can select multiple files to # delete using the tab key in fzf to select multiple elif [[ $1 == -delete || $1 == -d ]]; then while true; do - selected=$(cat "$full_path" | fzf -m --cycle --scroll-off 10) + selected=$(cat "$direct_paths_fullpath" "$wild_cards_fullpath" | fzf -m --cycle --scroll-off 10) if [[ -z "$selected" ]]; then exit 0 @@ -71,28 +52,34 @@ if [[ $# -eq 1 ]]; then count=$(echo "$selected" | wc -l) if [[ $count -gt 0 ]]; then - temp_file=$(mktemp) - grep -v -F -x -e "$selected" "$full_path" > "$temp_file" - mv "$temp_file" "$full_path" - rm $temp_file + awk -v pattern="$selected" '$0 != pattern' "$direct_paths_fullpath" > "$direct_paths_fullpath.tmp" && mv "$direct_paths_fullpath.tmp" "$direct_paths_fullpath" + awk -v pattern="$selected" '$0 != pattern' "$wild_cards_fullpath" > "$wild_cards_fullpath.tmp" && mv "$wild_cards_fullpath.tmp" "$wild_cards_fullpath" fi done # simply lists all paths in your tmux-sessionizer list to standard out elif [[ $1 == -list || $1 == -l ]]; then - cat $full_path + cat $direct_paths_fullpath $wild_cards_fullpath fi exit 0 fi - # remove any directories in the tmux-sessionizer list that no longer exist valid_paths=$(mktemp) while read -r line; do if [[ -d "$line" ]]; then echo "$line" >> $valid_paths fi -done < "$full_path" -mv "$valid_paths" "$full_path" +done < "$direct_paths_fullpath" +mv "$valid_paths" "$direct_paths_fullpath" + +valid_paths=$(mktemp) +while read -r line; do + base_dir="${line%/*}" + if [[ -d "$base_dir" ]]; then + echo "$line" >> $valid_paths + fi +done < "$wild_cards_fullpath" +mv "$valid_paths" "$wild_cards_fullpath" # switching tmux to the files @@ -119,9 +106,15 @@ hydrate() { if [[ $# -eq 1 ]]; then selected=$1 else - selected=$(cat $full_path | fzf --cycle --scroll-off 10) + temp=$(mktemp) + while read -r line; do + echo $(find $line -type d) + find $line -type d >> $temp + done < "$wild_cards_fullpath" + selected=$(cat $direct_paths_fullpath $temp | fzf --cycle --scroll-off 10) fi + if [[ -z $selected ]]; then exit 0 fi From 2f20a54ffd28a22eb52e4b0f2c23549ddc0317f9 Mon Sep 17 00:00:00 2001 From: johnmarco123 Date: Sat, 9 Nov 2024 17:46:47 -0500 Subject: [PATCH 12/17] dynamic tmux sessionizer version 1 --- README.md | 46 +++++++++++++++------------------------------- tmux-sessionizer | 43 +++++++++++++++++++++++++++---------------- 2 files changed, 42 insertions(+), 47 deletions(-) diff --git a/README.md b/README.md index 3be037a..7be348d 100644 --- a/README.md +++ b/README.md @@ -4,47 +4,31 @@ its a script that does everything awesome at all times ## Basic usage ```bash tmux-sessionizer [] +tmux-sessionizer -add PATH_TO_FOLDER_YOU_WANT_IN_YOUR_LIST [Optional: all] +tmux-sessionizer -delete +tmux-sessionizer -list ``` -By default, tmux-sessionizer holds its state in .tmux-sessionizer.txt in your -$HOME directory, but this can be changed within the top of tmux-sessionizer -itself. - -Inside of tmux-sessionizer you can also have commands that will automatically -add filepaths for you on each execution. These files never get saved in the -tmux-sessionizer list as they are dynamically created with the commands. ## Options -a, -add - Adds full filepaths to your tmux-sessionizer list via stdin or - interactive mode. It will automatically reject duplicates, and will - only accept valid absolute filepaths to folders that exist. + Adds the provided directory to your tmux-sessionizer list. This only + adds the actual directory which you provide. Alternatively, you can add + the "all" keyword after your filepath to automatically add any + directories directly within the filepath directory to your + tmux-sessionizer list -d, -delete - Opens a multi-select fzf of your current tmux-sessionizer list which - can be used to delete files in your list by selecting either one file - to delete using the enter key, or multiple files using tab to select - multiple files and then enter to delete those files. CTRL-C to exit - + Opens a fzf window that you can use to delete any directory paths that + you no longer want in your list. You can select multiple options to + delete by clicking tab on the paths you want to delete and then + clicking enter to confirm + -l, -list - Lists the contents of the tmux-sessionizer list. + List all the paths in your tmux-sessionizer list + ### Example commands -Add all directories within the current working directory to your list. -```bash -find $(pwd) -maxdepth 1 -type d | tmux-sessionizer -a -``` - -Enter fzf and delete folders in your tmux-sessionizer list by selecting them -with tab and then clicking enter -```bash -tmux-sessionizer -d -``` - -List all the directories in your tmux-sessionizer list -```bash -tmux-sessionizer -l -``` if you execute tmux-sessionizer without any parameters it will assume FZF and try to fuzzy find over a set of directories. diff --git a/tmux-sessionizer b/tmux-sessionizer index 15904f3..4b76cf0 100755 --- a/tmux-sessionizer +++ b/tmux-sessionizer @@ -10,21 +10,29 @@ wild_cards_fullpath="$script_dir/$wild_cards_filename" if [[ $# -gt 0 ]]; then # If any flags, or extra params were given +# we want it to be tmux-sessionizer -a/add ANY SORTA PATH (optional depth DEFAULT 0) + if [[ $1 == -add || $1 == -a ]]; then - if [[ $# -eq 2 ]]; then # if a filepath was given - input_path=$2; - # If the user wants to add their current path - path=$(pwd) - if [[ "$input_path" == "." ]]; then - if ! grep -qxF $path $direct_paths_fullpath; then - echo "$path" >> "$direct_paths_fullpath" - fi - elif [[ "$input_path" == "all" ]]; then - path_with_star="${path%/}/*" - if ! grep -qxF "$path_with_star" $wild_cards_fullpath; then - echo "$path_with_star" >> "$wild_cards_fullpath" + if [[ $# -gt 1 ]]; then + input_path=$(realpath "$2"); # make the path absolute + if [[ ! -d "$input_path" ]]; then # check to ensure its a valid path + echo "Error: Invalid path provided" >&2 + exit 1 + fi + if [[ $# -gt 2 ]]; then + if [[ $3 -eq "all" ]]; then + path_with_star="${input_path%/}/*" + if ! grep -qxF "$path_with_star" $wild_cards_fullpath; then + echo "$path_with_star" >> "$wild_cards_fullpath" + exit 0 + fi fi fi + if ! grep -qxF "$input_path" $direct_paths_fullpath; then + echo "$input_path" >> "$direct_paths_fullpath" + exit 0 + fi + exit 1 fi # TODO DEAL WITH PIPED DATA @@ -52,8 +60,11 @@ if [[ $# -gt 0 ]]; then # If any flags, or extra params were given count=$(echo "$selected" | wc -l) if [[ $count -gt 0 ]]; then - awk -v pattern="$selected" '$0 != pattern' "$direct_paths_fullpath" > "$direct_paths_fullpath.tmp" && mv "$direct_paths_fullpath.tmp" "$direct_paths_fullpath" - awk -v pattern="$selected" '$0 != pattern' "$wild_cards_fullpath" > "$wild_cards_fullpath.tmp" && mv "$wild_cards_fullpath.tmp" "$wild_cards_fullpath" + while IFS= read -r line; do + escaped_line=$(printf '%s' "$line" | sed 's/[&/\*]/\\&/g') + sed -i "/^$escaped_line$/d" "$wild_cards_fullpath" + sed -i "/^$escaped_line$/d" "$direct_paths_fullpath" + done <<< "$selected" fi done # simply lists all paths in your tmux-sessionizer list to standard out @@ -108,8 +119,8 @@ if [[ $# -eq 1 ]]; then else temp=$(mktemp) while read -r line; do - echo $(find $line -type d) - find $line -type d >> $temp + base_dir="${line%/*}" + find $base_dir -type d -maxdepth 1 >> $temp done < "$wild_cards_fullpath" selected=$(cat $direct_paths_fullpath $temp | fzf --cycle --scroll-off 10) fi From 0d151806044b6d726910bf26efb053198e248d33 Mon Sep 17 00:00:00 2001 From: johnmarco123 Date: Sat, 9 Nov 2024 17:54:51 -0500 Subject: [PATCH 13/17] updated todo list --- TODO.md | 8 -------- 1 file changed, 8 deletions(-) diff --git a/TODO.md b/TODO.md index 3621966..ebcb774 100644 --- a/TODO.md +++ b/TODO.md @@ -1,11 +1,3 @@ -* [X] Make quick command to add the current working directory - -* [X] Make quick command to add all directories in the current working - directory - -* [ ] After the quick commands, resolve how to do this when piping an entire - file - * [ ] handle duplicate entries, maybe do a check before inserting another path * [ ] make a separate list for wildcard dirs such as /home/user/* These will From fb6158a95ed91afc2be247c7468ae6995eb064cf Mon Sep 17 00:00:00 2001 From: johnmarco123 Date: Sat, 9 Nov 2024 18:41:27 -0500 Subject: [PATCH 14/17] added a small awk command so that if you have two of the same path, (one from regular add and one from the "all" option add) that you only see one when picking a path --- tmux-sessionizer | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/tmux-sessionizer b/tmux-sessionizer index 4b76cf0..b4bcdc2 100755 --- a/tmux-sessionizer +++ b/tmux-sessionizer @@ -122,7 +122,7 @@ else base_dir="${line%/*}" find $base_dir -type d -maxdepth 1 >> $temp done < "$wild_cards_fullpath" - selected=$(cat $direct_paths_fullpath $temp | fzf --cycle --scroll-off 10) + selected=$(cat $direct_paths_fullpath $temp | awk '!seen[$0]++' | fzf --cycle --scroll-off 10) fi From 6c527afc26599083f8841164b4c38bd5af6d475d Mon Sep 17 00:00:00 2001 From: johnmarco123 Date: Sat, 30 Nov 2024 10:48:54 -0500 Subject: [PATCH 15/17] made part of the script create the files needed if they do not already exist, and change there permissions --- tmux-sessionizer | 13 +++++++++++++ 1 file changed, 13 insertions(+) diff --git a/tmux-sessionizer b/tmux-sessionizer index b4bcdc2..af67531 100755 --- a/tmux-sessionizer +++ b/tmux-sessionizer @@ -8,6 +8,19 @@ direct_paths_fullpath="$script_dir/$direct_paths_filename" wild_cards_filename=".tmux-sessionizer_wild.txt" wild_cards_fullpath="$script_dir/$wild_cards_filename" + +# create the filepaths and change their perms if the files do not yet exist +if [ ! -f "$direct_paths_fullpath" ]; then + echo "sudo is required to initialize the script, this will create and chmod the needed files" + sudo touch "$direct_paths_fullpath" + sudo chmod 666 "$direct_paths_fullpath" +fi + +if [ ! -f "$wild_cards_fullpath" ]; then + sudo touch "$wild_cards_fullpath" + sudo chmod 666 "$wild_cards_fullpath" +fi + if [[ $# -gt 0 ]]; then # If any flags, or extra params were given # we want it to be tmux-sessionizer -a/add ANY SORTA PATH (optional depth DEFAULT 0) From 26769819468e24c0559e42839bdac7744b6949c6 Mon Sep 17 00:00:00 2001 From: johnmarco123 Date: Sat, 30 Nov 2024 22:58:48 -0500 Subject: [PATCH 16/17] oops, that actually did not work as i overwrote the file later on..., this will work now lol --- tmux-sessionizer | 14 ++++++-------- 1 file changed, 6 insertions(+), 8 deletions(-) diff --git a/tmux-sessionizer b/tmux-sessionizer index af67531..be8cfdf 100755 --- a/tmux-sessionizer +++ b/tmux-sessionizer @@ -11,14 +11,13 @@ wild_cards_fullpath="$script_dir/$wild_cards_filename" # create the filepaths and change their perms if the files do not yet exist if [ ! -f "$direct_paths_fullpath" ]; then - echo "sudo is required to initialize the script, this will create and chmod the needed files" - sudo touch "$direct_paths_fullpath" - sudo chmod 666 "$direct_paths_fullpath" +echo "sudo is required to initialize the script, this will create and chmod the needed files" + sudo touch "$direct_paths_fullpath" && sudo chmod 666 "$direct_paths_fullpath" fi if [ ! -f "$wild_cards_fullpath" ]; then - sudo touch "$wild_cards_fullpath" - sudo chmod 666 "$wild_cards_fullpath" + sudo touch "$wild_cards_fullpath" && sudo chmod 666 "$wild_cards_fullpath" + exit 1 fi if [[ $# -gt 0 ]]; then # If any flags, or extra params were given @@ -94,7 +93,7 @@ while read -r line; do echo "$line" >> $valid_paths fi done < "$direct_paths_fullpath" -mv "$valid_paths" "$direct_paths_fullpath" +cat "$valid_paths" > "$direct_paths_fullpath" valid_paths=$(mktemp) while read -r line; do @@ -103,8 +102,7 @@ while read -r line; do echo "$line" >> $valid_paths fi done < "$wild_cards_fullpath" -mv "$valid_paths" "$wild_cards_fullpath" - +cat "$valid_paths" > "$wild_cards_fullpath" # switching tmux to the files switch_to() { From 5e427b49eb48f6148057ae6d05ffd8dca77bc01e Mon Sep 17 00:00:00 2001 From: John-Marco Tasillo <79156127+johnmarco123@users.noreply.github.com> Date: Sat, 7 Dec 2024 11:52:54 -0500 Subject: [PATCH 17/17] Update tmux-sessionizer added missing shebang --- tmux-sessionizer | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/tmux-sessionizer b/tmux-sessionizer index be8cfdf..6bec30e 100755 --- a/tmux-sessionizer +++ b/tmux-sessionizer @@ -1,4 +1,4 @@ -#/usr/bin/env bash +#!/usr/bin/env bash # the directory in which the tmux-sessionizer state will be stored script_dir="$HOME" # the file where the tmux-sessionizer data will be stored