FoxDot & Troop tutorial - How to print the code in a distant terminal

While preparing our lastest stream, we encountered the following problem. We each have a laptop with Linux + Troop + Foxdot for our live stream and a Windows PC dedicated only to OBS streaming (with several webcams, overlay and Foxdot / Troop code).
The idea was to have something very simple and lightweight that could display our Troop code without having a 3rd Troop client open or any unnecessary clutter.
Our testing with CrashOS using OpenFrameworks had given us the idea to use UDP packet in order to send the code.
In order to do this we needd to modify the Troop client to send a UDP packet for each code evaluation, launch a script that receives the UDP code on the machine that streams and display it in a nice terminal.

1. TROOP MOD TO SEND UDP.

First you need to edit each Troop client with the following lines. This will send a UDP packet with your code preceded by ‘#’ for the Troop user name ‘Crash’ or ‘!’ for the user ‘server’. Don’t forget to change the name with the same as your Troop login. We will use this symbol in the next receive python script.

Edit the print_stdin() function in the Troop/src/interpreter.py (something like line 128).

crashOS_socket = socket.socket(socket.AF_INET, socket.SOCK_DGRAM)
crashOsEnable = True
crashOSIp = "192.168.0.10" # the stream/remote machine IP
crashOSPort = 20000

def print_stdin(self, string, name=None, colour="White"):
		""" Handles the printing of the execute code to screen with coloured
			names and formatting """
		# Split on newlines
		string = [line.replace("\n", "") for line in string.split("\n") if len(line.strip()) > 0]
		if len(string) > 0 and name is not None:
			name = str(name)
			print(colour_format(name, colour) + _ + string[0])
			
			# crash mod
			try:
				for i in range(0,len(string)):
					if crashOsEnable:
						#if string[0][0].isalpha() and string[0][1].isdigit():
						if name == "Crash":
							byte_message = bytes("#" + string[i], "utf-8")
						elif name == "Server":
							byte_message = bytes("!" + string[i], "utf-8")
						else:
							byte_message = ""
						crashOS_socket.sendto(byte_message, (crashOSIp, crashOSPort))
			except:
				print("Send udp error")
			#

			# Use ... for the remainder  of the  lines
			n = len(name)
			for i in range(1,len(string)):
				sys.stdout.write(colour_format("." * n, colour) + _ + string[i])
				sys.stdout.flush()
		return

2. Python script to receive and print Udp code.

import socket
import sys

class bcolors:
	SVDK = '\033[96m'
	ZBDM = '\033[95m'
	SERVER = '\033[31m'
	ENDC = '\033[0m'
	
if len(sys.argv) == 3:
	# Get "IP address of Server" and also the "port number" from
	#argument 1 and argument 2
	ip = sys.argv[1]
	port = int(sys.argv[2])
else:
	print("Run like : python3 server.py <arg1:server ip:this system IP 192.168.1.6> <arg2:server port:4444 >")
	exit(1)


# Create a UDP socket
s = socket.socket(socket.AF_INET, socket.SOCK_DGRAM)
# Bind the socket to the port
server_address = (ip, port)
s.bind(server_address)
print("Do Ctrl+c to exit the program !!")
print("####### Server is listening #######")

while True:
	data, address = s.recvfrom(4096)
	datadec = data.decode('utf-8')
	if datadec[0] == "#": # svdk color
		print(f"SVDK: {bcolors.SVDK}{datadec[1:]}")
	elif datadec[0] == "!": # server color
		print(f"ZBDM: {bcolors.ZBDM}{datadec[1:]}")
	elif datadec[0] == "@":
		print(f"SERVER: {bcolors.SERVER}{datadec[1:]}")

On the stream machine run this script with :

python name_of_the_file.py "0.0.0.0" 20000

where “0.0.0.0” is for listening to all IP address and 20000 is the port configured in Troop.

3. Terminal tuning to a retro CRT look.

We use the new Windows 10/11 Terminal (download on the W$ store) as you can now have a old retro crt look using shaders in HLSL format. For Windows users, you can download a nice CRT shader here and there are loads of ressources concerning HLSL shaders if needed. If you’re on Linux (you’re so cool) or mac, you can use the cool-retro-term which is very nice.

 

Cool retro term with code from troop and foxdot