Class: Kward::PromptHistory
- Inherits:
-
Object
- Object
- Kward::PromptHistory
- Defined in:
- lib/kward/prompt_history.rb
Overview
Workspace-scoped JSONL persistence for terminal prompt history.
Defined Under Namespace
Classes: Entry
Constant Summary collapse
- DEFAULT_LIMIT =
1_000
Instance Attribute Summary collapse
-
#cwd ⇒ Object
readonly
Returns the value of attribute cwd.
-
#kind ⇒ Object
readonly
Returns the value of attribute kind.
-
#limit ⇒ Object
readonly
Returns the value of attribute limit.
Instance Method Summary collapse
- #append(value) ⇒ Object
-
#initialize(config_dir: ConfigFiles.config_dir, cwd: Dir.pwd, limit: DEFAULT_LIMIT, kind: "prompt") ⇒ PromptHistory
constructor
A new instance of PromptHistory.
- #path ⇒ Object
- #values ⇒ Object
Constructor Details
#initialize(config_dir: ConfigFiles.config_dir, cwd: Dir.pwd, limit: DEFAULT_LIMIT, kind: "prompt") ⇒ PromptHistory
Returns a new instance of PromptHistory.
14 15 16 17 18 19 |
# File 'lib/kward/prompt_history.rb', line 14 def initialize(config_dir: ConfigFiles.config_dir, cwd: Dir.pwd, limit: DEFAULT_LIMIT, kind: "prompt") @config_dir = config_dir @cwd = ConfigFiles.canonical_workspace_root(cwd) @limit = limit.to_i.positive? ? limit.to_i : DEFAULT_LIMIT @kind = kind.to_s.empty? ? "prompt" : kind.to_s end |
Instance Attribute Details
#cwd ⇒ Object (readonly)
Returns the value of attribute cwd.
21 22 23 |
# File 'lib/kward/prompt_history.rb', line 21 def cwd @cwd end |
#kind ⇒ Object (readonly)
Returns the value of attribute kind.
21 22 23 |
# File 'lib/kward/prompt_history.rb', line 21 def kind @kind end |
#limit ⇒ Object (readonly)
Returns the value of attribute limit.
21 22 23 |
# File 'lib/kward/prompt_history.rb', line 21 def limit @limit end |
Instance Method Details
#append(value) ⇒ Object
27 28 29 30 31 32 33 34 35 36 |
# File 'lib/kward/prompt_history.rb', line 27 def append(value) text = value.to_s return false if text.strip.empty? existing = entries return false if existing.last&.value == text write_entries((existing + [Entry.new(value: text, timestamp: Time.now.utc.iso8601(3))]).last(limit)) true end |
#path ⇒ Object
38 39 40 |
# File 'lib/kward/prompt_history.rb', line 38 def path ConfigFiles.prompt_history_path(@cwd, config_dir: @config_dir, kind: @kind) end |
#values ⇒ Object
23 24 25 |
# File 'lib/kward/prompt_history.rb', line 23 def values entries.map(&:value) end |