chore: initialize project structure with empty workspace and configuration files
This commit is contained in:
@@ -0,0 +1,22 @@
|
||||
#include "config.h"
|
||||
#include <stdlib.h>
|
||||
#include <string.h>
|
||||
|
||||
Config *config_load(void) {
|
||||
Config *cfg = calloc(1, sizeof(Config));
|
||||
if (cfg) {
|
||||
cfg->shell = strdup("bash");
|
||||
cfg->filter_mode = strdup("global");
|
||||
cfg->history_limit = 10000;
|
||||
cfg->enable_colors = true;
|
||||
}
|
||||
return cfg;
|
||||
}
|
||||
|
||||
void config_free(Config *cfg) {
|
||||
if (cfg) {
|
||||
free(cfg->shell);
|
||||
free(cfg->filter_mode);
|
||||
free(cfg);
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,16 @@
|
||||
#ifndef RHISTORY_CONFIG_H
|
||||
#define RHISTORY_CONFIG_H
|
||||
|
||||
#include <stdbool.h>
|
||||
|
||||
typedef struct {
|
||||
char *shell;
|
||||
char *filter_mode;
|
||||
int history_limit;
|
||||
bool enable_colors;
|
||||
} Config;
|
||||
|
||||
Config *config_load(void);
|
||||
void config_free(Config *cfg);
|
||||
|
||||
#endif
|
||||
@@ -0,0 +1,106 @@
|
||||
#include "db.h"
|
||||
#include <stdio.h>
|
||||
#include <stdlib.h>
|
||||
#include <string.h>
|
||||
|
||||
static sqlite3 *db = NULL;
|
||||
|
||||
bool db_init(const char *db_path) {
|
||||
if (sqlite3_open_v2(db_path, &db, SQLITE_OPEN_READWRITE | SQLITE_OPEN_CREATE, NULL) != SQLITE_OK) {
|
||||
fprintf(stderr, "Error opening database: %s\n", sqlite3_errmsg(db));
|
||||
return false;
|
||||
}
|
||||
|
||||
// Enable WAL mode for better concurrency
|
||||
char *err_msg = NULL;
|
||||
if (sqlite3_exec(db, "PRAGMA journal_mode=WAL;", NULL, NULL, &err_msg) != SQLITE_OK) {
|
||||
fprintf(stderr, "Failed to set WAL mode: %s\n", err_msg);
|
||||
sqlite3_free(err_msg);
|
||||
// Not fatal
|
||||
}
|
||||
|
||||
const char *schema_sql =
|
||||
"CREATE TABLE IF NOT EXISTS history ("
|
||||
" id INTEGER PRIMARY KEY AUTOINCREMENT,"
|
||||
" command TEXT NOT NULL,"
|
||||
" cwd TEXT NOT NULL,"
|
||||
" exit_code INTEGER NOT NULL,"
|
||||
" duration INTEGER NOT NULL,"
|
||||
" hostname TEXT NOT NULL,"
|
||||
" username TEXT NOT NULL,"
|
||||
" shell TEXT NOT NULL,"
|
||||
" session TEXT NOT NULL,"
|
||||
" timestamp INTEGER NOT NULL,"
|
||||
" created_at DATETIME DEFAULT CURRENT_TIMESTAMP,"
|
||||
" updated_at DATETIME DEFAULT CURRENT_TIMESTAMP"
|
||||
");"
|
||||
"CREATE INDEX IF NOT EXISTS idx_timestamp ON history(timestamp DESC);"
|
||||
"CREATE INDEX IF NOT EXISTS idx_exit_code ON history(exit_code);"
|
||||
"CREATE INDEX IF NOT EXISTS idx_hostname ON history(hostname);"
|
||||
"CREATE INDEX IF NOT EXISTS idx_cwd ON history(cwd);";
|
||||
|
||||
if (sqlite3_exec(db, schema_sql, NULL, NULL, &err_msg) != SQLITE_OK) {
|
||||
fprintf(stderr, "Schema creation failed: %s\n", err_msg);
|
||||
sqlite3_free(err_msg);
|
||||
sqlite3_close(db);
|
||||
db = NULL;
|
||||
return false;
|
||||
}
|
||||
|
||||
return true;
|
||||
}
|
||||
|
||||
void db_close(void) {
|
||||
if (db) {
|
||||
sqlite3_close(db);
|
||||
db = NULL;
|
||||
}
|
||||
}
|
||||
|
||||
sqlite3 *db_get_conn(void) {
|
||||
return db;
|
||||
}
|
||||
|
||||
bool db_add_history(const HistoryEntry *entry) {
|
||||
if (!db) return false;
|
||||
|
||||
const char *sql = "INSERT INTO history "
|
||||
"(command, cwd, exit_code, duration, hostname, username, shell, session, timestamp) "
|
||||
"VALUES (?, ?, ?, ?, ?, ?, ?, ?, ?);";
|
||||
|
||||
sqlite3_stmt *stmt;
|
||||
if (sqlite3_prepare_v2(db, sql, -1, &stmt, NULL) != SQLITE_OK) {
|
||||
fprintf(stderr, "Prepare failed: %s\n", sqlite3_errmsg(db));
|
||||
return false;
|
||||
}
|
||||
|
||||
sqlite3_bind_text(stmt, 1, entry->command, -1, SQLITE_STATIC);
|
||||
sqlite3_bind_text(stmt, 2, entry->cwd, -1, SQLITE_STATIC);
|
||||
sqlite3_bind_int(stmt, 3, entry->exit_code);
|
||||
sqlite3_bind_int(stmt, 4, entry->duration);
|
||||
sqlite3_bind_text(stmt, 5, entry->hostname, -1, SQLITE_STATIC);
|
||||
sqlite3_bind_text(stmt, 6, entry->username, -1, SQLITE_STATIC);
|
||||
sqlite3_bind_text(stmt, 7, entry->shell, -1, SQLITE_STATIC);
|
||||
sqlite3_bind_text(stmt, 8, entry->session, -1, SQLITE_STATIC);
|
||||
sqlite3_bind_int64(stmt, 9, entry->timestamp);
|
||||
|
||||
int rc = sqlite3_step(stmt);
|
||||
sqlite3_finalize(stmt);
|
||||
|
||||
if (rc != SQLITE_DONE) {
|
||||
fprintf(stderr, "Insert failed: %s\n", sqlite3_errmsg(db));
|
||||
return false;
|
||||
}
|
||||
return true;
|
||||
}
|
||||
|
||||
void db_free_entry(HistoryEntry *entry) {
|
||||
if (!entry) return;
|
||||
free(entry->command);
|
||||
free(entry->cwd);
|
||||
free(entry->hostname);
|
||||
free(entry->username);
|
||||
free(entry->shell);
|
||||
free(entry->session);
|
||||
free(entry);
|
||||
}
|
||||
@@ -0,0 +1,31 @@
|
||||
#ifndef RHISTORY_DB_H
|
||||
#define RHISTORY_DB_H
|
||||
|
||||
#include <stdint.h>
|
||||
#include <stdbool.h>
|
||||
#include <sqlite3.h>
|
||||
|
||||
typedef struct {
|
||||
int64_t id;
|
||||
char *command;
|
||||
char *cwd;
|
||||
int exit_code;
|
||||
int duration; // ms
|
||||
char *hostname;
|
||||
char *username;
|
||||
char *shell;
|
||||
char *session;
|
||||
int64_t timestamp;
|
||||
} HistoryEntry;
|
||||
|
||||
// Database management
|
||||
bool db_init(const char *db_path);
|
||||
void db_close(void);
|
||||
sqlite3 *db_get_conn(void);
|
||||
|
||||
// CRUD
|
||||
bool db_add_history(const HistoryEntry *entry);
|
||||
HistoryEntry *db_get_entry(int64_t id);
|
||||
void db_free_entry(HistoryEntry *entry);
|
||||
|
||||
#endif
|
||||
+260
@@ -0,0 +1,260 @@
|
||||
#include "history.h"
|
||||
#include "utils.h"
|
||||
#include <unistd.h>
|
||||
#include <sys/types.h>
|
||||
#include <pwd.h>
|
||||
#include <stdlib.h>
|
||||
#include <string.h>
|
||||
#include <stdio.h>
|
||||
#include <ctype.h>
|
||||
|
||||
// Helper to check if a file exists
|
||||
static bool file_exists(const char *path) {
|
||||
return access(path, F_OK) != -1;
|
||||
}
|
||||
|
||||
static bool import_bash(void) {
|
||||
char *home = get_home_dir();
|
||||
char *path = join_path(home, ".bash_history");
|
||||
free(home);
|
||||
|
||||
if (!file_exists(path)) {
|
||||
printf("Bash history file not found: %s\n", path);
|
||||
free(path);
|
||||
return false;
|
||||
}
|
||||
|
||||
FILE *fp = fopen(path, "r");
|
||||
if (!fp) {
|
||||
perror("Failed to open bash history");
|
||||
free(path);
|
||||
return false;
|
||||
}
|
||||
|
||||
printf("Importing from %s...\n", path);
|
||||
|
||||
char line[4096];
|
||||
int64_t next_timestamp = 0;
|
||||
int count = 0;
|
||||
|
||||
// Use transaction for bulk insert
|
||||
sqlite3_exec(db_get_conn(), "BEGIN TRANSACTION;", NULL, NULL, NULL);
|
||||
|
||||
while (fgets(line, sizeof(line), fp)) {
|
||||
// Strip newline
|
||||
size_t len = strlen(line);
|
||||
if (len > 0 && line[len-1] == '\n') {
|
||||
line[len-1] = 0;
|
||||
len--;
|
||||
}
|
||||
|
||||
if (len == 0) continue;
|
||||
|
||||
if (line[0] == '#') {
|
||||
// Check if it is a timestamp (all digits)
|
||||
bool is_ts = true;
|
||||
for (int i = 1; line[i]; i++) {
|
||||
if (!isdigit(line[i])) {
|
||||
is_ts = false;
|
||||
break;
|
||||
}
|
||||
}
|
||||
if (is_ts && line[1]) { // ensure at least one digit
|
||||
next_timestamp = atoll(line + 1);
|
||||
continue;
|
||||
}
|
||||
}
|
||||
|
||||
// It is a command
|
||||
HistoryEntry entry;
|
||||
memset(&entry, 0, sizeof(entry));
|
||||
entry.command = line;
|
||||
entry.cwd = "unknown";
|
||||
entry.exit_code = 0;
|
||||
entry.duration = 0;
|
||||
entry.hostname = "localhost"; // TODO: get real hostname
|
||||
entry.username = "unknown"; // db_add_history might handle? No, we set it in record.
|
||||
entry.shell = "bash";
|
||||
entry.session = "import";
|
||||
entry.timestamp = next_timestamp > 0 ? next_timestamp : 0;
|
||||
|
||||
// Populate username
|
||||
struct passwd *pw = getpwuid(getuid());
|
||||
entry.username = pw ? pw->pw_name : "unknown";
|
||||
|
||||
db_add_history(&entry);
|
||||
next_timestamp = 0;
|
||||
count++;
|
||||
}
|
||||
|
||||
sqlite3_exec(db_get_conn(), "COMMIT;", NULL, NULL, NULL);
|
||||
|
||||
fclose(fp);
|
||||
free(path);
|
||||
printf("Imported %d entries from bash history.\n", count);
|
||||
return true;
|
||||
}
|
||||
|
||||
static bool parse_zsh_line(char *line, HistoryEntry *entry) {
|
||||
// Format: : {timestamp}:{duration};{command}
|
||||
if (line[0] != ':') return false;
|
||||
|
||||
char *p = line + 1;
|
||||
while (*p && isspace(*p)) p++; // skip space
|
||||
|
||||
char *ts_start = p;
|
||||
char *ts_end = strchr(p, ':');
|
||||
if (!ts_end) return false;
|
||||
|
||||
*ts_end = 0;
|
||||
entry->timestamp = atoll(ts_start);
|
||||
*ts_end = ':'; // restore
|
||||
|
||||
p = ts_end + 1;
|
||||
char *dur_end = strchr(p, ';');
|
||||
if (!dur_end) return false;
|
||||
|
||||
*dur_end = 0;
|
||||
entry->duration = atoi(p); // Usually sec, but we store ms? Atuin stores ns? Prompt says ms.
|
||||
// Zsh usually stores seconds. Let's assume seconds -> ms?
|
||||
// Actually zsh extended_history stores seconds.
|
||||
entry->duration *= 1000;
|
||||
*dur_end = ';'; // restore
|
||||
|
||||
entry->command = dur_end + 1;
|
||||
return true;
|
||||
}
|
||||
|
||||
static bool import_zsh(void) {
|
||||
char *home = get_home_dir();
|
||||
char *path = join_path(home, ".zsh_history");
|
||||
free(home);
|
||||
|
||||
if (!file_exists(path)) {
|
||||
printf("Zsh history file not found: %s\n", path);
|
||||
free(path);
|
||||
return false;
|
||||
}
|
||||
|
||||
FILE *fp = fopen(path, "r"); // zsh history can contain binary chars? Usually text.
|
||||
if (!fp) {
|
||||
perror("Failed to open zsh history");
|
||||
free(path);
|
||||
return false;
|
||||
}
|
||||
|
||||
printf("Importing from %s...\n", path);
|
||||
|
||||
char line[10240]; // zsh lines can be long
|
||||
int count = 0;
|
||||
|
||||
sqlite3_exec(db_get_conn(), "BEGIN TRANSACTION;", NULL, NULL, NULL);
|
||||
|
||||
// Zsh history can be multiline if ending with \
|
||||
// But for simplicity let's handle single lines first or assume extended format is one line per cmd usually
|
||||
|
||||
while (fgets(line, sizeof(line), fp)) {
|
||||
size_t len = strlen(line);
|
||||
if (len > 0 && line[len-1] == '\n') line[len-1] = 0;
|
||||
|
||||
HistoryEntry entry;
|
||||
memset(&entry, 0, sizeof(entry));
|
||||
|
||||
// Zsh extended history check
|
||||
if (line[0] == ':') {
|
||||
if (!parse_zsh_line(line, &entry)) {
|
||||
// Fallback or skip?
|
||||
entry.command = line;
|
||||
entry.timestamp = 0;
|
||||
}
|
||||
} else {
|
||||
entry.command = line;
|
||||
entry.timestamp = 0;
|
||||
}
|
||||
|
||||
entry.cwd = "unknown";
|
||||
entry.exit_code = 0;
|
||||
entry.hostname = "localhost";
|
||||
entry.shell = "zsh";
|
||||
entry.session = "import";
|
||||
struct passwd *pw = getpwuid(getuid());
|
||||
entry.username = pw ? pw->pw_name : "unknown";
|
||||
|
||||
db_add_history(&entry);
|
||||
count++;
|
||||
}
|
||||
|
||||
sqlite3_exec(db_get_conn(), "COMMIT;", NULL, NULL, NULL);
|
||||
|
||||
fclose(fp);
|
||||
free(path);
|
||||
printf("Imported %d entries from zsh history.\n", count);
|
||||
return true;
|
||||
}
|
||||
|
||||
bool history_record(const char *command, const char *cwd, int exit_code,
|
||||
int duration, const char *hostname, const char *session,
|
||||
const char *shell) {
|
||||
|
||||
HistoryEntry entry;
|
||||
memset(&entry, 0, sizeof(entry));
|
||||
|
||||
entry.command = (char*)command;
|
||||
entry.cwd = (char*)cwd;
|
||||
entry.exit_code = exit_code;
|
||||
entry.duration = duration;
|
||||
entry.hostname = (char*)hostname;
|
||||
entry.shell = (char*)shell;
|
||||
entry.session = (char*)session;
|
||||
entry.timestamp = current_timestamp_sec();
|
||||
|
||||
struct passwd *pw = getpwuid(getuid());
|
||||
entry.username = pw ? pw->pw_name : "unknown";
|
||||
|
||||
return db_add_history(&entry);
|
||||
}
|
||||
|
||||
bool history_import(const char *shell_name) {
|
||||
bool success = false;
|
||||
if (strcmp(shell_name, "bash") == 0) {
|
||||
success = import_bash();
|
||||
} else if (strcmp(shell_name, "zsh") == 0) {
|
||||
success = import_zsh();
|
||||
} else if (strcmp(shell_name, "auto") == 0) {
|
||||
bool b = import_bash();
|
||||
bool z = import_zsh();
|
||||
success = b || z;
|
||||
} else {
|
||||
printf("Shell import for '%s' not implemented yet.\n", shell_name);
|
||||
}
|
||||
return success;
|
||||
}
|
||||
|
||||
void history_stats(int limit) {
|
||||
if (limit <= 0) limit = 10;
|
||||
|
||||
sqlite3 *db = db_get_conn();
|
||||
if (!db) return;
|
||||
|
||||
const char *sql = "SELECT command, COUNT(*) as c FROM history GROUP BY command ORDER BY c DESC LIMIT ?;";
|
||||
sqlite3_stmt *stmt;
|
||||
|
||||
if (sqlite3_prepare_v2(db, sql, -1, &stmt, NULL) != SQLITE_OK) {
|
||||
fprintf(stderr, "Stats query failed: %s\n", sqlite3_errmsg(db));
|
||||
return;
|
||||
}
|
||||
|
||||
sqlite3_bind_int(stmt, 1, limit);
|
||||
|
||||
printf("Top %d commands:\n", limit);
|
||||
printf("%-10s %s\n", "Count", "Command");
|
||||
printf("------------------------\n");
|
||||
|
||||
while (sqlite3_step(stmt) == SQLITE_ROW) {
|
||||
const char *cmd = (const char*)sqlite3_column_text(stmt, 0);
|
||||
int count = sqlite3_column_int(stmt, 1);
|
||||
printf("%-10d %s\n", count, cmd);
|
||||
}
|
||||
|
||||
sqlite3_finalize(stmt);
|
||||
}
|
||||
@@ -0,0 +1,13 @@
|
||||
#ifndef RHISTORY_HISTORY_H
|
||||
#define RHISTORY_HISTORY_H
|
||||
|
||||
#include "db.h"
|
||||
|
||||
bool history_record(const char *command, const char *cwd, int exit_code,
|
||||
int duration, const char *hostname, const char *session,
|
||||
const char *shell);
|
||||
|
||||
bool history_import(const char *shell_name);
|
||||
void history_stats(int limit);
|
||||
|
||||
#endif
|
||||
+311
@@ -0,0 +1,311 @@
|
||||
#include <stdio.h>
|
||||
#include <stdlib.h>
|
||||
#include <string.h>
|
||||
#include <getopt.h>
|
||||
#include "db.h"
|
||||
#include "utils.h"
|
||||
#include "history.h"
|
||||
#include "config.h"
|
||||
#include "shell_integration.h"
|
||||
#include "tui.h"
|
||||
|
||||
#define RHISTORY_VERSION "0.1.0"
|
||||
|
||||
// Command handlers
|
||||
int cmd_search(int argc, char **argv);
|
||||
int cmd_import(int argc, char **argv);
|
||||
int cmd_list(int argc, char **argv);
|
||||
int cmd_stats(int argc, char **argv);
|
||||
int cmd_delete(int argc, char **argv);
|
||||
int cmd_init(int argc, char **argv);
|
||||
int cmd_config(int argc, char **argv);
|
||||
int cmd_record(int argc, char **argv); // Internal
|
||||
void print_help(void);
|
||||
void print_version(void);
|
||||
|
||||
int main(int argc, char **argv) {
|
||||
if (argc < 2) {
|
||||
return cmd_search(argc, argv);
|
||||
}
|
||||
|
||||
if (strcmp(argv[1], "search") == 0) {
|
||||
return cmd_search(argc - 1, argv + 1);
|
||||
} else if (strcmp(argv[1], "import") == 0) {
|
||||
return cmd_import(argc - 1, argv + 1);
|
||||
} else if (strcmp(argv[1], "list") == 0) {
|
||||
return cmd_list(argc - 1, argv + 1);
|
||||
} else if (strcmp(argv[1], "stats") == 0) {
|
||||
return cmd_stats(argc - 1, argv + 1);
|
||||
} else if (strcmp(argv[1], "delete") == 0) {
|
||||
return cmd_delete(argc - 1, argv + 1);
|
||||
} else if (strcmp(argv[1], "init") == 0) {
|
||||
return cmd_init(argc - 1, argv + 1);
|
||||
} else if (strcmp(argv[1], "config") == 0) {
|
||||
return cmd_config(argc - 1, argv + 1);
|
||||
} else if (strcmp(argv[1], "record") == 0) {
|
||||
return cmd_record(argc - 1, argv + 1);
|
||||
} else if (strcmp(argv[1], "help") == 0 || strcmp(argv[1], "--help") == 0 || strcmp(argv[1], "-h") == 0) {
|
||||
print_help();
|
||||
return 0;
|
||||
} else if (strcmp(argv[1], "version") == 0 || strcmp(argv[1], "--version") == 0 || strcmp(argv[1], "-v") == 0) {
|
||||
print_version();
|
||||
return 0;
|
||||
} else {
|
||||
// Default to search if unknown command, or maybe print help?
|
||||
// Prompt says "rhistory [OPTIONS] [COMMAND]".
|
||||
// If it starts with -, it's options for search probably.
|
||||
if (argv[1][0] == '-') {
|
||||
return cmd_search(argc, argv);
|
||||
}
|
||||
fprintf(stderr, "Unknown command: %s\n", argv[1]);
|
||||
print_help();
|
||||
return 1;
|
||||
}
|
||||
}
|
||||
|
||||
void print_help(void) {
|
||||
printf("rhistory %s\n", RHISTORY_VERSION);
|
||||
printf("Usage: rhistory [COMMAND] [OPTIONS]\n\n");
|
||||
printf("Commands:\n");
|
||||
printf(" search Search history (default)\n");
|
||||
printf(" import Import history from shell file\n");
|
||||
printf(" list List history entries\n");
|
||||
printf(" stats Show statistics\n");
|
||||
printf(" delete Delete entries\n");
|
||||
printf(" init Generate shell integration code\n");
|
||||
printf(" config Manage configuration\n");
|
||||
printf(" help Show this help\n");
|
||||
printf(" version Show version\n");
|
||||
}
|
||||
|
||||
void print_version(void) {
|
||||
printf("rhistory %s\n", RHISTORY_VERSION);
|
||||
}
|
||||
|
||||
// Database Helper
|
||||
bool setup_db(void) {
|
||||
char *data_dir = get_data_dir();
|
||||
if (!ensure_directory_exists(data_dir)) {
|
||||
fprintf(stderr, "Failed to create data directory: %s\n", data_dir);
|
||||
free(data_dir);
|
||||
return false;
|
||||
}
|
||||
|
||||
char *db_path = join_path(data_dir, "history.db");
|
||||
bool success = db_init(db_path);
|
||||
|
||||
free(db_path);
|
||||
free(data_dir);
|
||||
return success;
|
||||
}
|
||||
|
||||
// Stub implementations
|
||||
#include "shell_integration.h"
|
||||
#include "search.h"
|
||||
#include <time.h>
|
||||
|
||||
// ...
|
||||
|
||||
int cmd_search(int argc, char **argv) {
|
||||
if (!setup_db()) return 1;
|
||||
|
||||
SearchQuery query;
|
||||
memset(&query, 0, sizeof(query));
|
||||
query.exit_code = -1;
|
||||
query.limit = 20;
|
||||
|
||||
static struct option long_options[] = {
|
||||
{"interactive", no_argument, 0, 'i'},
|
||||
{"exit", required_argument, 0, 'e'},
|
||||
{"cwd", required_argument, 0, 'c'},
|
||||
{"host", required_argument, 0, 'h'},
|
||||
{"session", required_argument, 0, 's'},
|
||||
{"limit", required_argument, 0, 'l'},
|
||||
{"reverse", no_argument, 0, 'r'},
|
||||
{"cmd-only", no_argument, 0, 1001},
|
||||
{0, 0, 0, 0}
|
||||
};
|
||||
|
||||
bool interactive = false;
|
||||
bool cmd_only = false;
|
||||
|
||||
int opt_index = 0;
|
||||
int c;
|
||||
// Reset getopt
|
||||
optind = 1;
|
||||
|
||||
while ((c = getopt_long(argc, argv, "ie:c:h:s:l:r", long_options, &opt_index)) != -1) {
|
||||
switch (c) {
|
||||
case 'i': interactive = true; break;
|
||||
case 'e': query.exit_code = atoi(optarg); break;
|
||||
case 'c': query.cwd = optarg; break;
|
||||
case 'h': query.hostname = optarg; break;
|
||||
case 's': query.session = optarg; break;
|
||||
case 'l': query.limit = atoi(optarg); break;
|
||||
case 'r': query.reverse = true; break;
|
||||
case 1001: cmd_only = true; break;
|
||||
}
|
||||
}
|
||||
|
||||
if (optind < argc) {
|
||||
query.query_string = argv[optind];
|
||||
}
|
||||
|
||||
if (interactive) {
|
||||
tui_start(&query);
|
||||
} else {
|
||||
SearchResult *res = search_perform(&query);
|
||||
if (!res) {
|
||||
fprintf(stderr, "Search failed.\n");
|
||||
db_close();
|
||||
return 1;
|
||||
}
|
||||
|
||||
for (int i = 0; i < res->count; i++) {
|
||||
HistoryEntry *e = res->entries[i];
|
||||
if (cmd_only) {
|
||||
printf("%s\n", e->command);
|
||||
} else {
|
||||
// Format timestamp
|
||||
time_t t = (time_t)e->timestamp;
|
||||
struct tm *tm = localtime(&t);
|
||||
char time_str[64];
|
||||
strftime(time_str, sizeof(time_str), "%Y-%m-%d %H:%M:%S", tm);
|
||||
|
||||
printf("%s | %s | %s | %dms | %s\n",
|
||||
time_str, e->hostname, e->cwd, e->duration, e->command);
|
||||
}
|
||||
}
|
||||
|
||||
search_free_result(res);
|
||||
}
|
||||
|
||||
db_close();
|
||||
return 0;
|
||||
}
|
||||
|
||||
int cmd_import(int argc, char **argv) {
|
||||
if (!setup_db()) return 1;
|
||||
|
||||
const char *shell = "auto";
|
||||
if (argc > 1) {
|
||||
shell = argv[1];
|
||||
}
|
||||
|
||||
history_import(shell);
|
||||
|
||||
db_close();
|
||||
return 0;
|
||||
}
|
||||
|
||||
int cmd_list(int argc, char **argv) {
|
||||
if (!setup_db()) return 1;
|
||||
printf("List command not implemented yet.\n");
|
||||
db_close();
|
||||
return 0;
|
||||
}
|
||||
|
||||
int cmd_stats(int argc, char **argv) {
|
||||
if (!setup_db()) return 1;
|
||||
|
||||
int limit = 10;
|
||||
|
||||
static struct option long_options[] = {
|
||||
{"top", required_argument, 0, 't'},
|
||||
{0, 0, 0, 0}
|
||||
};
|
||||
|
||||
int opt_index = 0;
|
||||
int c;
|
||||
optind = 1;
|
||||
|
||||
while ((c = getopt_long(argc, argv, "t:", long_options, &opt_index)) != -1) {
|
||||
switch (c) {
|
||||
case 't': limit = atoi(optarg); break;
|
||||
}
|
||||
}
|
||||
|
||||
history_stats(limit);
|
||||
|
||||
db_close();
|
||||
return 0;
|
||||
}
|
||||
|
||||
int cmd_delete(int argc, char **argv) {
|
||||
if (!setup_db()) return 1;
|
||||
printf("Delete command not implemented yet.\n");
|
||||
db_close();
|
||||
return 0;
|
||||
}
|
||||
|
||||
#include "shell_integration.h"
|
||||
|
||||
// ... (other includes)
|
||||
|
||||
int cmd_init(int argc, char **argv) {
|
||||
if (argc > 1) {
|
||||
if (!shell_integration_init(argv[1])) {
|
||||
fprintf(stderr, "Unsupported shell: %s. Supported: bash, zsh, fish\n", argv[1]);
|
||||
return 1;
|
||||
}
|
||||
} else {
|
||||
printf("Usage: rhistory init [bash|zsh|fish]\n");
|
||||
return 1;
|
||||
}
|
||||
return 0;
|
||||
}
|
||||
|
||||
int cmd_config(int argc, char **argv) {
|
||||
printf("Config command not implemented yet.\n");
|
||||
return 0;
|
||||
}
|
||||
|
||||
int cmd_record(int argc, char **argv) {
|
||||
if (!setup_db()) return 1;
|
||||
|
||||
char *command = NULL;
|
||||
char *cwd = NULL;
|
||||
int exit_code = 0;
|
||||
int duration = 0;
|
||||
char *hostname = NULL;
|
||||
char *session = NULL;
|
||||
char *shell = NULL;
|
||||
|
||||
static struct option long_options[] = {
|
||||
{"command", required_argument, 0, 'c'},
|
||||
{"cwd", required_argument, 0, 'w'},
|
||||
{"exit-code", required_argument, 0, 'e'},
|
||||
{"duration", required_argument, 0, 'd'},
|
||||
{"hostname", required_argument, 0, 'H'},
|
||||
{"session", required_argument, 0, 's'},
|
||||
{"shell", required_argument, 0, 'S'},
|
||||
{0, 0, 0, 0}
|
||||
};
|
||||
|
||||
int opt_index = 0;
|
||||
int c;
|
||||
while ((c = getopt_long(argc, argv, "", long_options, &opt_index)) != -1) {
|
||||
switch (c) {
|
||||
case 'c': command = optarg; break;
|
||||
case 'w': cwd = optarg; break;
|
||||
case 'e': exit_code = atoi(optarg); break;
|
||||
case 'd': duration = atoi(optarg); break;
|
||||
case 'H': hostname = optarg; break;
|
||||
case 's': session = optarg; break;
|
||||
case 'S': shell = optarg; break;
|
||||
}
|
||||
}
|
||||
|
||||
if (command && cwd) {
|
||||
if (history_record(command, cwd, exit_code, duration, hostname, session, shell)) {
|
||||
// success, silent
|
||||
} else {
|
||||
fprintf(stderr, "Failed to record history.\n");
|
||||
}
|
||||
} else {
|
||||
fprintf(stderr, "Missing required arguments for record.\n");
|
||||
}
|
||||
|
||||
db_close();
|
||||
return 0;
|
||||
}
|
||||
+114
@@ -0,0 +1,114 @@
|
||||
#include "search.h"
|
||||
#include <stdio.h>
|
||||
#include <stdlib.h>
|
||||
#include <string.h>
|
||||
|
||||
SearchResult *search_perform(SearchQuery *query) {
|
||||
if (!query) return NULL;
|
||||
|
||||
sqlite3 *db = db_get_conn();
|
||||
if (!db) return NULL;
|
||||
|
||||
// Build SQL
|
||||
// We'll use a dynamic string or large buffer.
|
||||
// Given the simplicity, a large stack buffer is okay for now, but dynamic is safer.
|
||||
char sql[4096];
|
||||
char where_clause[2048];
|
||||
where_clause[0] = 0;
|
||||
|
||||
strcat(where_clause, " WHERE 1=1");
|
||||
|
||||
if (query->query_string && strlen(query->query_string) > 0) {
|
||||
// Simple LIKE search for now. FTS comes later.
|
||||
strcat(where_clause, " AND command LIKE ?");
|
||||
}
|
||||
if (query->cwd) {
|
||||
strcat(where_clause, " AND cwd = ?");
|
||||
}
|
||||
if (query->hostname) {
|
||||
strcat(where_clause, " AND hostname = ?");
|
||||
}
|
||||
if (query->session) {
|
||||
strcat(where_clause, " AND session = ?");
|
||||
}
|
||||
if (query->exit_code != -1) {
|
||||
strcat(where_clause, " AND exit_code = ?");
|
||||
}
|
||||
|
||||
const char *order_dir = query->reverse ? "ASC" : "DESC"; // Newest first (DESC) default?
|
||||
// Prompt says: "-r, --reverse Reverse order (oldest first)"
|
||||
// So default should be DESC (newest). Reverse -> ASC (oldest).
|
||||
|
||||
snprintf(sql, sizeof(sql),
|
||||
"SELECT id, command, cwd, exit_code, duration, hostname, username, shell, session, timestamp "
|
||||
"FROM history %s ORDER BY timestamp %s LIMIT ? OFFSET ?;",
|
||||
where_clause, order_dir);
|
||||
|
||||
sqlite3_stmt *stmt;
|
||||
if (sqlite3_prepare_v2(db, sql, -1, &stmt, NULL) != SQLITE_OK) {
|
||||
fprintf(stderr, "Search prepare failed: %s\n", sqlite3_errmsg(db));
|
||||
return NULL;
|
||||
}
|
||||
|
||||
int idx = 1;
|
||||
if (query->query_string && strlen(query->query_string) > 0) {
|
||||
// Add % around query for fuzzy/contains
|
||||
char pattern[1024];
|
||||
snprintf(pattern, sizeof(pattern), "%%%s%%", query->query_string);
|
||||
sqlite3_bind_text(stmt, idx++, pattern, -1, SQLITE_TRANSIENT);
|
||||
}
|
||||
if (query->cwd) {
|
||||
sqlite3_bind_text(stmt, idx++, query->cwd, -1, SQLITE_STATIC);
|
||||
}
|
||||
if (query->hostname) {
|
||||
sqlite3_bind_text(stmt, idx++, query->hostname, -1, SQLITE_STATIC);
|
||||
}
|
||||
if (query->session) {
|
||||
sqlite3_bind_text(stmt, idx++, query->session, -1, SQLITE_STATIC);
|
||||
}
|
||||
if (query->exit_code != -1) {
|
||||
sqlite3_bind_int(stmt, idx++, query->exit_code);
|
||||
}
|
||||
|
||||
sqlite3_bind_int(stmt, idx++, query->limit > 0 ? query->limit : 100);
|
||||
sqlite3_bind_int(stmt, idx++, query->offset);
|
||||
|
||||
// Execute
|
||||
SearchResult *result = calloc(1, sizeof(SearchResult));
|
||||
// Initial capacity
|
||||
int capacity = 100;
|
||||
result->entries = malloc(capacity * sizeof(HistoryEntry*));
|
||||
|
||||
while (sqlite3_step(stmt) == SQLITE_ROW) {
|
||||
if (result->count >= capacity) {
|
||||
capacity *= 2;
|
||||
result->entries = realloc(result->entries, capacity * sizeof(HistoryEntry*));
|
||||
}
|
||||
|
||||
HistoryEntry *entry = calloc(1, sizeof(HistoryEntry));
|
||||
entry->id = sqlite3_column_int64(stmt, 0);
|
||||
entry->command = strdup((const char*)sqlite3_column_text(stmt, 1));
|
||||
entry->cwd = strdup((const char*)sqlite3_column_text(stmt, 2));
|
||||
entry->exit_code = sqlite3_column_int(stmt, 3);
|
||||
entry->duration = sqlite3_column_int(stmt, 4);
|
||||
entry->hostname = strdup((const char*)sqlite3_column_text(stmt, 5));
|
||||
entry->username = strdup((const char*)sqlite3_column_text(stmt, 6));
|
||||
entry->shell = strdup((const char*)sqlite3_column_text(stmt, 7));
|
||||
entry->session = strdup((const char*)sqlite3_column_text(stmt, 8));
|
||||
entry->timestamp = sqlite3_column_int64(stmt, 9);
|
||||
|
||||
result->entries[result->count++] = entry;
|
||||
}
|
||||
|
||||
sqlite3_finalize(stmt);
|
||||
return result;
|
||||
}
|
||||
|
||||
void search_free_result(SearchResult *result) {
|
||||
if (!result) return;
|
||||
for (int i = 0; i < result->count; i++) {
|
||||
db_free_entry(result->entries[i]);
|
||||
}
|
||||
free(result->entries);
|
||||
free(result);
|
||||
}
|
||||
@@ -0,0 +1,26 @@
|
||||
#ifndef RHISTORY_SEARCH_H
|
||||
#define RHISTORY_SEARCH_H
|
||||
|
||||
#include "db.h"
|
||||
#include <stdbool.h>
|
||||
|
||||
typedef struct {
|
||||
char *query_string; // if NULL/empty, list all
|
||||
char *cwd;
|
||||
char *hostname;
|
||||
char *session;
|
||||
int exit_code; // -1 if ignored
|
||||
int limit;
|
||||
int offset;
|
||||
bool reverse; // false = newest first, true = oldest first
|
||||
} SearchQuery;
|
||||
|
||||
typedef struct {
|
||||
HistoryEntry **entries;
|
||||
int count;
|
||||
} SearchResult;
|
||||
|
||||
SearchResult *search_perform(SearchQuery *query);
|
||||
void search_free_result(SearchResult *result);
|
||||
|
||||
#endif
|
||||
@@ -0,0 +1,163 @@
|
||||
#include "shell_integration.h"
|
||||
#include <stdio.h>
|
||||
#include <string.h>
|
||||
|
||||
static void init_bash(void) {
|
||||
printf(
|
||||
"# rhistory bash integration\n"
|
||||
"if [ -n \"$BASH_VERSION\" ]; then\n"
|
||||
" export __rhistory_session=\"$(head -c 16 /dev/urandom | base64 | tr -d '/+')\"\n"
|
||||
"\n"
|
||||
" __rhistory_preexec() {\n"
|
||||
" __rhistory_start_time=$(date +%%s%%N)\n"
|
||||
" }\n"
|
||||
"\n"
|
||||
" __rhistory_precmd() {\n"
|
||||
" local exit_code=$?\n"
|
||||
" local duration=0\n"
|
||||
" if [ -n \"$__rhistory_start_time\" ]; then\n"
|
||||
" local now=$(date +%%s%%N)\n"
|
||||
" duration=$(( (now - __rhistory_start_time) / 1000000 ))\n"
|
||||
" fi\n"
|
||||
" \n"
|
||||
" local cmd=$(history 1 | sed -e 's/^[ ]*[0-9]*[ ]*//')\n"
|
||||
" \n"
|
||||
" rhistory record \\\n"
|
||||
" --command \"$cmd\" \\\n"
|
||||
" --cwd \"$PWD\" \\\n"
|
||||
" --exit-code $exit_code \\\n"
|
||||
" --duration $duration \\\n"
|
||||
" --hostname \"$HOSTNAME\" \\\n"
|
||||
" --session \"$__rhistory_session\" \\\n"
|
||||
" --shell \"bash\"\n"
|
||||
" \n"
|
||||
" unset __rhistory_start_time\n"
|
||||
" }\n"
|
||||
"\n"
|
||||
" if [[ -z \"$PROMPT_COMMAND\" ]]; then\n"
|
||||
" PROMPT_COMMAND=\"__rhistory_precmd\"\n"
|
||||
" else\n"
|
||||
" PROMPT_COMMAND=\"__rhistory_precmd; $PROMPT_COMMAND\"\n"
|
||||
" fi\n"
|
||||
" \n"
|
||||
" trap '__rhistory_preexec' DEBUG\n"
|
||||
"\n"
|
||||
" # Interactive Search Binding (Ctrl+R)\n"
|
||||
" __rhistory_search() {\n"
|
||||
" local output\n"
|
||||
" output=$(rhistory search -i)\n"
|
||||
" if [ -n \"$output\" ]; then\n"
|
||||
" READLINE_LINE=\"$output\"\n"
|
||||
" READLINE_POINT=${#output}\n"
|
||||
" fi\n"
|
||||
" }\n"
|
||||
" bind -x '\"\\C-r\": __rhistory_search'\n"
|
||||
"fi\n"
|
||||
);
|
||||
}
|
||||
|
||||
static void init_zsh(void) {
|
||||
printf(
|
||||
"# rhistory zsh integration\n"
|
||||
"export __rhistory_session=\"$(head -c 16 /dev/urandom | base64 | tr -d '/+')\"\n"
|
||||
"\n"
|
||||
"__rhistory_preexec() {\n"
|
||||
" __rhistory_start_time=$(($(date +%%s%%N)/1000000))\n"
|
||||
"}\n"
|
||||
"\n"
|
||||
"__rhistory_precmd() {\n"
|
||||
" local exit_code=$?\n"
|
||||
" local duration=0\n"
|
||||
" if [ -n \"$__rhistory_start_time\" ]; then\n"
|
||||
" local now=$(($(date +%%s%%N)/1000000))\n"
|
||||
" duration=$((now - __rhistory_start_time))\n"
|
||||
" fi\n"
|
||||
"\n"
|
||||
" local cmd=$(fc -ln -1)\n"
|
||||
"\n"
|
||||
" rhistory record \\n"
|
||||
" --command \"$cmd\" \\n"
|
||||
" --cwd \"$PWD\" \\n"
|
||||
" --exit-code $exit_code \\n"
|
||||
" --duration $duration \\n"
|
||||
" --hostname \"$HOST\" \\n"
|
||||
" --session \"$__rhistory_session\" \\n"
|
||||
" --shell \"zsh\"\n"
|
||||
" \n"
|
||||
" unset __rhistory_start_time\n"
|
||||
"}\n"
|
||||
"\n"
|
||||
"autoload -Uz add-zsh-hook\n"
|
||||
"add-zsh-hook preexec __rhistory_preexec\n"
|
||||
"add-zsh-hook precmd __rhistory_precmd\n"
|
||||
"\n"
|
||||
"# Interactive Search Binding (Ctrl+R)\n"
|
||||
"__rhistory_search_widget() {\n"
|
||||
" local output\n"
|
||||
" # Ensure tty is available for TUI, while capturing stdout\n"
|
||||
" output=$(rhistory search -i < $TTY)\n"
|
||||
" if [[ -n \"$output\" ]]; then\n"
|
||||
" BUFFER=\"$output\"\n"
|
||||
" CURSOR=${#BUFFER}\n"
|
||||
" fi\n"
|
||||
" zle reset-prompt\n"
|
||||
"}\n"
|
||||
"zle -N __rhistory_search_widget\n"
|
||||
"bindkey '^R' __rhistory_search_widget\n"
|
||||
);
|
||||
}
|
||||
|
||||
static void init_fish(void) {
|
||||
printf(
|
||||
"# rhistory fish integration\n"
|
||||
"set -gx __rhistory_session (random)\n"
|
||||
"\n"
|
||||
"function __rhistory_preexec --on-event fish_preexec\n"
|
||||
" set -gx __rhistory_start_time (date +%%s%%N)\n"
|
||||
"end\n"
|
||||
"\n"
|
||||
"function __rhistory_postexec --on-event fish_postexec\n"
|
||||
" set -l exit_code $status\n"
|
||||
" set -l duration 0\n"
|
||||
" if set -q __rhistory_start_time\n"
|
||||
" set -l now (date +%%s%%N)\n"
|
||||
" set duration (math \"($now - $__rhistory_start_time) / 1000000\")\n"
|
||||
" end\n"
|
||||
"\n"
|
||||
" rhistory record \\n"
|
||||
" --command \"$argv\" \\n"
|
||||
" --cwd \"$PWD\" \\n"
|
||||
" --exit-code $exit_code \\n"
|
||||
" --duration $duration \\n"
|
||||
" --hostname \"$hostname\" \\n"
|
||||
" --session \"$__rhistory_session\" \\n"
|
||||
" --shell \"fish\"\n"
|
||||
" \n"
|
||||
" set -e __rhistory_start_time\n"
|
||||
"end\n"
|
||||
"\n"
|
||||
"function __rhistory_search\n"
|
||||
" set cmd (rhistory search -i)\n"
|
||||
" if test -n \"$cmd\"\n"
|
||||
" commandline -r \"$$cmd\"\n"
|
||||
" end\n"
|
||||
" commandline -f repaint\n"
|
||||
"end\n"
|
||||
"bind \\cr __rhistory_search\n"
|
||||
);
|
||||
}
|
||||
|
||||
bool shell_integration_init(const char *shell) {
|
||||
if (strcmp(shell, "bash") == 0) {
|
||||
init_bash();
|
||||
return true;
|
||||
} else if (strcmp(shell, "zsh") == 0) {
|
||||
init_zsh();
|
||||
return true;
|
||||
} else if (strcmp(shell, "fish") == 0) {
|
||||
init_fish();
|
||||
return true;
|
||||
} else {
|
||||
return false;
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,8 @@
|
||||
#ifndef RHISTORY_SHELL_INTEGRATION_H
|
||||
#define RHISTORY_SHELL_INTEGRATION_H
|
||||
|
||||
#include <stdbool.h>
|
||||
|
||||
bool shell_integration_init(const char *shell);
|
||||
|
||||
#endif
|
||||
@@ -0,0 +1,153 @@
|
||||
#include "tui.h"
|
||||
#include <ncurses.h>
|
||||
#include <ctype.h>
|
||||
#include <string.h>
|
||||
#include <stdlib.h>
|
||||
#include <unistd.h>
|
||||
|
||||
int tui_start(SearchQuery *initial_query) {
|
||||
FILE *tty = fopen("/dev/tty", "r+");
|
||||
if (!tty) {
|
||||
// Fallback or error
|
||||
return 0;
|
||||
}
|
||||
|
||||
SCREEN *scr = newterm(NULL, tty, tty);
|
||||
if (!scr) {
|
||||
fclose(tty);
|
||||
return 0;
|
||||
}
|
||||
set_term(scr);
|
||||
|
||||
cbreak();
|
||||
noecho();
|
||||
keypad(stdscr, TRUE);
|
||||
curs_set(1);
|
||||
|
||||
start_color();
|
||||
init_pair(1, COLOR_CYAN, COLOR_BLACK); // Time
|
||||
init_pair(2, COLOR_GREEN, COLOR_BLACK); // Command
|
||||
init_pair(3, COLOR_BLACK, COLOR_WHITE); // Selected
|
||||
|
||||
char query_buf[1024] = {0};
|
||||
if (initial_query && initial_query->query_string) {
|
||||
strncpy(query_buf, initial_query->query_string, sizeof(query_buf)-1);
|
||||
}
|
||||
int query_len = strlen(query_buf);
|
||||
|
||||
int selected_idx = 0;
|
||||
int scroll_offset = 0; // for list view
|
||||
SearchResult *current_results = NULL;
|
||||
|
||||
bool running = true;
|
||||
char *selected_command = NULL;
|
||||
|
||||
// Base query struct
|
||||
SearchQuery query = {0};
|
||||
if (initial_query) query = *initial_query;
|
||||
query.limit = 100; // Load enough for scrolling
|
||||
|
||||
while (running) {
|
||||
// 1. Perform Search
|
||||
if (current_results) search_free_result(current_results);
|
||||
query.query_string = query_buf;
|
||||
current_results = search_perform(&query);
|
||||
|
||||
// 2. Draw
|
||||
clear();
|
||||
int height, width;
|
||||
getmaxyx(stdscr, height, width);
|
||||
|
||||
// Draw input at bottom
|
||||
mvprintw(height - 1, 0, "> %s", query_buf);
|
||||
|
||||
// Draw list (bottom-up like atuin?)
|
||||
// Atuin draws list above prompt, newest at bottom usually.
|
||||
// Let's draw list from height-2 upwards.
|
||||
|
||||
int list_area_height = height - 2;
|
||||
if (current_results && current_results->count > 0) {
|
||||
// Adjust selection bounds
|
||||
if (selected_idx >= current_results->count) selected_idx = current_results->count - 1;
|
||||
if (selected_idx < 0) selected_idx = 0;
|
||||
|
||||
// Calculate scroll
|
||||
// We want selected_idx to be visible.
|
||||
// But here we might want to keep the list fixed?
|
||||
// Let's just list from top down for simplicity first.
|
||||
// Row 0 to list_area_height
|
||||
|
||||
for (int i = 0; i < list_area_height && i < current_results->count; i++) {
|
||||
int entry_idx = i; // simple
|
||||
if (entry_idx >= current_results->count) break;
|
||||
|
||||
HistoryEntry *e = current_results->entries[entry_idx];
|
||||
|
||||
int row = i; // top down
|
||||
|
||||
if (entry_idx == selected_idx) {
|
||||
attron(COLOR_PAIR(3));
|
||||
mvprintw(row, 0, "% - *s", width, ""); // clear line background
|
||||
mvprintw(row, 0, "%s", e->command);
|
||||
attroff(COLOR_PAIR(3));
|
||||
} else {
|
||||
attron(COLOR_PAIR(2));
|
||||
mvprintw(row, 0, "%s", e->command);
|
||||
attroff(COLOR_PAIR(2));
|
||||
}
|
||||
|
||||
// Print some metadata at the right
|
||||
char meta[64];
|
||||
snprintf(meta, sizeof(meta), "%dms", e->duration);
|
||||
mvprintw(row, width - strlen(meta) - 1, "%s", meta);
|
||||
}
|
||||
} else {
|
||||
mvprintw(0, 0, "No results.");
|
||||
}
|
||||
|
||||
refresh();
|
||||
|
||||
// 3. Input
|
||||
int ch = getch();
|
||||
if (ch == 27) { // ESC
|
||||
// Could be alt key etc. assuming ESC for now.
|
||||
running = false;
|
||||
} else if (ch == KEY_UP) {
|
||||
if (selected_idx > 0) selected_idx--;
|
||||
} else if (ch == KEY_DOWN) {
|
||||
if (current_results && selected_idx < current_results->count - 1) selected_idx++;
|
||||
} else if (ch == KEY_BACKSPACE || ch == 127) {
|
||||
if (query_len > 0) {
|
||||
query_buf[--query_len] = 0;
|
||||
selected_idx = 0;
|
||||
}
|
||||
} else if (ch == '\n') { // Enter
|
||||
if (current_results && current_results->count > 0) {
|
||||
selected_command = strdup(current_results->entries[selected_idx]->command);
|
||||
running = false;
|
||||
}
|
||||
} else if (isprint(ch)) {
|
||||
if (query_len < (int)sizeof(query_buf) - 1) {
|
||||
query_buf[query_len++] = ch;
|
||||
query_buf[query_len] = 0;
|
||||
selected_idx = 0;
|
||||
}
|
||||
} else if (ch == 3) { // Ctrl+C
|
||||
running = false;
|
||||
}
|
||||
}
|
||||
|
||||
if (current_results) search_free_result(current_results);
|
||||
|
||||
endwin();
|
||||
delscreen(scr);
|
||||
fclose(tty);
|
||||
|
||||
if (selected_command) {
|
||||
printf("%s", selected_command); // Print to stdout for shell to pick up
|
||||
free(selected_command);
|
||||
return 1;
|
||||
}
|
||||
|
||||
return 0;
|
||||
}
|
||||
@@ -0,0 +1,9 @@
|
||||
#ifndef RHISTORY_TUI_H
|
||||
#define RHISTORY_TUI_H
|
||||
|
||||
#include "search.h"
|
||||
|
||||
// Starts the TUI. Returns 0 if cancelled, 1 if selected (printed to stdout).
|
||||
int tui_start(SearchQuery *initial_query);
|
||||
|
||||
#endif
|
||||
+101
@@ -0,0 +1,101 @@
|
||||
#include "utils.h"
|
||||
#include <stdio.h>
|
||||
#include <stdlib.h>
|
||||
#include <string.h>
|
||||
#include <sys/stat.h>
|
||||
#include <sys/types.h>
|
||||
#include <time.h>
|
||||
#include <unistd.h>
|
||||
#include <pwd.h>
|
||||
#include <errno.h>
|
||||
|
||||
char *get_home_dir(void) {
|
||||
char *home = getenv("HOME");
|
||||
if (home == NULL) {
|
||||
struct passwd *pw = getpwuid(getuid());
|
||||
if (pw) {
|
||||
home = pw->pw_dir;
|
||||
}
|
||||
}
|
||||
return home ? strdup(home) : NULL;
|
||||
}
|
||||
|
||||
char *get_data_dir(void) {
|
||||
char *home = get_home_dir();
|
||||
if (!home) return NULL;
|
||||
|
||||
// ~/.local/share/rhistory
|
||||
size_t len = strlen(home) + 30;
|
||||
char *path = malloc(len);
|
||||
if (path) {
|
||||
snprintf(path, len, "%s/.local/share/rhistory", home);
|
||||
}
|
||||
free(home);
|
||||
return path;
|
||||
}
|
||||
|
||||
char *get_config_dir(void) {
|
||||
char *home = get_home_dir();
|
||||
if (!home) return NULL;
|
||||
|
||||
// ~/.config/rhistory
|
||||
size_t len = strlen(home) + 25;
|
||||
char *path = malloc(len);
|
||||
if (path) {
|
||||
snprintf(path, len, "%s/.config/rhistory", home);
|
||||
}
|
||||
free(home);
|
||||
return path;
|
||||
}
|
||||
|
||||
bool ensure_directory_exists(const char *path) {
|
||||
if (!path) return false;
|
||||
|
||||
char tmp[1024];
|
||||
char *p = NULL;
|
||||
size_t len;
|
||||
|
||||
snprintf(tmp, sizeof(tmp), "%s", path);
|
||||
len = strlen(tmp);
|
||||
if (tmp[len - 1] == '/')
|
||||
tmp[len - 1] = 0;
|
||||
|
||||
for (p = tmp + 1; *p; p++) {
|
||||
if (*p == '/') {
|
||||
*p = 0;
|
||||
if (mkdir(tmp, 0755) != 0 && errno != EEXIST) {
|
||||
return false;
|
||||
}
|
||||
*p = '/';
|
||||
}
|
||||
}
|
||||
if (mkdir(tmp, 0755) != 0 && errno != EEXIST) {
|
||||
return false;
|
||||
}
|
||||
return true;
|
||||
}
|
||||
|
||||
char *join_path(const char *dir, const char *filename) {
|
||||
if (!dir || !filename) return NULL;
|
||||
size_t len = strlen(dir) + strlen(filename) + 2;
|
||||
char *path = malloc(len);
|
||||
if (path) {
|
||||
snprintf(path, len, "%s/%s", dir, filename);
|
||||
}
|
||||
return path;
|
||||
}
|
||||
|
||||
int64_t current_timestamp_ms(void) {
|
||||
struct timespec ts;
|
||||
clock_gettime(CLOCK_REALTIME, &ts);
|
||||
return (int64_t)ts.tv_sec * 1000 + ts.tv_nsec / 1000000;
|
||||
}
|
||||
|
||||
int64_t current_timestamp_sec(void) {
|
||||
return (int64_t)time(NULL);
|
||||
}
|
||||
|
||||
char *strdup_safe(const char *s) {
|
||||
if (!s) return NULL;
|
||||
return strdup(s);
|
||||
}
|
||||
+21
@@ -0,0 +1,21 @@
|
||||
#ifndef RHISTORY_UTILS_H
|
||||
#define RHISTORY_UTILS_H
|
||||
|
||||
#include <stdint.h>
|
||||
#include <stdbool.h>
|
||||
|
||||
// Path handling
|
||||
char *get_home_dir(void);
|
||||
char *get_data_dir(void);
|
||||
char *get_config_dir(void);
|
||||
bool ensure_directory_exists(const char *path);
|
||||
char *join_path(const char *dir, const char *filename);
|
||||
|
||||
// Time
|
||||
int64_t current_timestamp_ms(void);
|
||||
int64_t current_timestamp_sec(void);
|
||||
|
||||
// String utils
|
||||
char *strdup_safe(const char *s);
|
||||
|
||||
#endif
|
||||
Reference in New Issue
Block a user