项目作者: AlexanderThaller

项目描述 :
Better history management for zsh. Based on ideas from https://github.com/larkery/zsh-histdb.
高级语言: Rust
项目地址: git://github.com/AlexanderThaller/histdb-rs.git
创建时间: 2020-10-12T11:25:14Z
项目社区:https://github.com/AlexanderThaller/histdb-rs

开源协议:MIT License

下载


hstdb

Rust
crates.io

Better history management for zsh. Based on ideas from
https://github.com/larkery/zsh-histdb.

Licensed under MIT.

It was mainly written because the sqlite merging broke a few to many times for
me and using a sqlite database seemed overkill.

The tool is just writing CSV files for each host which makes syncing them via
git pretty painless.

Has pretty much the same feature set as zsh-histdb:

  • Start and stop time of the command
  • Working directory in which the command was run
  • Hostname of the machine the command was run in
  • Unique session ids based on UUIDs
  • Exit status of the command
  • Import from zsh histfile and zsh-histdb sqlite database

Installation

You can either install the right binary from the releases page or run:

  1. cargo install hstdb

Archlinux

Install from AUR:

First Start

After you installed hstdb you need to start the server:

  1. hstdb server

By default the server will run in the foreground.

To stop the server you can run the following:

  1. hstdb stop

Or send SIGTERM/SIGINT (Ctrl+C) to stop the server.

You can also use the systemd unit file in
hstdb.service which you can copy to
"$XDG_CONFIG_HOME/systemd (usually $HOME/.config/systemd) and
enable/start with the following:

  1. systemctl --user daemon-reload
  2. systemctl --user enable hstdb.service
  3. systemctl --user start hstdb.service

After that you can add the following to your .zshrc to enable hstdb for
you shell.

  1. eval "$(hstdb init)"

You can run that in your current shell to enable hstdb or restart your
shell.

Usage

Help output of default command:

  1. hstdb 2.1.0
  2. Better history management for zsh. Based on ideas from
  3. [https://github.com/larkery/zsh-histdb](https://github.com/larkery/zsh-histdb).
  4. USAGE:
  5. hstdb [OPTIONS] [SUBCOMMAND]
  6. OPTIONS:
  7. --all-hosts
  8. Print all hosts
  9. -c, --command <COMMAND>
  10. Only print entries beginning with the given command
  11. --config-path <CONFIG_PATH>
  12. Path to the socket for communication with the server [env: HISTDBRS_CONFIG_PATH=]
  13. [default: $XDG_CONFIG_HOME/hstdb/config.toml]
  14. -d, --data-dir <DATA_DIR>
  15. Path to folder in which to store the history files [default:
  16. $XDG_DATA_HOME/hstdb]
  17. --disable-formatting
  18. Disable fancy formatting
  19. -e, --entries-count <ENTRIES_COUNT>
  20. How many entries to print [default: 25]
  21. -f, --folder <FOLDER>
  22. Only print entries that have been executed in the given directory
  23. --filter-failed
  24. Filter out failed commands (return code not 0)
  25. --find-status <FIND_STATUS>
  26. Find commands with the given return code
  27. -h, --help
  28. Print help information
  29. --hide-header
  30. Disable printing of header
  31. --hostname <HOSTNAME>
  32. Filter by given hostname
  33. -i, --in
  34. Only print entries that have been executed in the current directory
  35. --no-subdirs
  36. Exclude subdirectories when filtering by folder
  37. --session <SESSION>
  38. Filter by given session
  39. --show-duration
  40. Show how long the command ran
  41. --show-host
  42. Print host column
  43. --show-pwd
  44. Show directory in which the command was run
  45. --show-session
  46. Show session id for command
  47. --show-status
  48. Print returncode of command
  49. -t, --text <COMMAND_TEXT>
  50. Only print entries containing the given regex
  51. -T, --text_excluded <COMMAND_TEXT_EXCLUDED>
  52. Only print entries not containing the given regex
  53. -V, --version
  54. Print version information
  55. SUBCOMMANDS:
  56. bench
  57. Run benchmark against server
  58. completion
  59. Generate autocomplete files for shells
  60. disable
  61. Disable history recording for current session
  62. enable
  63. Enable history recording for current session
  64. help
  65. Print this message or the help of the given subcommand(s)
  66. import
  67. Import entries from existing histdb sqlite or zsh histfile
  68. init
  69. Print out shell functions needed by histdb and set current session id
  70. precmd
  71. Finish command for current session
  72. server
  73. Start the server
  74. session_id
  75. Get new session id
  76. stop
  77. Stop the server
  78. zshaddhistory
  79. Add new command for current session

The most basic command ist just running hstdb without any arguments:

  1. » hstdb
  2. tmn cmd
  3. 14:28 cargo +nightly install --path .

That will print the history for the current machine. By default only the last
25 entries will be printed.

Git

hstdb was written to easily sync the history between multiple machines. For
that hstdb will write separate history files for each machine.

If you want to sync between machines go to the datadir (default is
$XDG_DATA_HOME/hstdb) and run the following commands:

  1. git init
  2. git add :/
  3. git commit -m "Initial commit"

After that you can configure origins and start syncing the files between
machines. There is no autocommit/autosync implemented as we don’t want to have
commits for each command run. This could be changed in the future.

Configuration

There is also a way to configure hstdb. By default the configuration
is stored under $XDG_CONFIG_HOME/hstdb/config.toml (usually
$HOME/.config/hstdb/config.toml). A different path can be specified
using the --config-path option.

The default configuration looks like this:

  1. # When true will not save commands that start with a space.
  2. # Default: true
  3. ignore_space = true
  4. # The log level to run under.
  5. # Default: Warn
  6. log_level = "Warn"

An example with all configuration options can be found in
config.toml.

Import

zsh-histdb

  1. » histdb import histdb -h
  2. hstdb-import-histdb 0.1.0
  3. Import entries from existing histdb sqlite file
  4. USAGE:
  5. hstdb import histdb [OPTIONS]
  6. FLAGS:
  7. -h, --help
  8. Prints help information
  9. OPTIONS:
  10. -d, --data-dir <data-dir>
  11. Path to folder in which to store the history files [default: $XDG_DATA_HOME/hstdb]
  12. -i, --import-file <import-file>
  13. Path to the existing histdb sqlite file [default: $HOME/.histdb/zsh-history.db]

If the defaults for the data-dir and the import-file are fine you can just
run the following command:

  1. histdb import histdb

This will create CSV files for each hostname found in the sqlite database. It
will create a UUID for each unique session found in sqlite so command run in the
same session should still be grouped together.

zsh histfile

  1. » histdb import histfile -h
  2. hstdb-import-histfile 0.1.0
  3. Import entries from existing zsh histfile
  4. USAGE:
  5. hstdb import histfile [OPTIONS]
  6. FLAGS:
  7. -h, --help
  8. Prints help information
  9. OPTIONS:
  10. -d, --data-dir <data-dir>
  11. Path to folder in which to store the history files [default: $XDG_DATA_HOME/hstdb]
  12. -i, --import-file <import-file>
  13. Path to the existing zsh histfile file [default: $HOME/.histfile]

If the defaults for the data-dir and the import-file are fine you can just
run the following command:

  1. histdb import histfile

As the information stored in the histfile is pretty limited the following
information will be stored:

  • time_finished will be parsed from the histfile
  • result (exit code) will be parsed from the histfile
  • command will be parsed from the histfile
  • time_start will be copied over from time_finished
  • hostname will use the current machines hostname
  • pwd will be set to the current users home directory
  • session_id will be generated and used for all commands imported from the
    histfile
  • user will use the current user thats running the import

Completion

Currentyl only zsh generation is enabled as other shells don’t make
sense at the moment.

Completion generation is provided through a subcommand:

  1. » hstdb completion -h
  2. hstdb-completion 2.1.0
  3. Generate autocomplete files for shells
  4. USAGE:
  5. hstdb completion <SHELL>
  6. ARGS:
  7. <SHELL>
  8. For which shell to generate the autocomplete [default: zsh] [possible values: zsh]
  9. OPTIONS:
  10. -h, --help
  11. Print help information
  12. -V, --version
  13. Print version information

Zsh

For zsh make sure your $fpath contains a folder you can write to:

  1. # add .zsh_completion to load additional zsh stuff
  2. export fpath=(~/.zsh_completion $fpath)

Then write the autocomplete file to that folder:

  1. hstdb completion zsh > ~/.zsh_completion/_hstdb

After that restart your shell which should now have working
autocompletion.

Contribution

I’m happy with how the tool works for me so I won’t expand it further but
contributions for features and fixes are always welcome!

Notes

Alternatives