Skip to content

Add sort by gist description or file extension alphabetically option. #155

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Open
wants to merge 1 commit into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
7 changes: 6 additions & 1 deletion Gist.sublime-settings
Original file line number Diff line number Diff line change
Expand Up @@ -44,5 +44,10 @@
//"gist_prefix": "Snippet:",

// Limit to gists with specific tag (#tag)
//"gist_tag": "snippet"
//"gist_tag": "snippet",

// Sort gists context menu alphabetically by description or file extension
// Example: "description"
// Example: "ext", "extension"
// "sort_gists": "description"
}
6 changes: 5 additions & 1 deletion README.md
Original file line number Diff line number Diff line change
Expand Up @@ -19,7 +19,7 @@ Go to the "Packages" directory (`Preferences` / `Browse Packages…`). Then clon
# Generating Access Token

As of [2013-05-16](https://github.com/blog/1509-personal-api-tokens), you can generate API Access Tokens via the Web UI or via the GitHub API.
**All other authorization methods is deprecated.**
**All other authorization methods are deprecated.**

## Web
* Account Settings -> [Applications](https://github.com/settings/applications)
Expand Down Expand Up @@ -71,6 +71,10 @@ Edit the settings file (it should open automatically the first time you use a Gi

Set the on-save behaviour of a loaded Gist. True implies that when the Gist is saved, it'll update the online Gist. False implies that it'll bring up a save dialog for the Gist to be saved to disk.

* `"sort_gists": description | ext`

You can set the context menu to sort alphabetically by either gist description or the first filename extension from the gist. Example: `"sort_gists": "description:"` or `"sort_gists": "ext:"`.


# Usage

Expand Down
69 changes: 69 additions & 0 deletions helpers.py
Original file line number Diff line number Diff line change
Expand Up @@ -52,6 +52,9 @@ def gist_title(gist):


def gists_filter(all_gists):

# Set variable for further reuse
sort_type = settings.get('sort_gists')
prefix = settings.get('gist_prefix')
if prefix:
prefix_len = len(prefix)
Expand All @@ -63,8 +66,10 @@ def gists_filter(all_gists):

gists = []
gists_names = []
gists_filenames = []

for gist in all_gists:

name = gist_title(gist)

if not gist['files']:
Expand All @@ -84,9 +89,29 @@ def gists_filter(all_gists):
else:
continue

if sort_type:

# Set the extra data for sorting
sorted_data = set_sort_data(sort_type, gist, name)

if not sorted_data is None:
gist = sorted_data[0]

if sort_type == 'extension' or sort_type == 'ext':
gists_filenames.append([sorted_data[1][0], sorted_data[1][1]])

gists.append(gist)
gists_names.append(name)

# Sort block
if sort_type:

# Custom sort the data alphabetically
sorted_results = sort_gists_data(sort_type, gists, gists_names, gists_filenames)
gists = sorted_results[0]
gists_names = sorted_results[1]


return [gists, gists_names]


Expand Down Expand Up @@ -117,3 +142,47 @@ def set_syntax(view, file_data):
view.set_syntax_file(new_syntax_path)
except:
pass

def set_sort_data(sort_type, gist, name):

# Set the gist description to the filename at element 0
# Same process that gist_title does for the title variable
if sort_type == 'description' and gist['description'] == "":
#print('Description')
gist['description'] = list(gist['files'].keys())[0]
return [gist]

# For sorting by extension lets add the extension to the gist list
if sort_type == 'extension' or sort_type == 'file extension' or sort_type == 'ext':
#print('Ext')
ext = list(gist['files'].keys())[0].split('.')
if 1 < len(ext):
gist['extension'] = ext[1]
else:
# This it to place gists without filenames at the end of the context list
gist['extension'] = '[No Filename]'

# Add to extensions name list
filename = [name[0], gist['extension']]
return [gist, filename]

def sort_gists_data(sort_type, gists, gists_names, gists_filenames):

if sort_type == 'description':
#print('Debug: Sort by description')

# Check for the filename to appear to determine how to sort
if not settings.get('prefer_filename'):
gists = sorted(gists, key=lambda k: k['description'].lower())
else:
gists = sorted(gists, key=lambda k: list(k['files'].keys())[0].lower())

gists_names = sorted(gists_names,key=lambda k: k[0].lower())

elif sort_type == 'extension' or sort_type == 'ext':
#print('Sort by file extension')

gists = sorted(gists, key=lambda k: (k['extension'] == "[No Filename]", k['extension'].lower()))
gists_names = sorted(gists_filenames, key=lambda k: (k[1] == "[No Filename]", k[1].lower()))

return [gists, gists_names]