Skip to content

Commit

Permalink
Added switch for repeating increments (#29)
Browse files Browse the repository at this point in the history
  • Loading branch information
duydao committed May 26, 2015
1 parent 2e3d551 commit 8e05d10
Show file tree
Hide file tree
Showing 2 changed files with 16 additions and 3 deletions.
1 change: 1 addition & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,7 @@ _Text Pastry_ is a free plugin for [Sublime Text](http://www.sublimetext.com/),
- Generate date ranges

## What's new ##
- 1.4.8: Added support for repeating increments [#29](https://github.com/duydao/Text-Pastry/issues/29) - Thanks [@mkruselj](https://github.com/mkruselj)
- 1.4.7: Fixed range command (stop condition was ignored)
- 1.4.6: Bugfix release for paste by row - Thanks [@Nieralyte](https://github.com/Nieralyte)
- 1.4.5: Added the paste by row command (use ```pr``` or ```pbr``` in the command line) [#28](https://github.com/duydao/Text-Pastry/issues/28)
Expand Down
18 changes: 15 additions & 3 deletions text_pastry.py
Original file line number Diff line number Diff line change
Expand Up @@ -745,8 +745,9 @@ def run(self, edit):

class TextPastryRangeCommand(sublime_plugin.TextCommand):
def run(self, edit, start=0, stop=None, step=1, padding=1, fillchar='0', justify=None,
align=None, prefix=None, suffix=None):
align=None, prefix=None, suffix=None, repeat_increment=None):
print('found range command', start, stop, step)
print('repeat increment:', repeat_increment)
start = int(start) if start else 0
step = int(step) if step else 1
stop = int(stop) if stop else None
Expand All @@ -766,6 +767,12 @@ def run(self, edit, start=0, stop=None, step=1, padding=1, fillchar='0', justify
if (start > stop and step > 0):
step = step * -1
items = [str(x) for x in range(start, stop, step)]
if repeat_increment and repeat_increment > 0:
tmp = items
items = []
for val in tmp:
for x in range(repeat_increment):
items.append(val)
if padding > 1:
fillchar = fillchar if fillchar is not None else '0'
just = str.ljust if justify == 'left' else str.rjust
Expand Down Expand Up @@ -1248,6 +1255,7 @@ def parse(self):
prefix = None
suffix = None
repeat = None
repeat_increment = None
flags = {}
remains = []
for pos, arg in enumerate(self.input_text.split()):
Expand Down Expand Up @@ -1280,21 +1288,25 @@ def parse(self):
prefix = arg[7:]
elif arg.lower().startswith('suffix='):
suffix = arg[7:]
elif arg.lower().startswith('each='):
repeat_increment = int(arg[5:])
elif arg.lower().startswith('x') and re.match(r"^x\d+$", arg) is not None:
repeat = int(arg[1:])
else:
remains.append(arg)
# regex matchers
if len(remains) > 0:
s = ' '.join(remains)
regex = r'^(?:\\?[iI])?[\s(]*(-?\d+)(?:[\s,]*(-?\d+))?(?:[\s,]*(-?\d+))?[\s)]*$'
regex = r'^(?:\\?[iI])?[\s(]*(-?\d+)(?:[\s,]*(-?\d+))?(?:[\s,]*(-?\d+))?(?:[\s,]*(-?\d+))?[\s)]*$'
m1 = re.match(regex, s)
if m1:
current = int(m1.group(1))
if m1.group(2):
step = int(m1.group(2))
if m1.group(3):
padding = int(m1.group(3))
if m1.group(4):
repeat_increment = int(m1.group(4))
remains = []
if len(remains) == 0 and current is not None:
# default values if valid
Expand All @@ -1305,7 +1317,7 @@ def parse(self):
if repeat is not None and step is not None and step != 0:
stop = start + ((repeat -1) * step)
return dict(command='text_pastry_range', args={'start': start, 'stop': stop, 'step': step, 'padding': padding, 'fillchar': fillchar,
'justify': justify, 'align': align, 'prefix': prefix, 'suffix': suffix})
'justify': justify, 'align': align, 'prefix': prefix, 'suffix': suffix, 'repeat_increment': repeat_increment})


class Overlay(object):
Expand Down

0 comments on commit 8e05d10

Please sign in to comment.