Sequence generator

Post your working scripts, libraries and tools.
william_ahk
Posts: 503
Joined: 03 Dec 2018, 20:02

Sequence generator

27 Nov 2022, 23:40

I often find myself writing loops for some number sequences, so I wrote this generator. Also, Gui in v2 is amazing! :clap:

Features:
  • Generate numbers from start to stop, increment by step, just like the for-loop.
  • Start can be greater than Stop, you can generate numbers in reverse.
  • Step can be zero, which will make Stop the count of numbers to generate.
  • Template is fed into the Format function, supporting any format option defined in the docs.
screenshot.png
screenshot.png (9.09 KiB) Viewed 2003 times

Code: Select all

#Requires AutoHotkey v2.0-a
#SingleInstance Force
#NoTrayIcon
if FileExist(trayicon_filepath := EnvGet("SystemRoot") "\System32\mmcndmgr.dll")
	TraySetIcon(trayicon_filepath, 48)
MainGui := Gui(, "Series Generator")
MainGui.SetFont("s12", "Courier")
MainGui.Add("Text", "xm", "Start:")
MainGui.Add("Edit", "x+10 w70 section")
MainGui.Add("UpDown", "Range-2147483648-2147483647 vstart")
MainGui.Add("Text", "x+18 vtext_stop", "Stop:")
MainGui.Add("Edit", "x+10 w70")
MainGui.Add("UpDown", "Range-2147483648-2147483647 vstop")
MainGui.Add("Text", "x+18", "Step:")
MainGui.Add("Edit", "x+10 w70")
MainGui.Add("UpDown", "vstep Range0-2147483647")
MainGui.Add("Text", "xm", "Template:")
MainGui.Add("Edit", "x+10 w150 vtemplate")
MainGui.Add("Text", "x+18", "Delimiter:")
MainGui.Add("Edit", "x+10 w60 vdelimiter")

; Default Values
MainGui["start"].value := 1
MainGui["stop"].value := 10
MainGui["step"].value := 1
MainGui["template"].value := "{}"
MainGui["delimiter"].value := "\n"

for ctl in MainGui
	if (ctl.type = "Edit")
		ctl.OnEvent("Change", UpdateState)
MainGui.Add("Edit", "xm r16 w420 voutput")
MainGui.Add("Button", "xm wp", "Copy").OnEvent("Click", (*) => (
	A_Clipboard := MainGui["output"].Text,
	Tooltip("Copied to clipboard"),
	SetTimer(() => ToolTip(), -1500)
))

UpdateState()

MainGui.Show()
MainGui.OnEvent("Close", (*) => ExitApp)
return


UpdateState(*) {
	global MainGui
	start := MainGui["start"].value
	stop := MainGui["stop"].value
	step := MainGui["step"].value
	template := MainGui["template"].value
	delimiter := MainGui["delimiter"].value
	delimiter := delimiter = "\n" ? "`n" : DecodeEscapeChar(delimiter)

	if (step > 0) {
		i := start
		incr := step
		ltr := start < stop
	} else {
		i := 1
		step := 1
		incr := 0
		ltr := true
	}
	if ltr {
		condition := () => (i <= stop)
	} else {
		condition := () => (i >= stop)
		step := -step
		incr := step
	}

	n := start
	output := ""
	while condition() {
		output .= delimiter Format(template, n)
		i += step
		n += incr
	}
	output := SubStr(output, 1+StrLen(delimiter))
	MainGui["output"].value := output
}

DecodeEscapeChar(str) {
	static esc := {n: "`n", r: "`r", b: "`b", t: "`t", v: "`v", a: "`a", f: "`f"}
	output := ""
	str := RegExReplace(str, "(.*?)(\\)(.)(?C)")
	output .= str
	return output

	pcre_callout(m, *) {
		output .= m[1] . (m[3] = "\" ? m[3] : esc.HasOwnProp(m[3]) ? esc.%m[3]% : "")
	}
}
peter_ahk
Posts: 140
Joined: 13 Feb 2024, 14:49

Re: Sequence generator

20 Apr 2024, 16:06

very nice, would it be ok if i add this as a tool to my macro lancher in there i am working on a setup gui to batch change button postions and this would be a great tool to include in there to generate a position list
william_ahk
Posts: 503
Joined: 03 Dec 2018, 20:02

Re: Sequence generator

20 Apr 2024, 19:09

peter_ahk wrote:
20 Apr 2024, 16:06
very nice, would it be ok if i add this as a tool to my macro lancher in there i am working on a setup gui to batch change button postions and this would be a great tool to include in there to generate a position list
Of course :)
peter_ahk
Posts: 140
Joined: 13 Feb 2024, 14:49

Re: Sequence generator

22 Apr 2024, 02:25

hello william could you tell me how to make it alwaysontop, i cant say i understand v2 code :)
william_ahk
Posts: 503
Joined: 03 Dec 2018, 20:02

Re: Sequence generator

22 Apr 2024, 02:57

Like so MainGui := Gui("+AlwaysOnTop", "Series Generator")
peter_ahk
Posts: 140
Joined: 13 Feb 2024, 14:49

Re: Sequence generator

22 Apr 2024, 02:59

william_ahk wrote:
22 Apr 2024, 02:57
Like so MainGui := Gui("+AlwaysOnTop", "Series Generator")
awesome thank you william :thumbup:
User avatar
kunkel321
Posts: 1162
Joined: 30 Nov 2015, 21:19

Re: Sequence generator

22 Apr 2024, 15:04

Pretty cool thing! I'm playing around with the Template: value... I don't really understand what is happening. I looked in the code, then found my way to this
https://www.autohotkey.com/docs/v2/lib/Format.htm

I'm just experimenting with some of the values in that ahk docs page...
If I set the Stop to 250, then set template to {1:2x} then I get what looks like a base-16 incrementation.
Spoiler
How is {1:2x} resulting in this?
ste(phen|ve) kunkel
william_ahk
Posts: 503
Joined: 03 Dec 2018, 20:02

Re: Sequence generator

22 Apr 2024, 19:21

william_ahk wrote:
27 Nov 2022, 23:40
Template is fed into the Format function, supporting any format option defined in the docs.
@kunkel321 I have mentioned this in the post :D

For the format string you tested, the syntax of the first parameter of Format is {Index:Format}. Index can only be 1 in this case as only a single value is provided to the function. It can be omitted if only one placeholder is used. Now to the other side - format specifiers,
  1. A decimal integer indicates the "width", or padding of the string;
    By default, values are right-aligned and spaces are used for padding.
    Format - Syntax & Usage
  2. A character from the type table indicates the type of the value, x means hexadecimal.
So :2x means, left pad 2 spaces and transform the value to hex.
I mainly use this to add leading zeroes by prefixing 0 to the width specifier, like so :02, producing the following:
01
02
03
04
05
06
07
08
09
10
There are of course many more possibilities with this function :trollface:
User avatar
kunkel321
Posts: 1162
Joined: 30 Nov 2015, 21:19

Re: Sequence generator

22 Apr 2024, 20:13

william_ahk wrote:
22 Apr 2024, 19:21
@kunkel321 I have mentioned this in the post :D
Ah yes, I see that now -- LOL!
And thanks for the extended explanation!
ste(phen|ve) kunkel
peter_ahk
Posts: 140
Joined: 13 Feb 2024, 14:49

Re: Sequence generator

28 May 2024, 01:23

hello william i hope you can help me out i know i should learn myself but i am deep into making a program in v1 and dont want to get confused so ive been staying away from v2

i have in my program a label that toggles this tool on or off but for that to work fully i need to write a value on guiclose in your cool little program but i dont know how

this line
v2 code

Code: Select all

MainGui.OnEvent("Close", (*) => ExitApp)
i need to also do this before closing so my other program can load its state for the toggle to always work
v1 code

Code: Select all

IniWrite, 0 , Settings\Setup Gui Settings.ini , setupinfo , NR Seq Generator Key
william_ahk
Posts: 503
Joined: 03 Dec 2018, 20:02

Re: Sequence generator

Today, 05:50

peter_ahk wrote:
28 May 2024, 01:23
i have in my program a label that toggles this tool on or off but for that to work fully i need to write a value on guiclose in your cool little program but i dont know how

this line
v2 code

Code: Select all

MainGui.OnEvent("Close", (*) => ExitApp)
i need to also do this before closing so my other program can load its state for the toggle to always work
v1 code

Code: Select all

IniWrite, 0 , Settings\Setup Gui Settings.ini , setupinfo , NR Seq Generator Key
Hi, sorry didn't see this earlier. For simplicity you can just attach a named function containing IniWrite to the close event MainGui.OnEvent("Close", YourFunction). Though I think a better design would be attaching it to a button that writes the value and closes the window.
peter_ahk
Posts: 140
Joined: 13 Feb 2024, 14:49

Re: Sequence generator

Today, 06:04

actualy i need it to only write on guiclose because it is an external program to my program that i can open or close with a hotkey (if i want to), however if someone uses gui close my toggle nr is off so having it do that on guiclose resets my hotkey basicly so that in all situations it does what it needs to do

thank you that very simple to implement

Return to “Scripts and Functions (v2)”

Who is online

Users browsing this forum: No registered users and 18 guests