Class: Tmux::Buffer

Inherits:
Object
  • Object
show all
Defined in:
lib/tmux/buffer.rb

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(number, session) ⇒ Filesize



11
12
13
14
15
16
17
18
# File 'lib/tmux/buffer.rb', line 11

def initialize(number, session)
  @number, @session, @size = number, session
  unless server.version >= "1.3"
    # we do not need a temporary file for tmux versions that can
    # directly load/save from/to stdin/stdout
    @file = Tempfile.new("buffer")
  end
end

Instance Attribute Details

#numberNumber (readonly)



7
8
9
# File 'lib/tmux/buffer.rb', line 7

def number
  @number
end

#serverServer (readonly)



86
87
88
# File 'lib/tmux/buffer.rb', line 86

def server
  @session.server
end

#sessionSession (readonly)



9
10
11
# File 'lib/tmux/buffer.rb', line 9

def session
  @session
end

#sizeFilesize (readonly)



24
25
26
27
28
29
30
# File 'lib/tmux/buffer.rb', line 24

def size(force_reload = false)
  if @size && !force_reload
    @size
  else
    Filesize.new(@session.buffers_information[number][:size].to_i)
  end
end

Instance Method Details

#data(force_reload = false) ⇒ String

Receive the buffer's data.



36
37
38
39
40
41
42
43
44
45
46
47
48
# File 'lib/tmux/buffer.rb', line 36

def data(force_reload = false)
  # note: we cannot use show-buffer because that would escape tabstops
  if @data && !force_reload
    @data
  else
    if server.version >= "1.3"
      return server.invoke_command "save-buffer -b #@number #{target_argument} -"
    else
      server.invoke_command "save-buffer -b #@number #{target_argument} #{@file.path}"
      return @file.read
    end
  end
end

#data=(new_data) ⇒ String

Set the buffer's data.



54
55
56
57
58
59
# File 'lib/tmux/buffer.rb', line 54

def data=(new_data)
  # FIXME maybe some more escaping?
  server.invoke_command "set-buffer -b #@number #{target_argument} \"#{new_data}\""
  @data = data(true) if @frozen
  @size = size(true)
end

#delete

This method returns an undefined value.

Deletes a buffer.

tmux command:

  • delete-buffer



94
95
96
97
# File 'lib/tmux/buffer.rb', line 94

def delete
  freeze! # so we can still access its old value
  server.invoke_command "delete-buffer -b #@number #{target_argument}"
end

#freeze!

This method returns an undefined value.

By default, Buffer will not cache its data but instead query it each time. By calling this method, the data will be cached and not updated anymore.



77
78
79
80
81
# File 'lib/tmux/buffer.rb', line 77

def freeze!
  @frozen = true
  @data = data
  @size = size
end

#paste(target = nil, pop = false, translate = true, separator = nil)

This method returns an undefined value.

Pastes the content of a buffer into a window or pane.

See Also:

tmux command:

  • paste-buffer

Required tmux version:

  • >=1.3 for pasting to panes



120
121
122
123
124
125
126
127
128
129
130
131
132
# File 'lib/tmux/buffer.rb', line 120

def paste(target = nil, pop = false, translate = true, separator = nil)
  if server.version < "1.3"
    if separator || target.is_a?(Pane)
      raise Exception::UnsupportedVersion, "1.3"
    end
  end

  flag_pop       = pop ? "-d" : ""
  flag_translate = translate ? "" : "-r"
  flag_separator = separator ? "" : "-s \"#{separator}\"" # FIXME escape
  window_param   = target ? "-t #{target.identifier}" : ""
  server.invoke_command "paste-buffer #{flag_pop} #{flag_translate} #{flag_separator} #{window_param}"
end

#save(file, append = false) Also known as: write

This method returns an undefined value.

Saves the contents of a buffer.

tmux command:

  • save-buffer



67
68
69
70
# File 'lib/tmux/buffer.rb', line 67

def save(file, append = false)
  flag = append ? "-a" : ""
  server.invoke_command "save-buffer #{flag} -b #@number #{target_argument} #{file}"
end

#target_argumentUndefined (private)



135
136
137
138
139
140
141
# File 'lib/tmux/buffer.rb', line 135

def target_argument
  if server.version < "1.5"
    "-t #{@session.identifier}"
  else
    ""
  end
end

#to_sString



100
101
102
# File 'lib/tmux/buffer.rb', line 100

def to_s
  text
end