191 lines
5.1 KiB
Bash
Executable File
191 lines
5.1 KiB
Bash
Executable File
SAVED_MODES_FILE=".saved_modes"
|
|
TEMP_FILE=".saved_modes.tmp"
|
|
|
|
# TODO: упростить работу с fzf
|
|
# либо сделать вход сразу с fuzzy
|
|
|
|
# Function to display the menu
|
|
show_menu() {
|
|
printf "\n\n=== Saved Modes ===\n"
|
|
|
|
if [[ ! -f "$SAVED_MODES_FILE" ]] || [[ ! -s "$SAVED_MODES_FILE" ]]; then
|
|
echo "No saved modes found."
|
|
echo ""
|
|
return
|
|
fi
|
|
|
|
local count=1
|
|
while IFS= read -r line; do
|
|
if [[ -n "$line" ]]; then
|
|
local name=$(echo "$line" | cut -d' ' -f1)
|
|
local command=$(echo "$line" | cut -d' ' -f2-)
|
|
|
|
# Truncate command to 40 characters max
|
|
if [[ ${#command} -gt 40 ]]; then
|
|
command="${command:0:40}..."
|
|
fi
|
|
|
|
printf "%2d) %-15s %s\n" "$count" "$name" "$command"
|
|
((count++))
|
|
fi
|
|
done < "$SAVED_MODES_FILE"
|
|
echo ""
|
|
}
|
|
|
|
# Function to add a new mode
|
|
add_mode() {
|
|
echo "Adding new mode:"
|
|
read -p "Enter name: " new_name
|
|
read -p "Enter command: " new_command
|
|
|
|
if [[ -n "$new_name" && -n "$new_command" ]]; then
|
|
# Create temp file with new entry at the beginning
|
|
echo "$new_name $new_command" > "$TEMP_FILE"
|
|
if [[ -f "$SAVED_MODES_FILE" ]]; then
|
|
cat "$SAVED_MODES_FILE" >> "$TEMP_FILE"
|
|
fi
|
|
mv "$TEMP_FILE" "$SAVED_MODES_FILE"
|
|
echo "Mode '$new_name' added successfully!"
|
|
else
|
|
echo "Error: Name and command cannot be empty."
|
|
fi
|
|
echo ""
|
|
}
|
|
|
|
# Function to remove a mode
|
|
remove_mode() {
|
|
local mode_num=$1
|
|
local count=0
|
|
|
|
if [[ ! -f "$SAVED_MODES_FILE" ]]; then
|
|
echo "No saved modes file found."
|
|
return
|
|
fi
|
|
|
|
# Count total lines
|
|
total_lines=$(wc -l < "$SAVED_MODES_FILE")
|
|
|
|
if [[ $mode_num -lt 1 || $mode_num -gt $total_lines ]]; then
|
|
echo "Error: Invalid mode number $mode_num"
|
|
return
|
|
fi
|
|
|
|
# Create temp file without the specified line
|
|
rm -f "$TEMP_FILE"
|
|
touch "$TEMP_FILE"
|
|
local current_line=1
|
|
while IFS= read -r line; do
|
|
if [[ $current_line -ne $mode_num ]]; then
|
|
echo "$line" >> "$TEMP_FILE"
|
|
else
|
|
echo "Removed mode: $line"
|
|
fi
|
|
((current_line++))
|
|
done < "$SAVED_MODES_FILE"
|
|
|
|
mv "$TEMP_FILE" "$SAVED_MODES_FILE"
|
|
echo "Mode $mode_num removed successfully!"
|
|
echo ""
|
|
}
|
|
|
|
# Function to execute a mode
|
|
execute_mode() {
|
|
local mode_num=$1
|
|
|
|
if [[ ! -f "$SAVED_MODES_FILE" ]]; then
|
|
echo "Error: No saved modes file found."
|
|
return 1
|
|
fi
|
|
|
|
# Get the command for the specified mode number
|
|
local command_line=$(sed -n "${mode_num}p" "$SAVED_MODES_FILE")
|
|
local command=$(echo "$command_line" | cut -d' ' -f2-)
|
|
|
|
if [[ -z "$command" ]]; then
|
|
echo "Error: Could not find command for mode $mode_num"
|
|
return 1
|
|
fi
|
|
|
|
# Clear terminal and execute command
|
|
#clear
|
|
echo "Executing: $command"
|
|
echo "========================================"
|
|
eval "$command"
|
|
exit 0
|
|
}
|
|
|
|
# Main script loop
|
|
main() {
|
|
while true; do
|
|
show_menu
|
|
|
|
echo "Options:"
|
|
echo " f - Search with fzf"
|
|
echo " [number] - Execute mode"
|
|
echo " n - Add new mode"
|
|
echo " r[number] - Remove mode (e.g., r1)"
|
|
echo " e - Exit"
|
|
echo ""
|
|
|
|
read -p "Enter choice: " input
|
|
|
|
case "$input" in
|
|
[eE])
|
|
echo "Goodbye!"
|
|
exit 0
|
|
;;
|
|
[nN])
|
|
add_mode
|
|
;;
|
|
[fF])
|
|
if command -v fzf >/dev/null 2>&1; then
|
|
local selected=$(cut -d' ' -f1 "$SAVED_MODES_FILE" | fzf --prompt="Select mode: ")
|
|
if [[ -n "$selected" ]]; then
|
|
local line_num=$(grep -n "^$selected " "$SAVED_MODES_FILE" | cut -d: -f1)
|
|
execute_mode "$line_num"
|
|
else
|
|
echo "No mode selected."
|
|
echo ""
|
|
fi
|
|
else
|
|
echo "Error: fzf is not installed."
|
|
echo ""
|
|
fi
|
|
;;
|
|
r[0-9]*)
|
|
# Extract number after 'r'
|
|
local mode_num="${input#r}"
|
|
if [[ "$mode_num" =~ ^[0-9]+$ ]]; then
|
|
remove_mode "$mode_num"
|
|
else
|
|
echo "Error: Invalid format. Use r1, r2, etc."
|
|
echo ""
|
|
fi
|
|
;;
|
|
[0-9]*)
|
|
if [[ "$input" =~ ^[0-9]+$ ]]; then
|
|
execute_mode "$input"
|
|
else
|
|
echo "Error: Please enter a valid number"
|
|
echo ""
|
|
fi
|
|
;;
|
|
*)
|
|
echo "Error: Invalid input. Please try again."
|
|
echo ""
|
|
;;
|
|
esac
|
|
done
|
|
}
|
|
|
|
# Cleanup function
|
|
cleanup() {
|
|
rm -f "$TEMP_FILE"
|
|
}
|
|
|
|
# Set up cleanup on exit
|
|
trap cleanup EXIT
|
|
|
|
# Run main function
|
|
main
|