Skip to content

Commit

Permalink
First 2nd 3rd and 4th rev-isions
Browse files Browse the repository at this point in the history
4th revision being most current  - goal is to evolve this to a working standalone program with a window not a series of pop-ups - in what language threes alternatives at the moment its a toss up between PyQt6 - and now looking at Tauri as a possibility - Though it may take diff directions tis only the beginning
  • Loading branch information
CLJP85 authored Oct 5, 2024
1 parent efbc16b commit d09f95b
Show file tree
Hide file tree
Showing 4 changed files with 306 additions and 0 deletions.
99 changes: 99 additions & 0 deletions TextSearchTruncate/duckduckgo_ai_text_find_truncate_rev1.bash
Original file line number Diff line number Diff line change
@@ -0,0 +1,99 @@
#!/bin/bash

# Set the initial text file and truncation lines
text_file=""
truncate_lines=1

# Function to get user input for the text file
get_text_file() {
while true; do
text_file=$(zenity --file-selection --title="Select Text File" --filename="$HOME" --file-filter="Text files | *.txt" --style="background-color:#282A36;color:#F8F8F2;font-size:14px")
if [ "$?" -eq 1 ]; then
# User clicked the "Cancel" button or closed the window
notify_user "Canceled" "No text file selected."
return
elif [ ! -f "$text_file" ]; then
notify_user "Error" "The selected file does not exist or is not a regular file."
elif [ ! -r "$text_file" ]; then
notify_user "Error" "You do not have permission to read the selected file."
else
echo "Using text file: $text_file"
break
fi
done
}

# Function to get user input for the truncation lines
get_truncate_lines() {
while true; do
truncate_lines=$(zenity --entry --title="Truncate Lines" --text="Please enter the number of lines to truncate from the tail:" --ok-label="Enter" --cancel-label="-- " --extra-button="X" --style="background-color:#282A36;color:#F8F8F2;font-size:14px")
if [ "$?" -eq 1 ]; then
# User clicked the "Cancel" button or closed the window
notify_user "Canceled" "No truncation value specified."
return
elif [ "$?" -eq 2 ]; then
# User clicked the "X" button
exit 0
elif ! [[ "$truncate_lines" =~ ^[0-9]+$ ]]; then
notify_user "Error" "Invalid truncation value. Please enter a positive integer."
elif [ "$truncate_lines" -le 0 ]; then
notify_user "Error" "The truncation value must be greater than 0."
else
echo "Truncating $truncate_lines lines from the tail."
break
fi
done
}

# Function to display a notification
notify_user() {
local title="$1"
local message="$2"
notify-send --urgency=low --icon=dialog-information --expire-time=3000 --style="background-color:#282A36;color:#F8F8F2;font-size:14px" "$title" "$message"
}

# Function to clear the input fields
clear_inputs() {
text_file=""
truncate_lines=1
}

# Function to process the file and display the output
process_file() {
if [ ! -f "$text_file" ]; then
notify_user "Error" "The selected file does not exist or is not a regular file."
return
elif [ ! -r "$text_file" ]; then
notify_user "Error" "You do not have permission to read the selected file."
return
fi

# Truncate and output the last line(s)
last_lines=$(cat "$text_file" | tail -n "$truncate_lines")
echo "$last_lines"

# Display a notification with the last line(s)
notify_user "Last $truncate_lines Line(s)" "$last_lines"
}

# Main function to run the script
run_script() {
get_text_file
if [ -n "$text_file" ]; then
get_truncate_lines
process_file
fi
}

# Keyboard shortcuts
bind_shortcuts() {
# Ctrl+Enter to submit the inputs
bind -x '"\C-m": run_script'

# Ctrl+C to clear the inputs
bind -x '"\C-c": clear_inputs'
}

# Run the script
bind_shortcuts
run_script
85 changes: 85 additions & 0 deletions TextSearchTruncate/duckduckgo_ai_text_find_truncate_rev2.bash
Original file line number Diff line number Diff line change
@@ -0,0 +1,85 @@
#!/bin/bash

# Set the initial text file, truncation lines, and default directory
text_file=""
truncate_lines=1
default_dir="$HOME"

# Directories for results, archives, and config
script_dir="$PWD"
results_dir="$script_dir/results"
archives_dir="$script_dir/archives"
config_file="$script_dir/config/config.sh"
mkdir -p "$results_dir" "$archives_dir" "$(dirname "$config_file")"

# Load the configuration file
if [ -f "$config_file" ]; then
source "$config_file"
text_file="${last_text_file:-$text_file}"
truncate_lines="${last_truncate_lines:-$truncate_lines}"
default_dir=$(dirname "$text_file")
fi

# Function to get user input for the text file
get_text_file() {
while true; do
text_file=$(zenity --file-selection --title="Select Text File" --filename="$default_dir" --file-filter="Text files | *.txt" --style="background-color:#282A36;color:#F8F8F2;font-size:14px")
if [ "$?" -eq 1 ]; then
# User clicked the "Cancel" button or closed the window
notify_user "Canceled" "No text file selected."
return
elif [ ! -f "$text_file" ]; then
notify_user "Error" "The selected file does not exist or is not a regular file."
elif [ ! -r "$text_file" ]; then
notify_user "Error" "You do not have permission to read the selected file."
else
echo "Using text file: $text_file"
default_dir=$(dirname "$text_file")
break
fi
done
}

# Function to get user input for the truncation lines
get_truncate_lines() {
while true; do
truncate_lines=$(zenity --entry --title="Truncate Lines" --text="Please enter the number of lines to truncate from the tail:" --ok-label="Enter" --cancel-label="-- " --extra-button="X" --style="background-color:#282A36;color:#F8F8F2;font-size:14px")
if [ "$?" -eq 1 ]; then
# User clicked the "Cancel" button or closed the window
notify_user "Canceled" "No truncation value specified."
return
elif [ "$?" -eq 2 ]; then
# User clicked the "X" button
exit 0
elif ! [[ "$truncate_lines" =~ ^[0-9]+$ ]]; then
notify_user "Error" "Invalid truncation value. Please enter a positive integer."
elif [ "$truncate_lines" -le 0 ]; then
notify_user "Error" "The truncation value must be greater than 0."
else
# Ensure the truncation value does not exceed the number of lines in the file
local total_lines=$(wc -l < "$text_file")
if [ "$truncate_lines" -gt "$total_lines" ]; then
truncate_lines=$total_lines
notify_user "Warning" "The truncation value exceeds the number of lines in the file. Truncating to the last $truncate_lines line(s)."
fi
echo "Truncating $truncate_lines lines from the tail."
break
fi
done
}

# Function to display a notification
notify_user() {
local title="$1"
local message="$2"
notify-send --urgency=low --icon=dialog-information --expire-time=3000 --style="background-color:#282A36;color:#F8F8F2;font-size:14px" "$title" "$message"
}

# Function to clear the input fields
clear_inputs() {
text_file=""
truncate_lines=1
default_dir="$HOME"
last_text_file=""
last_truncate_lines=1

61 changes: 61 additions & 0 deletions TextSearchTruncate/duckduckgo_ai_text_find_truncate_rev3.bash
Original file line number Diff line number Diff line change
@@ -0,0 +1,61 @@
#!/bin/bash

# ...

# Function to create directories if they don't exist
create_directories() {
local dir_path="$1"
if [ ! -d "$dir_path" ]; then
mkdir -p "$dir_path"
echo "Created directory: $dir_path" | tee -a "$log_file"
fi
}

# Function to log messages
log_message() {
local message="$1"
local log_file_path="$script_dir/logs/truncate-file.log"
create_directories "$(dirname "$log_file_path")"
echo "$message" | tee -a "$log_file_path"
}

# Function to truncate files
truncate_files() {
for file in $text_files; do
if [ ! -f "$file" ]; then
log_message "Error: File $file does not exist."
continue
elif [ ! -r "$file" ]; then
log_message "Error: You do not have permission to read file $file."
continue
elif [ ! -s "$file" ]; then
log_message "Error: File $file is empty."
continue
fi

if [ -n "$output_file_name" ]; then
output_file="${file%.*}_${output_file_name}.txt"
else
output_file="${file}.truncated"
fi

output_dir=$(dirname "$output_file")
create_directories "$output_dir"

if ! head -n "$truncate_lines" "$file" > "$output_file"; then
log_message "Error: Failed to truncate file $file."
continue
fi

log_message "File $file truncated successfully"
done
log_message "Files truncated successfully"
}

# Main program
get_text_files
get_truncate_lines
get_output_file_name
confirm_truncation
truncate_files

61 changes: 61 additions & 0 deletions TextSearchTruncate/duckduckgo_ai_text_find_truncate_rev4.bash
Original file line number Diff line number Diff line change
@@ -0,0 +1,61 @@
#!/bin/bash

# ...

# Function to create directories if they don't exist
create_directories() {
local dir_path="$1"
if [ ! -d "$dir_path" ]; then
mkdir -p "$dir_path"
echo "Created directory: $dir_path" | tee -a "$log_file"
fi
}

# Function to log messages
log_message() {
local message="$1"
local log_file_path="$script_dir/logs/truncate-file.log"
create_directories "$(dirname "$log_file_path")"
echo "$message" | tee -a "$log_file_path"
}

# Function to truncate files
truncate_files() {
for file in $text_files; do
if [ ! -f "$file" ]; then
log_message "Error: File $file does not exist."
continue
elif [ ! -r "$file" ]; then
log_message "Error: You do not have permission to read file $file."
continue
elif [ ! -s "$file" ]; then
log_message "Error: File $file is empty."
continue
fi

if [ -n "$output_file_name" ]; then
output_file="${file%.*}_${output_file_name}.txt"
else
output_file="${file}.truncated"
fi

output_dir=$(dirname "$output_file")
create_directories "$output_dir"

if ! head -n "$truncate_lines" "$file" > "$output_file"; then
log_message "Error: Failed to truncate file $file."
continue
fi

log_message "File $file truncated successfully"
done
log_message "Files truncated successfully"
}

# Main program
get_text_files
get_truncate_lines
get_output_file_name
confirm_truncation
truncate_files

0 comments on commit d09f95b

Please sign in to comment.