Skip to content
Kward Search API index

Class: Kward::TabStore

Inherits:
Object
  • Object
show all
Defined in:
lib/kward/tab_store.rb

Overview

Persists the terminal UI's open tabs per workspace.

Constant Summary collapse

VERSION =
2

Instance Method Summary collapse

Constructor Details

#initialize(config_dir: ConfigFiles.config_dir, cwd: Dir.pwd) ⇒ TabStore

Returns a new instance of TabStore.



12
13
14
15
# File 'lib/kward/tab_store.rb', line 12

def initialize(config_dir: ConfigFiles.config_dir, cwd: Dir.pwd)
  @config_dir = config_dir
  @cwd = File.expand_path(cwd)
end

Instance Method Details

#loadObject



17
18
19
20
21
22
23
24
25
26
# File 'lib/kward/tab_store.rb', line 17

def load
  return empty_state unless File.file?(path)

  data = JSON.parse(File.read(path))
  return typed_state(data) if data["tabs"].is_a?(Array)

  legacy_state(data)
rescue JSON::ParserError
  empty_state
end

#pathObject



50
51
52
# File 'lib/kward/tab_store.rb', line 50

def path
  File.join(@config_dir, "tabs", "#{workspace_key}.json")
end

#save(session_paths: nil, tabs: nil, active_index:, labels: []) ⇒ Object

session_paths remains supported for callers and layouts written before typed tab descriptors were introduced.



30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
# File 'lib/kward/tab_store.rb', line 30

def save(session_paths: nil, tabs: nil, active_index:, labels: [])
  tabs ||= Array(session_paths).filter_map.with_index do |session_path, index|
    path = session_path.to_s
    next if path.empty?

    {
      "kind" => "session",
      "session_path" => path,
      "label" => Array(labels)[index].to_s
    }
  end
  tabs = normalize_tabs(tabs)
  PrivateFile.write_json(path, {
    "version" => VERSION,
    "cwd" => @cwd,
    "tabs" => tabs,
    "active_index" => [[active_index.to_i, 0].max, [tabs.length - 1, 0].max].min
  })
end