Class: Tmux::Buffer

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

Instance Attribute Summary (collapse)

Instance Method Summary (collapse)

Constructor Details

- (Filesize) initialize(number, session)



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

- (Number) number (readonly)

Returns:

  • (Number)


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

def number
  @number
end

- (Server) server (readonly)

Returns:



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

def server
  @session.server
end

- (Session) session (readonly)

Returns:



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

def session
  @session
end

- (Filesize) size (readonly)

Parameters:

  • force_reload (Boolean)

    Ignore frozen state if true

Returns:

  • (Filesize)


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

- (String) data(force_reload = false)

Receive the buffer's data.

Parameters:

  • force_reload (Boolean) (defaults to: false)

    Ignore frozen state if true

Returns:

  • (String)


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

- (String) data=(new_data)

Set the buffer's data.

Parameters:

  • new_data (String)

Returns:

  • (String)


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.

Parameters:

  • target (Window) (defaults to: nil)

    The pane or window to paste the buffer into. Note: Panes as as target are only supported since tmux version 1.3.

  • pop (Boolean) (defaults to: false)

    If true, delete the buffer from the stack

  • translate (Boolean) (defaults to: true)

    If true, any linefeed (LF) characters in the paste buffer are replaced with carriage returns (CR)

  • separator (String) (defaults to: nil)

    Replace any linefeed (LF) in the buffer with this separator. +translate+ must be false.

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.

Parameters:

  • file (String)

    The file to write to

  • append (Boolean) (defaults to: false)

    Append to instead of overwriting the file

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

- (Undefined) target_argument (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

- (String) to_s

Returns The content of a buffer

Returns:

  • (String)

    The content of a buffer



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

def to_s
  text
end