Module: Snibbets::OS

Defined in:
lib/snibbets/os.rb

Class Method Summary collapse

Class Method Details

.copy(text) ⇒ Object

Platform-agnostic copy command

Parameters:

  • text

    The text to copy



11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
# File 'lib/snibbets/os.rb', line 11

def copy(text)
  os = RbConfig::CONFIG['target_os']
  case os
  when /darwin.*/i
    `echo #{Shellwords.escape(text.uncolor)} | pbcopy`
  else
    if TTY::Which.exist?('xclip')
      `echo #{Shellwords.escape(text.uncolor)} | xclip -sel c`
    elsif TTY::Which.exist('xsel')
      `echo #{Shellwords.escape(text.uncolor)} | xsel -ib`
    else
      puts 'Copy not supported on this system, please install xclip or xsel.'
    end
  end
end

.darwin_open(file, app: nil) ⇒ Object

macOS open command

Parameters:

  • file

    The file

  • app (defaults to: nil)

    The application



69
70
71
72
73
74
75
# File 'lib/snibbets/os.rb', line 69

def darwin_open(file, app: nil)
  if app
    `open -a "#{app}" #{Shellwords.escape(file)}`
  else
    `open #{Shellwords.escape(file)}`
  end
end

.linux_open(file) ⇒ Object

Linux open command

Parameters:

  • file

    The file



91
92
93
94
95
96
97
# File 'lib/snibbets/os.rb', line 91

def linux_open(file)
  if TTY::Which.exist?('xdg-open')
    `xdg-open #{Shellwords.escape(file)}`
  else
    notify('{r}Unable to determine executable for `xdg-open`.')
  end
end

.open(file, app: nil) ⇒ Object

Platform-agnostic open command

Parameters:

  • file (String)

    The file to open



51
52
53
54
55
56
57
58
59
60
61
# File 'lib/snibbets/os.rb', line 51

def open(file, app: nil)
  os = RbConfig::CONFIG['target_os']
  case os
  when /darwin.*/i
    darwin_open(file, app: app)
  when /mingw|mswin/i
    win_open(file)
  else
    linux_open(file)
  end
end

.pasteObject

Platform-agnostic paste command



30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
# File 'lib/snibbets/os.rb', line 30

def paste
  os = RbConfig::CONFIG['target_os']
  case os
  when /darwin.*/i
    `pbpaste -pboard general -Prefer txt`.strip_newlines
  else
    if TTY::Which.exist?('xclip')
      `xclip -o -sel c`.strip_newlines
    elsif TTY::Which.exist('xsel')
      `xsel -ob`.strip_newlines
    else
      puts 'Paste not supported on this system, please install xclip or xsel.'
    end
  end
end

.win_open(file) ⇒ Object

Windows open command

Parameters:

  • file

    The file



82
83
84
# File 'lib/snibbets/os.rb', line 82

def win_open(file)
  `start #{Shellwords.escape(file)}`
end