Skip to content

Instantly share code, notes, and snippets.

@cjthomp
Created July 21, 2017 19:28
Show Gist options
  • Save cjthomp/b2fa84847ad533fb9a9d14c2142ba128 to your computer and use it in GitHub Desktop.
Save cjthomp/b2fa84847ad533fb9a9d14c2142ba128 to your computer and use it in GitHub Desktop.
Pushid plugin for SublimeText 3
import sublime, sublime_plugin
import random
import time
class PushidCommand(sublime_plugin.TextCommand):
def run(self, edit):
self.view.insert(edit, self.view.sel()[0].begin(), PushID().next_id())
class PushID(object):
PUSH_CHARS = ('-0123456789'
'ABCDEFGHIJKLMNOPQRSTUVWXYZ'
'_abcdefghijklmnopqrstuvwxyz')
def __init__(self):
self.lastPushTime = 0
self.lastRandChars = [0,0,0,0,0,0,0,0,0,0,0,0];
def next_id(self):
now = int(time.time() * 1000)
duplicateTime = (now == self.lastPushTime)
self.lastPushTime = now
timeStampChars = ["", "", "", "", "", "", "", ""]
for i in range(7, -1, -1):
timeStampChars[i] = self.PUSH_CHARS[now % 64]
now = int(now / 64)
if (now != 0):
raise ValueError('We should have converted the entire timestamp.')
uid = ''.join(timeStampChars)
if not duplicateTime:
for i in range(12):
self.lastRandChars[i] = int(random.random() * 64)
else:
for i in range(11, -1, -1):
if self.lastRandChars[i] == 63:
self.lastRandChars[i] = 0
else:
break
self.lastRandChars[i] += 1
for i in range(12):
uid += self.PUSH_CHARS[self.lastRandChars[i]]
if len(uid) != 20:
raise ValueError('Length should be 20.')
return uid
@jasas1
Copy link

jasas1 commented Jul 21, 2017

Cool

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment