tailgrab 1.0

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
checksums.yaml ADDED
@@ -0,0 +1,7 @@
1
+ ---
2
+ SHA256:
3
+ metadata.gz: a55734bb8ed595c37416cd8193218baf59c33031b0e08be308d63ee55d26aeb3
4
+ data.tar.gz: 5787a353f6a1a69865e566fb1a111e8a2fd5414b5b1627a03fd5ee951a525d64
5
+ SHA512:
6
+ metadata.gz: 998130391a27e5d839a4af4ae3ff4a2c95b45f01bfdc67f3bf6ed394edd77899f598109b730532adc00431680ac3fb91c916a8623c34ee534ff54e3ecc2cc1c6
7
+ data.tar.gz: a3b4e419e798cfe3d13e94012430c2c3156f27697d893251995842ede1d2bfcfe209dae1d47a9ea80ab38f2c2ced188afe494bfee6a963ee50b18a995e891824
data/.rspec ADDED
@@ -0,0 +1,3 @@
1
+ --format documentation
2
+ --color
3
+ --require spec_helper
data/.rubocop.yml ADDED
@@ -0,0 +1,23 @@
1
+ AllCops:
2
+ NewCops: enable
3
+ TargetRubyVersion: 3.0
4
+ SuggestExtensions: false
5
+
6
+ Style/StringLiterals:
7
+ EnforcedStyle: double_quotes
8
+
9
+ Style/StringLiteralsInInterpolation:
10
+ EnforcedStyle: double_quotes
11
+
12
+ Style/Documentation:
13
+ Enabled: false
14
+
15
+ Metrics/BlockLength:
16
+ Exclude:
17
+ - "spec/**/*"
18
+
19
+ Metrics/MethodLength:
20
+ Max: 15
21
+
22
+ Metrics/ClassLength:
23
+ Max: 125
data/CHANGELOG.md ADDED
@@ -0,0 +1,18 @@
1
+ ## [1.0] - 2026-02-28
2
+
3
+ - Complete rewrite of the CLI tool
4
+ - `grab N` saves last N commands from shell history with timestamps
5
+ - `grab last` copies last saved command to clipboard
6
+ - `grab last N` prints last N saved commands
7
+ - `grab all` prints all saved commands
8
+ - `grab day` shows commands saved in last 24 hours
9
+ - `grab wipe` clears all saved commands
10
+ - `grab help` shows usage information
11
+ - Cross-platform clipboard support (pbcopy, wl-copy, xclip, xsel)
12
+ - Reads zsh and bash history files directly
13
+ - Color-coded output with TTY detection
14
+ - Zero runtime dependencies
15
+
16
+ ## [0.1.0] - 2024-12-13
17
+
18
+ - Initial release
data/LICENSE.txt ADDED
@@ -0,0 +1,21 @@
1
+ The MIT License (MIT)
2
+
3
+ Copyright (c) 2024 Himanshu Panwar
4
+
5
+ Permission is hereby granted, free of charge, to any person obtaining a copy
6
+ of this software and associated documentation files (the "Software"), to deal
7
+ in the Software without restriction, including without limitation the rights
8
+ to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
9
+ copies of the Software, and to permit persons to whom the Software is
10
+ furnished to do so, subject to the following conditions:
11
+
12
+ The above copyright notice and this permission notice shall be included in
13
+ all copies or substantial portions of the Software.
14
+
15
+ THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
16
+ IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
17
+ FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
18
+ AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
19
+ LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
20
+ OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
21
+ THE SOFTWARE.
data/README.md ADDED
@@ -0,0 +1,44 @@
1
+ # Tailgrab
2
+
3
+ A CLI tool that reads your shell history (zsh/bash), saves commands to a local file
4
+ with timestamps, and lets you recall, filter, or copy them to your clipboard.
5
+
6
+ ## Install
7
+
8
+ ```
9
+ gem install tailgrab
10
+ ```
11
+
12
+ ## Usage
13
+
14
+ ```
15
+ grab 5 # Save last 5 commands from shell history
16
+ grab last # Copy last saved command to clipboard
17
+ grab last 3 # Show last 3 saved commands
18
+ grab all # Show all saved commands
19
+ grab day # Show commands saved in the last 24 hours
20
+ grab wipe # Clear all saved commands
21
+ grab help # Show help
22
+ ```
23
+
24
+ Commands are saved to `~/tailgrab.txt` with timestamps.
25
+
26
+ ## Shell Setup
27
+
28
+ For best results with multiple terminals, add to your `~/.zshrc`:
29
+
30
+ ```
31
+ setopt INC_APPEND_HISTORY
32
+ ```
33
+
34
+ ## Development
35
+
36
+ ```
37
+ bundle install
38
+ bundle exec rake # runs specs + rubocop
39
+ bundle exec rake install # install locally
40
+ ```
41
+
42
+ ## License
43
+
44
+ MIT
data/Rakefile ADDED
@@ -0,0 +1,12 @@
1
+ # frozen_string_literal: true
2
+
3
+ require "bundler/gem_tasks"
4
+ require "rspec/core/rake_task"
5
+
6
+ RSpec::Core::RakeTask.new(:spec)
7
+
8
+ require "rubocop/rake_task"
9
+
10
+ RuboCop::RakeTask.new
11
+
12
+ task default: %i[spec rubocop]
data/bin/grab ADDED
@@ -0,0 +1,6 @@
1
+ #!/usr/bin/env ruby
2
+ # frozen_string_literal: true
3
+
4
+ require "tailgrab"
5
+
6
+ Tailgrab::CLI.new(ARGV).run
@@ -0,0 +1,131 @@
1
+ # frozen_string_literal: true
2
+
3
+ module Tailgrab
4
+ class CLI
5
+ COMMANDS = %w[help last all day wipe].freeze
6
+ HELP_TEXT = <<~HELP
7
+ Tailgrab — Save and recall your terminal commands
8
+
9
+ Usage:
10
+ grab N Save last N commands from shell history
11
+ grab last Copy last saved command to clipboard
12
+ grab last N Show last N saved commands (N=1 copies to clipboard)
13
+ grab all Show all saved commands
14
+ grab day Show commands saved in the last 24 hours
15
+ grab wipe Clear all saved commands
16
+ grab help Show this help message
17
+
18
+ Examples:
19
+ grab 5 Save your last 5 terminal commands
20
+ grab last 3 Show the 3 most recently saved commands
21
+ grab last Copy the most recent saved command to clipboard
22
+
23
+ Tip: Add 'setopt INC_APPEND_HISTORY' to ~/.zshrc so commands from
24
+ all open terminals are available immediately.
25
+
26
+ File: ~/tailgrab.txt
27
+ HELP
28
+
29
+ def initialize(args, storage: Storage.new, history_reader: HistoryReader.new)
30
+ @args = args
31
+ @storage = storage
32
+ @history_reader = history_reader
33
+ end
34
+
35
+ def run
36
+ command = @args[0]
37
+
38
+ if command.nil? || command == "help"
39
+ print_help
40
+ elsif COMMANDS.include?(command)
41
+ send("handle_#{command}")
42
+ elsif command.match?(/\A\d+\z/)
43
+ handle_save(command.to_i)
44
+ else
45
+ puts "Unknown command: #{command}\n\n"
46
+ print_help
47
+ end
48
+ end
49
+
50
+ private
51
+
52
+ def handle_save(count)
53
+ return puts("Please provide a positive number.") if count < 1
54
+
55
+ commands = @history_reader.last(count)
56
+ return puts("No shell history found. Is your history file accessible?") if commands.empty?
57
+
58
+ @storage.save(commands)
59
+ noun = commands.length == 1 ? "command" : "commands"
60
+ puts "Saved #{commands.length} #{noun} to #{@storage.file_path}"
61
+ end
62
+
63
+ def handle_last
64
+ count = parse_last_count
65
+ return if count.nil?
66
+
67
+ entries = @storage.read_last(count)
68
+ return puts("No saved commands yet. Run 'grab N' to save some.") if entries.empty?
69
+
70
+ count == 1 ? copy_to_clipboard(entries.last) : print_entries("Last #{entries.length} saved commands:", entries)
71
+ end
72
+
73
+ def handle_day
74
+ entries = @storage.read_since(Time.now - 86_400)
75
+ return puts("No commands saved in the last 24 hours.") if entries.empty?
76
+
77
+ print_entries("Commands saved in the last 24 hours:", entries)
78
+ end
79
+
80
+ def handle_all
81
+ entries = @storage.read_all
82
+ return puts("No saved commands yet. Run 'grab N' to save some.") if entries.empty?
83
+
84
+ print_entries("All saved commands (#{entries.length}):", entries)
85
+ end
86
+
87
+ def handle_wipe
88
+ @storage.wipe
89
+ puts "All saved commands have been cleared."
90
+ end
91
+
92
+ def parse_last_count
93
+ raw = @args[1]
94
+ return 1 if raw.nil?
95
+ return puts("Invalid number: #{raw}") unless raw.match?(/\A\d+\z/)
96
+
97
+ count = raw.to_i
98
+ return puts("Please provide a positive number.") if count < 1
99
+
100
+ count
101
+ end
102
+
103
+ def copy_to_clipboard(entry)
104
+ command = @storage.extract_command(entry)
105
+ if Clipboard.copy(command)
106
+ puts "Copied to clipboard: #{command}"
107
+ else
108
+ puts command
109
+ puts "(clipboard not available — install pbcopy, xclip, or xsel)"
110
+ end
111
+ end
112
+
113
+ def print_entries(header, entries)
114
+ puts header
115
+ entries.each_with_index do |entry, idx|
116
+ puts " #{Colors.cyan("#{idx + 1}.")} #{format_entry(entry)}"
117
+ end
118
+ end
119
+
120
+ def format_entry(entry)
121
+ match = entry.match(Storage::ENTRY_PATTERN)
122
+ return entry unless match
123
+
124
+ "#{Colors.dim(match[1])} #{Colors.dim("|")} #{Colors.bold(match[2])}"
125
+ end
126
+
127
+ def print_help
128
+ puts HELP_TEXT
129
+ end
130
+ end
131
+ end
@@ -0,0 +1,43 @@
1
+ # frozen_string_literal: true
2
+
3
+ module Tailgrab
4
+ class Clipboard
5
+ class << self
6
+ def copy(text)
7
+ cmd = clipboard_command
8
+ return false unless cmd
9
+
10
+ IO.popen(cmd, "w") { |io| io.write(text) }
11
+ true
12
+ rescue Errno::ENOENT, IOError
13
+ false
14
+ end
15
+
16
+ def available?
17
+ !clipboard_command.nil?
18
+ end
19
+
20
+ private
21
+
22
+ def clipboard_command
23
+ @clipboard_command ||= detect_clipboard_command
24
+ end
25
+
26
+ def detect_clipboard_command
27
+ if command_exists?("pbcopy")
28
+ "pbcopy"
29
+ elsif command_exists?("wl-copy")
30
+ "wl-copy"
31
+ elsif command_exists?("xclip")
32
+ "xclip -selection clipboard"
33
+ elsif command_exists?("xsel")
34
+ "xsel --clipboard --input"
35
+ end
36
+ end
37
+
38
+ def command_exists?(cmd)
39
+ system("which #{cmd} > /dev/null 2>&1")
40
+ end
41
+ end
42
+ end
43
+ end
@@ -0,0 +1,36 @@
1
+ # frozen_string_literal: true
2
+
3
+ module Tailgrab
4
+ module Colors
5
+ CODES = {
6
+ reset: "\e[0m",
7
+ bold: "\e[1m",
8
+ dim: "\e[2m",
9
+ cyan: "\e[36m"
10
+ }.freeze
11
+
12
+ module_function
13
+
14
+ def enabled?
15
+ $stdout.tty?
16
+ end
17
+
18
+ def cyan(text)
19
+ wrap(text, :cyan)
20
+ end
21
+
22
+ def bold(text)
23
+ wrap(text, :bold)
24
+ end
25
+
26
+ def dim(text)
27
+ wrap(text, :dim)
28
+ end
29
+
30
+ def wrap(text, code)
31
+ return text unless enabled?
32
+
33
+ "#{CODES[code]}#{text}#{CODES[:reset]}"
34
+ end
35
+ end
36
+ end
@@ -0,0 +1,66 @@
1
+ # frozen_string_literal: true
2
+
3
+ module Tailgrab
4
+ class HistoryReader
5
+ SUPPORTED_SHELLS = %i[zsh bash].freeze
6
+
7
+ attr_reader :shell, :history_file
8
+
9
+ def initialize(shell: nil, history_file: nil)
10
+ @shell = shell || detect_shell
11
+ @history_file = history_file || find_history_file
12
+ end
13
+
14
+ def last(count)
15
+ return [] unless history_file && File.exist?(history_file)
16
+
17
+ lines = read_last_lines(count)
18
+ lines.map { |line| parse_line(line) }.compact
19
+ end
20
+
21
+ private
22
+
23
+ def detect_shell
24
+ shell_env = ENV["SHELL"] || ""
25
+ if shell_env.include?("zsh")
26
+ :zsh
27
+ elsif shell_env.include?("bash")
28
+ :bash
29
+ else
30
+ :unknown
31
+ end
32
+ end
33
+
34
+ def find_history_file
35
+ histfile = ENV.fetch("HISTFILE", nil)
36
+ return File.expand_path(histfile) if histfile && !histfile.empty?
37
+
38
+ case shell
39
+ when :zsh
40
+ File.expand_path("~/.zsh_history")
41
+ when :bash
42
+ File.expand_path("~/.bash_history")
43
+ end
44
+ end
45
+
46
+ def read_last_lines(count)
47
+ lines = File.readlines(history_file, chomp: true).reject(&:empty?)
48
+ lines.last(count)
49
+ end
50
+
51
+ def parse_line(line)
52
+ case shell
53
+ when :zsh
54
+ parse_zsh_line(line)
55
+ else
56
+ line.strip
57
+ end
58
+ end
59
+
60
+ def parse_zsh_line(line)
61
+ # Zsh extended history format: ": <timestamp>:0;<command>"
62
+ match = line.match(/^: \d+:\d+;(.+)$/)
63
+ match ? match[1].strip : line.strip
64
+ end
65
+ end
66
+ end
@@ -0,0 +1,77 @@
1
+ # frozen_string_literal: true
2
+
3
+ require "time"
4
+
5
+ module Tailgrab
6
+ class Storage
7
+ DEFAULT_FILE = File.join(Dir.home, "tailgrab.txt").freeze
8
+ TIMESTAMP_FORMAT = "%Y-%m-%d %H:%M:%S"
9
+ ENTRY_PATTERN = /^(\d{4}-\d{2}-\d{2} \d{2}:\d{2}:\d{2}) \| (.+)$/
10
+
11
+ attr_reader :file_path
12
+
13
+ def initialize(file_path: DEFAULT_FILE)
14
+ @file_path = file_path
15
+ end
16
+
17
+ def save(commands)
18
+ timestamp = Time.now.strftime(TIMESTAMP_FORMAT)
19
+
20
+ File.open(file_path, "a") do |f|
21
+ commands.each do |cmd|
22
+ f.puts "#{timestamp} | #{cmd}"
23
+ end
24
+ end
25
+ end
26
+
27
+ def read_last(count)
28
+ return [] unless content?
29
+
30
+ entries = all_entries
31
+ entries.last(count)
32
+ end
33
+
34
+ def read_since(since_time)
35
+ return [] unless content?
36
+
37
+ all_entries.select do |entry|
38
+ ts = parse_timestamp(entry)
39
+ ts && ts >= since_time
40
+ end
41
+ end
42
+
43
+ def wipe
44
+ File.write(file_path, "")
45
+ end
46
+
47
+ def content?
48
+ File.exist?(file_path) && !File.empty?(file_path)
49
+ end
50
+
51
+ def extract_command(entry)
52
+ match = entry.match(ENTRY_PATTERN)
53
+ match ? match[2] : entry
54
+ end
55
+
56
+ def read_all
57
+ return [] unless content?
58
+
59
+ all_entries
60
+ end
61
+
62
+ private
63
+
64
+ def all_entries
65
+ File.readlines(file_path, chomp: true).reject(&:empty?)
66
+ end
67
+
68
+ def parse_timestamp(entry)
69
+ match = entry.match(ENTRY_PATTERN)
70
+ return nil unless match
71
+
72
+ Time.parse(match[1])
73
+ rescue ArgumentError
74
+ nil
75
+ end
76
+ end
77
+ end
@@ -0,0 +1,5 @@
1
+ # frozen_string_literal: true
2
+
3
+ module Tailgrab
4
+ VERSION = "1.0"
5
+ end
data/lib/tailgrab.rb ADDED
@@ -0,0 +1,11 @@
1
+ # frozen_string_literal: true
2
+
3
+ require_relative "tailgrab/version"
4
+ require_relative "tailgrab/history_reader"
5
+ require_relative "tailgrab/clipboard"
6
+ require_relative "tailgrab/colors"
7
+ require_relative "tailgrab/storage"
8
+ require_relative "tailgrab/cli"
9
+
10
+ module Tailgrab
11
+ end
metadata ADDED
@@ -0,0 +1,63 @@
1
+ --- !ruby/object:Gem::Specification
2
+ name: tailgrab
3
+ version: !ruby/object:Gem::Version
4
+ version: '1.0'
5
+ platform: ruby
6
+ authors:
7
+ - Himanshu Panwar
8
+ autorequire:
9
+ bindir: bin
10
+ cert_chain: []
11
+ date: 2026-02-28 00:00:00.000000000 Z
12
+ dependencies: []
13
+ description: Save and recall your terminal commands with timestamps
14
+ email:
15
+ - hpanwar@g2.com
16
+ executables:
17
+ - grab
18
+ extensions: []
19
+ extra_rdoc_files: []
20
+ files:
21
+ - ".rspec"
22
+ - ".rubocop.yml"
23
+ - CHANGELOG.md
24
+ - LICENSE.txt
25
+ - README.md
26
+ - Rakefile
27
+ - bin/grab
28
+ - lib/tailgrab.rb
29
+ - lib/tailgrab/cli.rb
30
+ - lib/tailgrab/clipboard.rb
31
+ - lib/tailgrab/colors.rb
32
+ - lib/tailgrab/history_reader.rb
33
+ - lib/tailgrab/storage.rb
34
+ - lib/tailgrab/version.rb
35
+ homepage: https://github.com/hpanwar09/tailgrab
36
+ licenses:
37
+ - MIT
38
+ metadata:
39
+ allowed_push_host: https://rubygems.org
40
+ homepage_uri: https://github.com/hpanwar09/tailgrab
41
+ source_code_uri: https://github.com/hpanwar09/tailgrab
42
+ changelog_uri: https://github.com/hpanwar09/tailgrab/blob/master/CHANGELOG.md
43
+ rubygems_mfa_required: 'true'
44
+ post_install_message:
45
+ rdoc_options: []
46
+ require_paths:
47
+ - lib
48
+ required_ruby_version: !ruby/object:Gem::Requirement
49
+ requirements:
50
+ - - ">="
51
+ - !ruby/object:Gem::Version
52
+ version: 3.0.0
53
+ required_rubygems_version: !ruby/object:Gem::Requirement
54
+ requirements:
55
+ - - ">="
56
+ - !ruby/object:Gem::Version
57
+ version: '0'
58
+ requirements: []
59
+ rubygems_version: 3.4.19
60
+ signing_key:
61
+ specification_version: 4
62
+ summary: Save and recall your terminal commands
63
+ test_files: []