Skip to content

Commit

Permalink
Merge pull request #14 from mandx/feature/clipboard-commands-handle-m…
Browse files Browse the repository at this point in the history
…ultiple-files

Make the clipboard commands work with multiple files/paths
  • Loading branch information
braver authored Aug 13, 2017
2 parents 059772e + 8505649 commit f87e665
Showing 1 changed file with 32 additions and 15 deletions.
47 changes: 32 additions & 15 deletions SideBar.py
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,10 @@ class SideBarCommand(sublime_plugin.WindowCommand):

def copy_to_clipboard_and_inform(self, data):
sublime.set_clipboard(data)
self.window.status_message('Copied "{}" to clipboard'.format(data))
lines = len(data.split('\n'))
self.window.status_message('Copied {} to clipboard'.format(
'{} lines'.format(lines) if lines > 1 else '"{}"'.format(data)
))

def get_path(self, paths):
try:
Expand All @@ -34,42 +37,56 @@ def make_dirs_for(filename):
return False


class SideBarCopyNameCommand(SideBarCommand):
class MultipleFilesMixin(object):

def get_paths(self, paths):
return paths or [self.get_path(paths)]

def is_visible(self, paths):
return bool(paths or self.window.active_view().file_name())


class SideBarCopyNameCommand(MultipleFilesMixin, SideBarCommand):

def run(self, paths):
path = self.get_path(paths)
name = os.path.split(path)[1]
self.copy_to_clipboard_and_inform(name)
names = (os.path.split(path)[1] for path in self.get_paths(paths))
self.copy_to_clipboard_and_inform('\n'.join(names))

def description(self):
return 'Copy Filename'


class SideBarCopyAbsolutePathCommand(SideBarCommand):
class SideBarCopyAbsolutePathCommand(MultipleFilesMixin, SideBarCommand):

def run(self, paths):
path = self.get_path(paths)
self.copy_to_clipboard_and_inform(path)
paths = self.get_paths(paths)
self.copy_to_clipboard_and_inform('\n'.join(paths))

def description(self):
return 'Copy Absolute Path'


class SideBarCopyRelativePathCommand(SideBarCommand):
class SideBarCopyRelativePathCommand(MultipleFilesMixin, SideBarCommand):

def run(self, paths):
path = self.get_path(paths)
project_file_name = self.window.project_file_name()
root_dir = ''
if project_file_name:
root_dir = os.path.dirname(project_file_name)
else:
root_dir = self.window.project_data()['folders'][0]['path']
common = os.path.commonprefix([root_dir, path])
path = path[len(common):]
if path.startswith('/') or path.startswith('\\'):
path = path[1:]
self.copy_to_clipboard_and_inform(path)

paths = self.get_paths(paths)
relative_paths = []

for path in paths:
common = os.path.commonprefix([root_dir, path])
path = path[len(common):]
if path.startswith('/') or path.startswith('\\'):
path = path[1:]
relative_paths.append(path)

self.copy_to_clipboard_and_inform('\n'.join(relative_paths))

def description(self):
return 'Copy Relative Path'
Expand Down

0 comments on commit f87e665

Please sign in to comment.