Skip to content

Instantly share code, notes, and snippets.

View nat-418's full-sized avatar

Nat nat-418

View GitHub Profile
@nat-418
nat-418 / static-variables.tcl
Last active November 14, 2021 08:26
Using Tcl's uplevel command to create a static variable control structure
# This is an example of using Tcl's uplevel to create new control structures,
# and can be found on page 59 of "Exploring Expect" by Don Libes.
# Like variables declared global, variables declared static are
# accessible from other procedures. However, the same static variable
# cannot be accessed by procedures in different files.
#
# @param args The aguments to static are the same as global.
# @return Void, the side effect is manipulating the static state array.
proc static {args} {
set unique [info script]
@nat-418
nat-418 / space2escape.tcl
Created November 14, 2021 13:11
Replace whitespace characters with escape codes in Tcl
# Replace whitespace characters with their escape codes
#
# @param str String containing some whitespace
# @return New string with escapes instead of whitespace
proc space2escape str {
set escapes {
\b \\b
\f \\f
\n \\n
\r \\r
@nat-418
nat-418 / box.tcl
Created November 14, 2021 13:16
Draw a pretty UTF-8 box around some text in Tcl
# Draw a UTF-8 box around some text. Useful for pretty-printing, logging, etc.
#
# @param str String to draw box around
# @return String enclosed in a fancy box
proc box str {
# Draw the top of the box
#
# @param length Number representing how long the top should be
# @return String drawing the top of the box
proc top length {
@nat-418
nat-418 / table.tcl
Last active November 14, 2021 14:22
Pretty-print a list of lists as a table in Tcl
# Format a list of lists into an evently-spaced table for logging etc.
#
# @param llst List of lists to format.
# @param padding Optionally specify how many spaces to separate fields, defaults to four.
# @return String of the pretty table.
proc table {llst {padding 4}} {
set lengths [lmap each [join $llst] {string length $each}]
set longest [expr $padding + max([join $lengths ","])]
foreach lst $llst {
@nat-418
nat-418 / making-tcl-starpacks.md
Last active November 25, 2021 23:51
How to make standalone native binaries from Tcl scripts.

How to make Tcl Starpacks

Tcl scripts can be made into standalone executables for various platforms in what are called Starpacks. Statically linking Tcl libraries in a Starpack helps make Tcl programs more portable.

Building a Starpack requires:

  1. The sdx tool.
  2. A TclKit runtime for your operating system and architecture.
@nat-418
nat-418 / translate.tcl
Created December 7, 2021 16:45
Wrapper script for Argos Translate
#!/bin/env tclsh
# language should be a two-character ISO code
# languages must be installed using Argos GUI
set language_codes {
ar de en es fr it zh
}
# CLI takes two arguments, ignore the rest
lassign $argv from_code to_code
@nat-418
nat-418 / posix-prompt-for-editor-input.sh
Last active March 13, 2022 11:33
How to prompt user input from an interactive text editor in POSIX shell
#!/bin/sh
#
# How to prompt user input from an interactive text editor in POSIX shell.
# ======================================================================
#
# Permission to use, copy, modify, and/or distribute this software for any
# purpose with or without fee is hereby granted.
#
# THE SOFTWARE IS PROVIDED "AS IS" AND THE AUTHOR DISCLAIMS ALL WARRANTIES WITH
# REGARD TO THIS SOFTWARE INCLUDING ALL IMPLIED WARRANTIES OF MERCHANTABILITY
@nat-418
nat-418 / tcl-prompt-for-editor-input.sh
Created March 13, 2022 14:23
How to read user input from an interactive text editor in Tcl
#!/usr/bin/env tclsh
#
# How to read user input from an interactive text editor in Tcl
# =============================================================
#
# Permission to use, copy, modify, and/or distribute this software for any
# purpose with or without fee is hereby granted.
#
# THE SOFTWARE IS PROVIDED "AS IS" AND THE AUTHOR DISCLAIMS ALL WARRANTIES WITH
# REGARD TO THIS SOFTWARE INCLUDING ALL IMPLIED WARRANTIES OF MERCHANTABILITY
@nat-418
nat-418 / making-tcl-packages-and-modules.md
Last active November 5, 2022 23:13
How to make Tcl packages and modules

How to make Tcl packages and modules

Tcl provides a package system that allows Tclers to share code. This package system supports two different formats that serve different use-cases:

  1. The "package" or "8.5" format is more verbose, but allows package authors more flexibility through the pkgIndex.tcl file. This is designed for complex, multi-file programs.
@nat-418
nat-418 / expecting.md
Created January 8, 2023 15:16 — forked from ksafranski/expecting.md
Basic principles of using tcl-expect scripts

Intro

TCL-Expect scripts are an amazingly easy way to script out laborious tasks in the shell when you need to be interactive with the console. Think of them as a "macro" or way to programmaticly step through a process you would run by hand. They are similar to shell scripts but utilize the .tcl extension and a different #! call.

Setup Your Script

The first step, similar to writing a bash script, is to tell the script what it's executing under. For expect we use the following:

#!/usr/bin/expect