Command-line history logging utilities
You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

50 lines
950 B

  1. #!/usr/bin/env perl
  2. =pod
  3. =head1 NAME
  4. commandlog-add - log command line history and context to an sqlite db
  5. =head1 SYNOPSIS
  6. # in zsh
  7. function preexec {
  8. commandlog add "$@"
  9. }
  10. =head1 AUTHOR
  11. Brennen Bearnes <bbearnes@gmail.com>
  12. =cut
  13. use warnings;
  14. use strict;
  15. use 5.10.0;
  16. use App::CommandLog;
  17. use Cwd;
  18. use Sys::Hostname;
  19. # Bail if logging is paused - can toggle this with commandlog-pause and
  20. # commandlog-resume:
  21. if (-e "$ENV{HOME}/commandlog-paused") {
  22. exit;
  23. }
  24. my $command = $ARGV[0];
  25. my $expanded_command = $ARGV[2];
  26. # Skip things prefixed with a space:
  27. if ($command =~ m/^[ ]+/) {
  28. exit;
  29. }
  30. my $dbh = App::CommandLog::get_dbh();
  31. my $sth = $dbh->prepare(q{
  32. INSERT INTO commands (command, expanded_command, path, hostname, username, shell, terminal, datetime)
  33. VALUES (?, ?, ?, ?, ?, ?, ?, datetime('now'))
  34. });
  35. $sth->execute($command, $expanded_command, cwd(), hostname(), $ENV{USER}, $ENV{SHELL}, $ENV{TERM});