Created
March 13, 2022 14:23
-
-
Save nat-418/7c2757721fb09e82e8d795383aa4061a to your computer and use it in GitHub Desktop.
How to read user input from an interactive text editor in Tcl
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
#!/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 | |
# AND FITNESS. IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR ANY SPECIAL, DIRECT, | |
# INDIRECT, OR CONSEQUENTIAL DAMAGES OR ANY DAMAGES WHATSOEVER RESULTING FROM | |
# LOSS OF USE, DATA OR PROFITS, WHETHER IN AN ACTION OF CONTRACT, NEGLIGENCE OR | |
# OTHER TORTIOUS ACTION, ARISING OUT OF OR IN CONNECTION WITH THE USE OR | |
# PERFORMANCE OF THIS SOFTWARE. | |
# | |
# Editor to use, set to vi if not specifed in the environment | |
# ----------------------------------------------------------- | |
if {[info exists ::env(EDITOR)]} { | |
set editor $::env(EDITOR) | |
} else { | |
set editor "vi" | |
} | |
# Create a temporary input file | |
# ----------------------------- | |
file tempfile temp_file | |
# Open the input file for editing | |
# ------------------------------- | |
exec $editor $temp_file <@ stdin >@ stdout 2>@ stderr | |
# Read the file into a variable | |
# ----------------------------- | |
set opened_temp_file [open $temp_file r] | |
set user_input [read $opened_temp_file] | |
# Cleanup | |
# ------- | |
close $opened_temp_file | |
file delete $temp_file | |
# Use the input | |
# ------------- | |
puts -nonewline $user_input | |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment