Class: Muxr::Session

Inherits:
Object
  • Object
show all
Defined in:
lib/muxr/session.rb

Overview

A Session bundles the user’s Window + Drawer plus a snapshot of the terminal dimensions. It is responsible for persisting/restoring its own state as JSON on disk (~/.muxr/sessions/<name>.json). Only the shape of the session (pane count, layout, cwds, drawer state) is persisted — the live shell history is not.

Constant Summary collapse

SESSIONS_DIR =
File.join(Dir.home, ".muxr", "sessions").freeze

Instance Attribute Summary collapse

Class Method Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(name: "default", width: 80, height: 24) ⇒ Session

Returns a new instance of Session.



16
17
18
19
20
21
22
23
# File 'lib/muxr/session.rb', line 16

def initialize(name: "default", width: 80, height: 24)
  @name = name
  @width = width
  @height = height
  @window = Window.new(name: name)
  @drawer = nil
  @focus_drawer = false
end

Instance Attribute Details

#drawerObject

Returns the value of attribute drawer.



13
14
15
# File 'lib/muxr/session.rb', line 13

def drawer
  @drawer
end

#focus_drawerObject

Returns the value of attribute focus_drawer.



13
14
15
# File 'lib/muxr/session.rb', line 13

def focus_drawer
  @focus_drawer
end

#heightObject

Returns the value of attribute height.



13
14
15
# File 'lib/muxr/session.rb', line 13

def height
  @height
end

#nameObject (readonly)

Returns the value of attribute name.



14
15
16
# File 'lib/muxr/session.rb', line 14

def name
  @name
end

#widthObject

Returns the value of attribute width.



13
14
15
# File 'lib/muxr/session.rb', line 13

def width
  @width
end

#windowObject

Returns the value of attribute window.



13
14
15
# File 'lib/muxr/session.rb', line 13

def window
  @window
end

Class Method Details

.exists?(name) ⇒ Boolean

Returns:

  • (Boolean)


61
62
63
# File 'lib/muxr/session.rb', line 61

def self.exists?(name)
  File.exist?(save_path_for(name))
end

.listObject



65
66
67
68
69
70
71
# File 'lib/muxr/session.rb', line 65

def self.list
  return [] unless File.directory?(SESSIONS_DIR)
  Dir.children(SESSIONS_DIR).filter_map do |entry|
    next unless entry.end_with?(".json")
    File.basename(entry, ".json")
  end.sort
end

.load(name) ⇒ Object



53
54
55
56
57
58
59
# File 'lib/muxr/session.rb', line 53

def self.load(name)
  path = save_path_for(name)
  return nil unless File.exist?(path)
  JSON.parse(File.read(path))
rescue JSON::ParserError
  nil
end

.save_path_for(name) ⇒ Object



29
30
31
# File 'lib/muxr/session.rb', line 29

def self.save_path_for(name)
  File.join(SESSIONS_DIR, "#{name}.json")
end

Instance Method Details

#saveObject



33
34
35
36
37
# File 'lib/muxr/session.rb', line 33

def save
  FileUtils.mkdir_p(SESSIONS_DIR)
  File.write(save_path, JSON.pretty_generate(serialize))
  save_path
end

#save_pathObject



25
26
27
# File 'lib/muxr/session.rb', line 25

def save_path
  File.join(SESSIONS_DIR, "#{@name}.json")
end

#serializeObject



39
40
41
42
43
44
45
46
47
48
49
50
51
# File 'lib/muxr/session.rb', line 39

def serialize
  {
    "name"           => @name,
    "width"          => @width,
    "height"         => @height,
    "layout"         => @window.layout.to_s,
    "focused_index"  => @window.focused_index,
    "master_index"   => @window.master_index,
    "focus_drawer"   => @focus_drawer,
    "panes"          => @window.panes.map { |p| { "cwd" => safe_cwd(p) } },
    "drawer"         => serialize_drawer
  }
end