def execute(self): if 'xclip' not in get_executables(): self.fm.notify('xclip is not found.', bad=True) return arg = self.rest(1) if arg: if not os.path.isfile(arg): self.fm.notify('{} is not a file.'.format(arg)) return file = File(arg) else: file = self.fm.thisfile if not file.is_file: self.fm.notify('{} is not a file.'.format(file.relative_path)) return relative_path = file.relative_path cmd = ['xclip', '-selection', 'clipboard'] if not file.is_binary(): with open(file.path, 'rb') as fd: subprocess.check_call(cmd, stdin=fd) elif file.image: cmd += ['-t', file.mimetype, file.path] subprocess.check_call(cmd) self.fm.notify( 'Content of {} is copied to x clipboard'.format(relative_path)) else: self.fm.notify('{} is not an image file or a text file.'.format( relative_path))
def execute(self): import subprocess from ranger.container.file import File from ranger.ext.get_executables import get_executables clipboard_managers = { 'xclip': [ ['xclip', '-selection', 'primary'], ['xclip', '-selection', 'clipboard'], ], 'xsel': [ ['xsel', '--primary'], ['xsel', '--clipboard'], ], 'wl-copy': [ ['wl-copy'], ], 'pbcopy': [ ['pbcopy'], ], } ordered_managers = ['pbcopy', 'wl-copy', 'xclip', 'xsel'] executables = get_executables() for manager in ordered_managers: if manager in executables: clipboard_commands = clipboard_managers[manager] break else: self.fm.notify('Could not find a clipboard manager in the PATH.', bad=True) return arg = self.rest(1) if arg: if not os.path.isfile(arg): self.fm.notify("'{}' is not a file.".format(arg), bad=True) return file = File(arg) else: file = self.fm.thisfile if not file.is_file: self.fm.notify("'{}' is not a file.".format( file.relative_path), bad=True) return if not file.is_binary(): for command in clipboard_commands: with open(file.path, mode='r') as fd: self.fm.execute_command(command, universal_newlines=True, stdin=fd) self.fm.notify( "The content of '{}' is copied to the clipboard.".format( file.relative_path)) else: self.fm.notify("'{}' is not a text file.".format( file.relative_path))