Skip to content

Instantly share code, notes, and snippets.

@cjthomp cjthomp/pushid.py
Created Jul 21, 2017

Embed
What would you like to do?
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

This comment has been minimized.

Copy link

commented Jul 21, 2017

Cool

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
You can’t perform that action at this time.