From 1cb9e9d14af8dc777482a122a9195c9e6e3970db Mon Sep 17 00:00:00 2001 From: Joe Lim <50560759+joelim-work@users.noreply.github.com> Date: Sun, 13 Apr 2025 11:14:00 +1000 Subject: [PATCH] Add `tty-write` command (#1961) This command offers a synchronized way to write to /dev/tty. --- complete.go | 1 + doc.md | 11 +++++++++-- doc.txt | 17 +++++++++++++---- eval.go | 13 +++++++++++++ lf.1 | 18 ++++++++++++++---- 5 files changed, 50 insertions(+), 10 deletions(-) diff --git a/complete.go b/complete.go index a4af8b28..44da95e2 100644 --- a/complete.go +++ b/complete.go @@ -80,6 +80,7 @@ var ( "mark-remove", "tag", "tag-toggle", + "tty-write", "cmd-escape", "cmd-complete", "cmd-menu-complete", diff --git a/doc.md b/doc.md index 2d4f5be3..f3ebc8bc 100644 --- a/doc.md +++ b/doc.md @@ -102,6 +102,7 @@ The following commands are provided by lf: mark-remove (modal) (default '"') tag tag-toggle (default 't') + tty-write The following command line commands are provided by lf: @@ -569,6 +570,12 @@ You can define a new tag-clearing command by combining `tag` with `tag-toggle` ( Tag a file with `*` or a single width character given in the argument if the file is untagged, otherwise remove the tag. +## tty-write + +Write the given string to the tty. +This is useful for sending escape sequences to the terminal to control its behavior (e.g. OSC 0 to set the window title). +Using `tty-write` is preferred over directly writing to `/dev/tty` because the latter is not synchronized and can interfere with drawing the UI. + # COMMAND LINE COMMANDS The prompt character specifies which of the several command-line modes you are in. @@ -1681,11 +1688,11 @@ You can define it just as you would define any other command: ' }} -If you want to print escape sequences, you may redirect `printf` output to `/dev/tty`. +If you want to send escape sequences to the terminal, you can use the `tty-write` command to do so. The following xterm-specific escape sequence sets the terminal title to the working directory: cmd on-cd &{{ - printf "\033]0; $PWD\007" > /dev/tty + lf -remote "send $id tty-write \"\033]0;$PWD\007\"" }} This command runs whenever you change the directory but not on startup. diff --git a/doc.txt b/doc.txt index 7f111a4d..1f4298ce 100644 --- a/doc.txt +++ b/doc.txt @@ -92,6 +92,7 @@ The following commands are provided by lf: mark-remove (modal) (default '"') tag tag-toggle (default 't') + tty-write The following command line commands are provided by lf: @@ -588,6 +589,14 @@ tag-toggle (default t) Tag a file with * or a single width character given in the argument if the file is untagged, otherwise remove the tag. +tty-write + +Write the given string to the tty. This is useful for sending escape +sequences to the terminal to control its behavior (e.g. OSC 0 to set the +window title). Using tty-write is preferred over directly writing to +/dev/tty because the latter is not synchronized and can interfere with +drawing the UI. + COMMAND LINE COMMANDS The prompt character specifies which of the several command-line modes @@ -1893,12 +1902,12 @@ would define any other command: ' }} -If you want to print escape sequences, you may redirect printf output to -/dev/tty. The following xterm-specific escape sequence sets the terminal -title to the working directory: +If you want to send escape sequences to the terminal, you can use the +tty-write command to do so. The following xterm-specific escape sequence +sets the terminal title to the working directory: cmd on-cd &{{ - printf "\033]0; $PWD\007" > /dev/tty + lf -remote "send $id tty-write \"\033]0;$PWD\007\"" }} This command runs whenever you change the directory but not on startup. diff --git a/eval.go b/eval.go index 82a17a0c..aeb70cd6 100644 --- a/eval.go +++ b/eval.go @@ -1061,6 +1061,19 @@ func (e *callExpr) eval(app *app, args []string) { app.ui.loadFile(app, true) app.ui.loadFileInfo(app.nav) } + case "tty-write": + if len(e.args) != 1 { + app.ui.echoerr("tty-write: requires an argument") + return + } + + tty, ok := app.ui.screen.Tty() + if !ok { + log.Printf("tty-write: failed to get tty") + return + } + + tty.Write([]byte(e.args[0])) case "scroll-down": if !app.nav.init { return diff --git a/lf.1 b/lf.1 index 62e8071c..ce595b78 100644 --- a/lf.1 +++ b/lf.1 @@ -1,6 +1,6 @@ .\" Automatically generated by Pandoc 2.11.4 .\" -.TH "LF" "1" "2025-04-08" "r34-21-g3fdd991" "DOCUMENTATION" +.TH "LF" "1" "2025-04-13" "" "DOCUMENTATION" .hy .SH NAME .PP @@ -102,6 +102,7 @@ mark-load (modal) (default \[dq]\[aq]\[dq]) mark-remove (modal) (default \[aq]\[dq]\[aq]) tag tag-toggle (default \[aq]t\[aq]) +tty-write \f[R] .fi .PP @@ -627,6 +628,15 @@ with \f[C]tag-toggle\f[R] (i.e. .PP Tag a file with \f[C]*\f[R] or a single width character given in the argument if the file is untagged, otherwise remove the tag. +.SS tty-write +.PP +Write the given string to the tty. +This is useful for sending escape sequences to the terminal to control +its behavior (e.g. +OSC 0 to set the window title). +Using \f[C]tty-write\f[R] is preferred over directly writing to +\f[C]/dev/tty\f[R] because the latter is not synchronized and can +interfere with drawing the UI. .SH COMMAND LINE COMMANDS .PP The prompt character specifies which of the several command-line modes @@ -2154,15 +2164,15 @@ cmd on-cd &{{ \f[R] .fi .PP -If you want to print escape sequences, you may redirect \f[C]printf\f[R] -output to \f[C]/dev/tty\f[R]. +If you want to send escape sequences to the terminal, you can use the +\f[C]tty-write\f[R] command to do so. The following xterm-specific escape sequence sets the terminal title to the working directory: .IP .nf \f[C] cmd on-cd &{{ - printf \[dq]\[rs]033]0; $PWD\[rs]007\[dq] > /dev/tty + lf -remote \[dq]send $id tty-write \[rs]\[dq]\[rs]033]0;$PWD\[rs]007\[rs]\[dq]\[dq] }} \f[R] .fi