Class: SafeDb::Clipboard

Inherits:
Object
  • Object
show all
Defined in:
lib/utils/clipboard/clip.rb

Overview

This Clip class reads, writes and overwrites text that either has been placed in, or will be placed in the clipboard.

xclip pre-condition

xclip must be installed using ‘sudo apt install –assume-yes xclip`

Class Method Summary collapse

Class Method Details

.put(text_line) ⇒ Object

Put the parameter text into the clipboard thus overwriting whatever content may or may not exist there.

Parameters:

  • text_line (String)

    the text line to put in the clipboard



45
46
47
48
49
50
51
# File 'lib/utils/clipboard/clip.rb', line 45

def self.put( text_line )

  log.info(x) { "Putting text into the clipboard thus overwriting existing content." }
  clipboard_put_command = "printf #{text_line} | xclip -selection c"
  system clipboard_put_command

end

.read_lineString

Get the first line of text from the clipboard. Raise an exception if the clipboard either has no text, or the text is empy, or the text consists solely of whitespace.

The text will be trimmed before being returned so any leading or trailing whitespace will be removed.

For **multiple line** text, the first line that is non-empty and non whitespace only is returned.

Returns:

  • (String)

    trimmed version of the clipboard’s contents

Raises:

  • (RuntimeError)

    if text is nil, empty or consists solely of whitespace



66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
# File 'lib/utils/clipboard/clip.rb', line 66

def self.read_line()
  
  log.info(x) { "About to read and process a text line from the clipboard." }
  xclip_command = "xclip -o"
  textual_content = %x[ #{xclip_command} ]
  no_content = textual_content.nil?() || textual_content.chomp().strip().empty?()
  raise RuntimeError, "The clipboard does not contain any text." if no_content
  clipboard_text = textual_content.chomp().strip()
  num_lines = clipboard_text.lines.count()
  return clipboard_text if num_lines == 1

  log.info(x) { "Clipboard text has #{num_lines} lines - will return first viable line." }

  clipboard_text.each_line do |text_line|
    candidate_line = text_line.chomp.gsub("\\n","").strip()
    return candidate_line unless candidate_line.empty()
  end

  raise RuntimeError, "The multi-line clipboard text contained no printable characters."

end

.read_passwordString

Get the first line of text from the clipboard. Raise an exception if the clipboard either has no text, or the text is empy, or the text consists solely of whitespace.

The text will be trimmed before being returned so any leading or trailing whitespace will be removed.

For **multiple line** text, the first line that is non-empty and non whitespace only is returned.

Due to the sensitive nature of the text the clipboards contents will be immediately overwritten once the pertinent textual content has been consumed.

Returns:

  • (String)

    trimmed version of the clipboard’s contents

Raises:

  • (RuntimeError)

    if text is nil, empty or consists solely of whitespace



31
32
33
34
35
36
37
38
# File 'lib/utils/clipboard/clip.rb', line 31

def self.read_password()
  
  log.info(x) { "About to read a sensitive password from the clipboard." }
  password_text = read_line()
  put( "safe has overwritten clipboard contents." )
  return password_text

end