Home Download Buy Blog Forum Support

Sort Open Files

Re: Sort Open Files

Postby agibsonsw on Wed Mar 07, 2012 4:54 pm

tito wrote:You can sort the tabs every time a new tab is opened or finish loading. That way the list will stay sorted always.

How do we sort tabs? Presumably there's a Command for that :D. Although, this won't stop me finishing my work :D

This last/latest(?) version will display the files in either alphabetical or modified date order. If using modified date order, the date-time also appears in the quick panel.

Code: Select all
import sublime, sublime_plugin, os, datetime
from operator import itemgetter

file_views = []

class OrderedFilesCommand(sublime_plugin.TextCommand):
   def run(self, edit, index):
      global file_views
      file_views = []
      for vw in self.view.window().views():
         head, tail = os.path.split(vw.file_name())
         modified = os.path.getmtime(vw.file_name())
         file_views.append((tail, vw, modified))
      file_views.sort(key = itemgetter(index))
      print file_views
      if index == 2:
         self.view.window().show_quick_panel([x + '   ' + \
            (datetime.datetime.fromtimestamp(z)).strftime("%d-%m-%y %H:%M") \
            for (x, y, z) in file_views], self.on_chosen)
      else:
         self.view.window().show_quick_panel([x for (x, y, z) in file_views], \
            self.on_chosen)

   def on_chosen(self, index):
      if index != -1:
         win = self.view.window()
         win.focus_view(file_views[index][1])

I've assigned key bindings:
Code: Select all
{ "keys": ["ctrl+alt+x"], "command": "ordered_files", "args": { "index": 0 }  },
{ "keys": ["ctrl+alt+c"], "command": "ordered_files", "args": { "index": 2 }  }

where index == 2 sorts by modified date.

I've been playing with 'rjust(50 - len(x))' to try and align the date-times to the right, but it's misbehaving a little. Andy.

Added: Doh! Why "piddle with padding" when I can just put the timestamp first :lol:

Code: Select all
import sublime, sublime_plugin, os, datetime
from operator import itemgetter

file_views = []

class OrderedFilesCommand(sublime_plugin.TextCommand):
   def run(self, edit, index):
      global file_views
      file_views = []
      for vw in self.view.window().views():
         head, tail = os.path.split(vw.file_name())
         modified = os.path.getmtime(vw.file_name())
         file_views.append((tail, vw, modified))
      file_views.sort(key = itemgetter(index))
      print file_views
      if index == 2:
         self.view.window().show_quick_panel([
            (datetime.datetime.fromtimestamp(z)).strftime("%d-%m-%y %H:%M ") + x \
            for (x, y, z) in file_views], self.on_chosen)
      else:
         self.view.window().show_quick_panel([x for (x, y, z) in file_views], \
            self.on_chosen)

   def on_chosen(self, index):
      if index != -1:
         win = self.view.window()
         win.focus_view(file_views[index][1])

Now perfect. Signing off.
Last edited by agibsonsw on Wed Mar 07, 2012 5:09 pm, edited 1 time in total.
"I'm here to save your life. But if I'm going to do that, I'll need total uninanonynymity." Me Myself & Irene.
agibsonsw
 
Posts: 901
Joined: Fri Jan 27, 2012 9:11 pm

Re: Sort Open Files

Postby tito on Wed Mar 07, 2012 5:05 pm

There is an API method to set the position of a tab in the tabbar with set_view_index. Example to move the current view to group 0 in position 0
sublime.active_window().set_view_index(sublime.active_window().active_view(), 0, 0)

You can check current view_index with
group, index = sublime.active_window().get_view_index(sublime.active_window().active_view())
print group
print index


http://www.sublimetext.com/docs/2/api_reference.html
Give APIs, let the community build the rest!
https://github.com/titoBouzout
tito
 
Posts: 849
Joined: Thu Sep 29, 2011 2:27 pm
Location: Montevideo, Uruguay

Re: Sort Open Files

Postby agibsonsw on Wed Mar 07, 2012 5:11 pm

Thank you tito. This could be an additional feature to the one I've built. Andy.
"I'm here to save your life. But if I'm going to do that, I'll need total uninanonynymity." Me Myself & Irene.
agibsonsw
 
Posts: 901
Joined: Fri Jan 27, 2012 9:11 pm

Re: Sort Open Files

Postby agibsonsw on Wed Mar 07, 2012 6:00 pm

Inspired/ encouraged by tito, the following will sort tabs alphabetically. My previous code was case-sensitive, whereas this is not. It should also sort within each existing group - although I haven't tried this.

Code: Select all
import sublime, sublime_plugin, os

class SortTabsCommand(sublime_plugin.TextCommand):
   def run(self, edit):
      file_views = []
      for vw in self.view.window().views():
         head, tail = os.path.split(vw.file_name())
         file_views.append((tail, vw))
      file_views.sort(key = lambda (tail, _): tail.lower())
      win = self.view.window()
      for index, (_, vw) in enumerate(file_views):
         group, _ = win.get_view_index(vw)
         win.set_view_index(vw, group, index)

Added: Careful! It doesn't work with more than one group as yet.
"I'm here to save your life. But if I'm going to do that, I'll need total uninanonynymity." Me Myself & Irene.
agibsonsw
 
Posts: 901
Joined: Fri Jan 27, 2012 9:11 pm

Re: Sort Open Files

Postby agibsonsw on Wed Mar 07, 2012 6:39 pm

Now we can work with more than one group (if you use such things..)

Code: Select all
import sublime, sublime_plugin, os
from operator import itemgetter

class SortTabsCommand(sublime_plugin.TextCommand):
   def run(self, edit):
      file_views = []
      win = self.view.window()
      curr_view = win.active_view()
      for vw in win.views():
         head, tail = os.path.split(vw.file_name())
         group, _ = win.get_view_index(vw)
         file_views.append((tail.lower(), vw, group))
      file_views.sort(key = itemgetter(2, 0))
      for index, (_, vw, group) in enumerate(file_views):
         if not index:
            prev_group = group
            moving_index = 0
         elif group > prev_group:
               moving_index = 0
               prev_group = group
         else:
            moving_index += 1
         win.set_view_index(vw, group, moving_index)
      win.focus_view(curr_view)
"I'm here to save your life. But if I'm going to do that, I'll need total uninanonynymity." Me Myself & Irene.
agibsonsw
 
Posts: 901
Joined: Fri Jan 27, 2012 9:11 pm

Re: Sort Open Files

Postby tito on Wed Mar 07, 2012 6:42 pm

I'm interested into having a "sort tabs" package. In rare occasions is useful, please add it to github. if you can add it to the sublime organization better.
Give APIs, let the community build the rest!
https://github.com/titoBouzout
tito
 
Posts: 849
Joined: Thu Sep 29, 2011 2:27 pm
Location: Montevideo, Uruguay

Re: Sort Open Files

Postby agibsonsw on Wed Mar 07, 2012 6:51 pm

tito wrote:I'm interested into having a "sort tabs" package. In rare occasions is useful, please add it to github. if you can add it to the sublime organization better.


I've put it on Gist for the moment. Oops, try this https://gist.github.com/1995067

This final (again) version of OrderedFiles.py is now case-insensitive:

Code: Select all
import sublime, sublime_plugin, os, datetime
from operator import itemgetter

file_views = []

class OrderedFilesCommand(sublime_plugin.TextCommand):
   def run(self, edit, index):
      global file_views
      file_views = []
      for vw in self.view.window().views():
         head, tail = os.path.split(vw.file_name())
         modified = os.path.getmtime(vw.file_name())
         file_views.append((tail, vw, modified))
      if index == 0:
         file_views.sort(key = lambda (tail, Doh, Dur): tail.lower())
      else:
         file_views.sort(key = itemgetter(2))
      if index == 2:
         self.view.window().show_quick_panel([
            (datetime.datetime.fromtimestamp(z)).strftime("%d-%m-%y %H:%M ") + x \
            for (x, y, z) in file_views], self.on_chosen)
      else:
         self.view.window().show_quick_panel([x for (x, y, z) in file_views], \
            self.on_chosen)

   def on_chosen(self, index):
      if index != -1:
         win = self.view.window()
         win.focus_view(file_views[index][1])
"I'm here to save your life. But if I'm going to do that, I'll need total uninanonynymity." Me Myself & Irene.
agibsonsw
 
Posts: 901
Joined: Fri Jan 27, 2012 9:11 pm

Re: Sort Open Files

Postby agibsonsw on Fri Mar 09, 2012 8:50 pm

OrderedFiles.py is more sensible as a WindowCommand, and I've tidied the code a little.

Code: Select all
import sublime_plugin
from os import path
from operator import itemgetter
from datetime import datetime

class OrderedFilesCommand(sublime_plugin.WindowCommand):
   def run(self, index):
      OF = OrderedFilesCommand
      OF.file_views = []
      win = self.window
      for vw in win.views():
         head, tail = path.split(vw.file_name())
         modified = path.getmtime(vw.file_name())
         OF.file_views.append((tail, vw, modified))
      if index == 0:      # sort by file name (case-insensitive)
         OF.file_views.sort(key = lambda (tail, _, Doh): tail.lower())
         win.show_quick_panel([x for (x, y, z) in OF.file_views], self.on_chosen)
      else:            # sort by modified date (index == 2)
         OF.file_views.sort(key = itemgetter(2))
         win.show_quick_panel([
            (datetime.fromtimestamp(z)).strftime("%d-%m-%y %H:%M ") + x \
            for (x, y, z) in OF.file_views], self.on_chosen)

   def on_chosen(self, index):
      if index != -1:
         self.window.focus_view(OrderedFilesCommand.file_views[index][1])

No previous poster has come back about this, so I've no idea if anyone is finding it useful (I am 8-) )

I was thinking it's (probably) possible to expand it such that typing a single number will jump to that (numbered) file: '(1) somefile.ext'. But having more than 10 files open might make it a little cumbersome.. But still, you could just type a second number :?:. A more complex solution would be to have a 'key' letter(s) highlighted (I believe? this is possible in the quick panel..) to cause the file to be focused.

But I won't bother unless someone expresses an interest :). Andy.
"I'm here to save your life. But if I'm going to do that, I'll need total uninanonynymity." Me Myself & Irene.
agibsonsw
 
Posts: 901
Joined: Fri Jan 27, 2012 9:11 pm

Re: Sort Open Files

Postby skube on Mon Mar 12, 2012 9:53 pm

How does one use this? Do you copy and save to a file call OrderedFiles.py? Does it have to go in any particular folder in the packages directory?
skube
 
Posts: 86
Joined: Tue Feb 28, 2012 8:42 pm

Re: Sort Open Files

Postby C0D312 on Mon Mar 12, 2012 10:11 pm

Go to Preferences > Browse Packages. Then save it in the User folder.
C0D312
 
Posts: 1063
Joined: Sun Jul 10, 2011 3:23 am

PreviousNext

Return to Ideas and Feature Requests

Who is online

Users browsing this forum: No registered users and 14 guests