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 session tabs per workspace.

Instance Method Summary collapse

Constructor Details

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

Returns a new instance of TabStore.



10
11
12
13
# File 'lib/kward/tab_store.rb', line 10

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

Instance Method Details

#loadObject



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

def load
  return { "session_paths" => [], "active_index" => 0 } unless File.file?(path)

  data = JSON.parse(File.read(path))
  paths = Array(data["session_paths"]).map(&:to_s).reject(&:empty?)
  labels = Array(data["labels"]).map(&:to_s)
  active_index = data["active_index"].to_i
  { "session_paths" => paths, "labels" => labels, "active_index" => active_index }
rescue JSON::ParserError
  { "session_paths" => [], "active_index" => 0 }
end

#pathObject



37
38
39
# File 'lib/kward/tab_store.rb', line 37

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

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



27
28
29
30
31
32
33
34
35
# File 'lib/kward/tab_store.rb', line 27

def save(session_paths:, active_index:, labels: [])
  paths = Array(session_paths).map(&:to_s).reject(&:empty?)
  PrivateFile.write_json(path, {
    "cwd" => @cwd,
    "session_paths" => paths,
    "labels" => Array(labels).map(&:to_s),
    "active_index" => [[active_index.to_i, 0].max, [paths.length - 1, 0].max].min
  })
end