-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathshellLog.lib
More file actions
executable file
·139 lines (121 loc) · 4.71 KB
/
shellLog.lib
File metadata and controls
executable file
·139 lines (121 loc) · 4.71 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
#!/usr/bin/env bash
#
# ############################################################################
# Project: scripts (none)
# File...: shellLog.lib
# Created: Saturday, 2025/02/08 - 20:02:28
# Author.: fbnmtz, (fabiano.matoz@gmail.com)
# ~·~·~·~·~·~·~·~·~·~·~·~·~~·~·~·~·~·~·~·~·~·~·~·~·~~·~·~·~·~·~~·~·~·~·~·~·~·~
# Last Modified: Sunday, 2025/02/09 - 11:42:49
# Modified By..: @fbnmtz, (fabiano.matoz@gmail.com)
# ~·~·~·~·~·~·~·~·~·~·~·~·~~·~·~·~·~·~·~·~·~·~·~·~·~~·~·~·~·~·~~·~·~·~·~·~·~·~
# Version: 0.0.2.50
# ~·~·~·~·~·~·~·~·~·~·~·~·~~·~·~·~·~·~·~·~·~·~·~·~·~~·~·~·~·~·~~·~·~·~·~·~·~·~
# Description:
# >
# ############################################################################
# HISTORY:
#
# Set a flag to indicate that this library is loaded
_xLIB_SHELLLOG_=true
# Define the database name and table name
DB_NAME=$APP_HOME/shellLog.db
TABLE_NAME="command_history"
# Define the log file
LOG_FILE=$APP_HOME/shellLog.log
# Define log levels
LOG_LEVELS=("DEBUG" "INFO" "WARN" "ERROR")
LOG_LEVEL=1 # Default to INFO
# Function to log messages with log levels and optional display
log_message() {
local level="$1"
local message="$2"
local always_display="$3"
local log_level_index=$(printf "%s\n" "${LOG_LEVELS[@]}" | grep -n "^$level\$" | cut -d: -f1)
if [ -n "$log_level_index" ] && [ "$log_level_index" -le "$LOG_LEVEL" ]; then
echo "$(date +"%F %T") - [$level] - $message" >> "$LOG_FILE"
if [ "$VERBOSE" = true ] || [ "$always_display" = true ]; then
echo "$(date +"%F %T") - [$level] - $message"
fi
fi
}
# Function to initialize the application
_app_init(){
# Check if the database exists, if not, create it
if [ ! -f "$DB_NAME" ]; then
if sqlite3 "$DB_NAME" "CREATE TABLE $TABLE_NAME (id INTEGER PRIMARY KEY, command TEXT, timestamp DATETIME DEFAULT CURRENT_TIMESTAMP);"; then
log_message "INFO" "Database created." true
else
log_message "ERROR" "Failed to create database." true
exit 1
fi
fi
}
# Function to insert a command into the database
insert_command() {
local command="$1"
local timestamp="$2"
sqlite3 "$DB_NAME" "INSERT INTO $TABLE_NAME (command, timestamp) VALUES ('$command', '$timestamp');"
log_message "DEBUG" "Inserted command: $command at $timestamp"
}
# Function to import history from a file
fImport(){
local filename=$1
# Set the history file to the provided filename or default to .zhistory
if [ -n "$filename" ]; then
HISTORY_FILE=$filename
else
HISTORY_FILE=$HOME/.zhistory
fi
# Check if the history file exists
if [ -f "$HISTORY_FILE" ]; then
# Read the history file line by line
while IFS= read -r line; do
# Extract the timestamp and command
timestamp=$(echo "$line" | awk -F';' '{print $1}' | awk -F':' '{print $2}')
command=$(echo "$line" | awk -F';' '{print $2}')
# Convert the Unix timestamp to the format YYYY-MM-DD HH:MM:SS
timestamp=$(date -r "$timestamp" +"%F %T")
# Insert the command into the database with its respective timestamp
insert_command "$command" "$timestamp"
done < "$HISTORY_FILE"
log_message "INFO" "Imported history from $HISTORY_FILE." true
else
log_message "WARN" "History file not found: $HISTORY_FILE" true
fi
}
# Function to capture and save the current command
save_current_command() {
local command="$1"
if [ -n "$command" ]; then
local timestamp=$(date +"%F %T")
insert_command "$command" "$timestamp"
log_message "DEBUG" "Saved current command: $command at $timestamp"
fi
}
# Hook to capture and save the current command before displaying the prompt
export PROMPT_COMMAND="save_current_command \"\$(history 1 | sed 's/^[ ]*[0-9]*[ ]*//')\""
# Function to view all records
fListAll() {
sqlite3 "$DB_NAME" "SELECT * FROM $TABLE_NAME;"
log_message "INFO" "Listed all commands." true
}
# Function to search in the command history using fzf
fSearchHistory() {
sqlite3 "$DB_NAME" "SELECT command FROM $TABLE_NAME;" | fzf
log_message "INFO" "Searched in command history." true
}
# Function to replace Ctrl+R with the search function
replace_ctrl_r() {
bind -x '"\C-r": "fSearchHistory"'
}
# Function to clear the database with confirmation
fClearDb() {
read -p "Are you sure you want to clear the database? (y/n): " confirm
if [[ $confirm == [yY] ]]; then
sqlite3 "$DB_NAME" "DELETE FROM $TABLE_NAME;"
log_message "INFO" "Database cleared." true
else
log_message "INFO" "Operation canceled." true
fi
}