#title: Cast-On Sampler (plain python)
# Creates several sheets, all with different cast-ons.

#Write header:
print(';!knitout-2')
print(';;Machine: SWGN2')
print(';;Carriers: 1 2 3 4 5 6 7 8 9 10')

#Parameters:
carrier = "3" #carrier name
min = 1 #needle number of left edge
max = 20 #needle number of right edge
rows = 20 #rows of knitting in each sample
cordWidth = 4 #cord width (for I-cord cast-on)

# === Alternating Tucks === 
#Bring in carrier:
print(f"inhook {carrier}")
#Set stitch table entry for cast-on:
print(f"x-stitch-number 101")

#On SWGN2 machines, carriers start on the right,
#so will start tucking onto needles right-to-left,
#and will be sure to tuck the rightmost needle in the first pass:
for n in range(max, min-1, -1):
	#tuck alternating needles, making sure to do the right edge:
	if (max - n) % 2 == 0:
		print(f"tuck - f{n} {carrier}")
#now, moving left-to-right, tuck the needles that were not tucked on the first pass:
for n in range(min, max+1, 1):
	if (max - n) % 2 != 0:
		print(f"tuck + f{n} {carrier}")
#Set stitch table entry for knitting:
print(f"x-stitch-number 105")
#knit two plain rows to allow cast-on stitches to relax:
# two isn't set in stone here -- it's just convenient
# for this example code to have the carrier end up on the right.
for n in range(max, min-1, -1):
	print(f"knit - f{n} {carrier}")
#send out yarn inserting hook; it is no longer needed to hold the yarn in place:
print(f"releasehook {carrier}")
for n in range(min, max+1, 1):
	print(f"knit + f{n} {carrier}")
# ---- single jersey sheet ----
for r in range(0, rows+1, 1):
	if r % 2 == 0:
		#left-going row:
		for n in range(max, min-1, -1):
			print(f"knit - f{n} {carrier}")
	else:
		#right-going row:
		for n in range(min, max+1, 1):
			print(f"knit + f{n} {carrier}")

# ---- take carrier out and drop remaining loops ----
#Take carrier out with yarn inserting hook:
print(f"outhook {carrier}")

#drop loops:
for n in range(min, max+1, 1):
	print(f"drop f{n}")

# === All-Needle Welt === 
#Bring in carrier:
print(f"inhook {carrier}")
#Set stitch table entry for cast-on:
print(f"x-stitch-number 101")

#On SWGN2 machines, carriers start on the right,
#so will start by tucking all needles right-to-left,
#and will be sure to tuck the leftmost back-bed needle last.

#Need to use quarter-pitch racking to tuck all needles in one pass:
print(f"rack -0.75")

for n in range(max, min-1, -1):
	print(f"tuck - f{n} {carrier}")
	print(f"tuck - b{n} {carrier}")

#Return to aligned racking:
print(f"rack 0")

#Set stitch table entry for knitting:
print(f"x-stitch-number 105")

#Do a rows of plain knitting on the front and back:
for n in range(min, max+1, 1):
	print(f"knit + f{n} {carrier}")
for n in range(max, min-1, -1):
	print(f"knit - b{n} {carrier}")
#One can knit more on the front or back beds here to make a larger "welt" at the edge.

#Stack back-bed stitches with front-bed stitches:
for n in range(max, min-1, -1):
	print(f"xfer b{n} f{n}")

#knit a plain row through the stacked stitches and return carrier to the right edge:
for n in range(min, max+1, 1):
	print(f"knit + f{n} {carrier}")

#send out yarn inserting hook; it is no longer needed to hold the yarn in place:
print(f"releasehook {carrier}")
# ---- single jersey sheet ----
for r in range(0, rows+1, 1):
	if r % 2 == 0:
		#left-going row:
		for n in range(max, min-1, -1):
			print(f"knit - f{n} {carrier}")
	else:
		#right-going row:
		for n in range(min, max+1, 1):
			print(f"knit + f{n} {carrier}")

# ---- take carrier out and drop remaining loops ----
#Take carrier out with yarn inserting hook:
print(f"outhook {carrier}")

#drop loops:
for n in range(min, max+1, 1):
	print(f"drop f{n}")

# === Twisted Tucks === 
#Bring in carrier:
print(f"inhook {carrier}")
#Set stitch table entry for cast-on:
print(f"x-stitch-number 101")

#Use a twisted tuck to cast on every stitch.
#Note: this requires a new pass for every stitch, so is pretty slow!
#There are both faster and fancier cast-ons.
#On SWGN2 machines, carriers start on the right, so will start with rightmost needle.
#Miss the rightmost needle to ensure yarn inserting hook is parked to its right instead of left:
print(f"miss - f{max} {carrier}")
for n in range(max, min-1, -1):
	print(f"tuck + f{n} {carrier}")
#Set stitch table entry for knitting:
print(f"x-stitch-number 105")
#knit a plain row to bring the carrier back to the right:
for n in range(min, max+1, 1):
	print(f"knit + f{n} {carrier}")
#send out yarn inserting hook; it is no longer needed to hold the yarn in place:
print(f"releasehook {carrier}")
# ---- single jersey sheet ----
for r in range(0, rows+1, 1):
	if r % 2 == 0:
		#left-going row:
		for n in range(max, min-1, -1):
			print(f"knit - f{n} {carrier}")
	else:
		#right-going row:
		for n in range(min, max+1, 1):
			print(f"knit + f{n} {carrier}")

# ---- take carrier out and drop remaining loops ----
#Take carrier out with yarn inserting hook:
print(f"outhook {carrier}")

#drop loops:
for n in range(min, max+1, 1):
	print(f"drop f{n}")

# === I-Cord === 
#Bring in carrier:
print(f"inhook {carrier}")
#Set stitch table entry for cast-on:
print(f"x-stitch-number 101")

#On SWGN2 machines, carriers start on the right,
#so will start by making a closed tube bind-off for the cord on the right.

#All-needle closed-tube cast-on, with carrier ending on the left:
print(f"rack -0.75")
for n in range(max, max - cordWidth-1, -1):
	print(f"tuck - f{n} {carrier}")
	print(f"tuck - b{n} {carrier}")
print(f"rack 0")

#Set stitch table entry for knitting:
print(f"x-stitch-number 105")

#Knit a small tube, leaving an extra stitch to the right and moving the stitches every course:
#Start first course of the tube:
for n in range(max - cordWidth, max+1, 1):
	print(f"knit + f{n} {carrier}")
#send out yarn inserting hook; it is no longer needed to hold the yarn in place:
print(f"releasehook {carrier}")
#Middle of tube:
for tubeMax in range(max, min-1, -1):
	for n in range(tubeMax, tubeMax - cordWidth-1, -1):
		print(f"knit - b{n} {carrier}")
	print(f"miss - b{tubeMax-cordWidth-1} {carrier}")
	for n in range(tubeMax - cordWidth, tubeMax+1, 1):
		print(f"xfer f{n} bs{n}")
	print(f"rack -1")
	for n in range(tubeMax - cordWidth, tubeMax+1, 1):
		print(f"xfer bs{n} f{n-1}")
	print(f"rack 0")
	for n in range(tubeMax - cordWidth, tubeMax+1, 1):
		print(f"knit + f{n-1} {carrier}")
	print(f"tuck + f{tubeMax} {carrier}")
	for n in range(tubeMax - cordWidth, tubeMax+1, 1):
		print(f"xfer b{n} fs{n}")
	print(f"rack 1")
	for n in range(tubeMax - cordWidth, tubeMax+1, 1):
		print(f"xfer fs{n} b{n-1}")
	print(f"rack 0")
#Finish last course of tube (and get carrier to the left):
for n in range(min - 1, min - cordWidth - 1-1, -1):
	print(f"knit - b{n} {carrier}")

#Closed bind-off on the tube:
for n in range(min - cordWidth - 1, min - 1+1, 1):
	print(f"xfer b{n} f{n}")
	print(f"knit + f{n} {carrier}")
	print(f"rack -1")
	print(f"xfer f{n} b{n+1}")
	print(f"knit + b{n+1} {carrier}")
	print(f"rack 0")
print(f"xfer b{min} f{min}")

#Knit regular course to get carrier to the right:
for n in range(min, max+1, 1):
	print(f"knit + f{n} {carrier}")
# ---- single jersey sheet ----
for r in range(0, rows+1, 1):
	if r % 2 == 0:
		#left-going row:
		for n in range(max, min-1, -1):
			print(f"knit - f{n} {carrier}")
	else:
		#right-going row:
		for n in range(min, max+1, 1):
			print(f"knit + f{n} {carrier}")

# ---- take carrier out and drop remaining loops ----
#Take carrier out with yarn inserting hook:
print(f"outhook {carrier}")

#drop loops:
for n in range(min, max+1, 1):
	print(f"drop f{n}")

