Pushid plugin for SublimeText 3
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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 |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Cool