Class: Diakonos::Session

Inherits:
Object show all
Defined in:
lib/diakonos/sessions.rb

Instance Attribute Summary collapse

Class Method Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(filepath, data = nil) ⇒ Session

Returns a new instance of Session



6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
# File 'lib/diakonos/sessions.rb', line 6

def initialize(filepath, data = nil)
  if data.nil?
    @filename = File.expand_path(filepath)
    @settings = Hash.new

    basename = File.basename(filepath)
    if ! Session.pid_session?(filepath)
      @name = basename
    end

    @buffers = []
    @buffer_current = 1
    self.dir = Dir.getwd
  else
    @filename = data['filename']
    @settings = data['settings']
    @name = data['name']
    @buffers = data['buffers']
    @buffer_current = data['buffer_current'] || 1
    self.dir = data['dir']
  end
end

Instance Attribute Details

#buffer_currentObject

Returns the value of attribute buffer_current



4
5
6
# File 'lib/diakonos/sessions.rb', line 4

def buffer_current
  @buffer_current
end

#buffersObject (readonly)

Returns the value of attribute buffers



3
4
5
# File 'lib/diakonos/sessions.rb', line 3

def buffers
  @buffers
end

#dirObject

Returns the value of attribute dir



3
4
5
# File 'lib/diakonos/sessions.rb', line 3

def dir
  @dir
end

#filenameObject (readonly)

Returns the value of attribute filename



3
4
5
# File 'lib/diakonos/sessions.rb', line 3

def filename
  @filename
end

#nameObject (readonly)

Returns the value of attribute name



3
4
5
# File 'lib/diakonos/sessions.rb', line 3

def name
  @name
end

#readline_historiesObject (readonly)

Returns the value of attribute readline_histories



3
4
5
# File 'lib/diakonos/sessions.rb', line 3

def readline_histories
  @readline_histories
end

#settingsObject (readonly)

Returns the value of attribute settings



3
4
5
# File 'lib/diakonos/sessions.rb', line 3

def settings
  @settings
end

Class Method Details

.file_hash_for(filepath) ⇒ Object



117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
# File 'lib/diakonos/sessions.rb', line 117

def self.file_hash_for(filepath)
  filepath, line_number = ::Diakonos.parse_filename_and_line_number(filepath)
  {
    'filepath'  => filepath,
    'read_only' => false,
    'cursor'    => {
      'row' => line_number || 0,
      'col' => 0,
    },
    'display'   => {
      'top_line'    => 0,
      'left_column' => 0
    },
  }
end

.from_yaml_file(yaml_filename) ⇒ Session

Returns The Session created from the YAML data in the specified file or nil on failure to load

Returns:

  • (Session)

    The Session created from the YAML data in the specified file or nil on failure to load



79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
# File 'lib/diakonos/sessions.rb', line 79

def self.from_yaml_file(yaml_filename)
  return nil  if ! File.exist?(yaml_filename)
  session = nil

  File.open(yaml_filename) do |f|
    loaded = YAML::load(f) or break

    if(
      loaded[ 'filename' ] &&
      loaded[ 'settings' ] &&
      loaded[ 'settings' ].respond_to?( :values ) &&
      loaded.has_key?( 'name' ) &&
      (
        loaded[ 'files' ] &&
        loaded[ 'files' ].respond_to?( :each ) ||
        loaded[ 'buffers' ] &&
        loaded[ 'buffers' ].respond_to?( :each )
      )
    )
      # Convert old sessions
      if loaded[ 'files' ]
        loaded[ 'buffers' ] = loaded[ 'files' ].map { |f|
          Session.file_hash_for f
        }
        loaded.delete 'files'
      end

      session = Session.new(loaded['filename'], loaded)
    end
  end

  session
end

.pid_session?(path) ⇒ Boolean

Returns:

  • (Boolean)


113
114
115
# File 'lib/diakonos/sessions.rb', line 113

def self.pid_session?(path)
  %r{/\d+$} === path
end

Instance Method Details

#set_buffers(buffers) ⇒ Object



41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
# File 'lib/diakonos/sessions.rb', line 41

def set_buffers(buffers)
  @buffers = buffers.reject { |buffer|
    buffer.name.nil?
  }.collect { |buffer|
    {
      'filepath' => buffer.name,
      'read_only' => buffer.read_only,
      'cursor'   => {
        'row' => buffer.last_row,
        'col' => buffer.last_col,
      },
      'display'  => {
        'top_line'    => buffer.top_line,
        'left_column' => buffer.left_column
      },
      'last_search_regexps' => buffer.last_search_regexps.map { |r| r.to_s },
    }
  }.compact
end

#set_readline_histories(rlh_general, rlh_files, rlh_search, rlh_shell, rlh_help, rlh_sessions) ⇒ Object



61
62
63
64
65
66
67
68
69
70
# File 'lib/diakonos/sessions.rb', line 61

def set_readline_histories(rlh_general, rlh_files, rlh_search, rlh_shell, rlh_help, rlh_sessions)
  @readline_histories = {
    'general'  => rlh_general,
    'files'    => rlh_files,
    'search'   => rlh_search,
    'shell'    => rlh_shell,
    'help'     => rlh_help,
    'sessions' => rlh_sessions,
  }
end

#to_yamlObject



29
30
31
32
33
34
35
36
37
38
39
# File 'lib/diakonos/sessions.rb', line 29

def to_yaml
  {
    'filename' => @filename,
    'settings' => @settings,
    'name' => @name,
    'buffer_current' => @buffer_current,
    'dir' => @dir,
    'buffers' => @buffers,
    'readline_histories' => @readline_histories
  }.to_yaml
end