-
Notifications
You must be signed in to change notification settings - Fork 33
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
hterm: implement OSC 1337 file display/transfer
This is the escape sequence used by iTerm2 for displaying files inline and downloading them. Currently we only support displaying of images. There is no container type in HTML for autodetecting formats, and at this point I'd rather not implement a custom mime type sniffer. Since things are transferred inline and embedded directly in the DOM, this does not handle large transfers well (think 5+ MB). Not clear whether there's much we could ever do about this, especially since the base64 encoding is so inefficient. One possible avenue of research is the HTML FS, but those too usually have quota limits. Based on the current parser framework, the escape sequence doesn't trigger until it's been fully received. That means we can't display a progress bar for showing overall transfers. We might revise that based on user feedback, or we just discourage people from doing large transfers here and develop a more efficient solution (e.g. zmodem or sixel). Two other limitations worth noting: - If the terminal is resized, the number of rows the image occupies is not updated accordingly. When increasing the character size, this can make the padded rows take up more empty space than the image. - The image is shown only when the last row is visible. This comes up when browsing the history from old->newer -- the image completely disappears. Picking the last row rather than the first row behaves better when old content automatically scrolls off (newer->older) which I suspect is the more common usage scenario. Change-Id: Ib1acc8addcf0b94a180b22712039b05d20c4dfc7 Reviewed-on: https://chromium-review.googlesource.com/484519 Tested-by: Mike Frysinger <[email protected]> Reviewed-by: Brandon Gilmore <[email protected]>
- Loading branch information
Showing
11 changed files
with
809 additions
and
15 deletions.
There are no files selected for viewing
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
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
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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,120 @@ | ||
#!/bin/sh | ||
# Copyright 2017 The Chromium OS Authors. All rights reserved. | ||
# Use of this source code is governed by a BSD-style license that can be | ||
# found in the LICENSE file. | ||
|
||
# Write an error message and exit. | ||
# Usage: <message> | ||
die() { | ||
echo "ERROR: $*" | ||
exit 1 | ||
} | ||
|
||
# Send a DCS sequence through tmux. | ||
# Usage: <sequence> | ||
tmux_dcs() { | ||
printf '\033Ptmux;\033%s\033\\' "$1" | ||
} | ||
|
||
# Send a DCS sequence through screen. | ||
# Usage: <sequence> | ||
screen_dcs() { | ||
printf '\033P\033%s\033\\' "$1" | ||
} | ||
|
||
# Send an escape sequence to hterm. | ||
# Usage: <sequence> | ||
print_seq() { | ||
local seq="$1" | ||
|
||
case ${TERM-} in | ||
screen*) | ||
# Since tmux defaults to setting TERM=screen (ugh), we need to detect | ||
# it here specially. | ||
if [ -n "${TMUX-}" ]; then | ||
tmux_dcs "${seq}" | ||
else | ||
screen_dcs "${seq}" | ||
fi | ||
;; | ||
tmux*) | ||
tmux_dcs "${seq}" | ||
;; | ||
*) | ||
echo "${seq}" | ||
;; | ||
esac | ||
} | ||
|
||
# Base64 encode stdin. | ||
b64enc() { | ||
base64 | tr -d '\n' | ||
} | ||
|
||
# Get the image height/width via imagemagick if possible. | ||
# Usage: <file> | ||
dimensions() { | ||
identify -format 'width=%wpx;height=%hpx;' "$1" 2>/dev/null | ||
} | ||
|
||
# Send the 1337 OSC sequence to display the file. | ||
# Usage: <file> | ||
show() { | ||
local name="$1" | ||
local opts="inline=1;$2" | ||
|
||
print_seq "$(printf '\033]1337;File=name=%s;%s%s:%s\a' \ | ||
"$(echo "$(basename "${name}")" | b64enc)" \ | ||
"$(dimensions "${name}")" \ | ||
"${opts}" \ | ||
"$(b64enc <"${name}")")" | ||
} | ||
|
||
# Write tool usage and exit. | ||
# Usage: [error message] | ||
usage() { | ||
if [ $# -gt 0 ]; then | ||
exec 1>&2 | ||
fi | ||
cat <<EOF | ||
Usage: hterm-show-file [options] <file> [options] | ||
Send a file to hterm. It can be shown inline or downloaded. | ||
This can also be used for small file transfers. | ||
EOF | ||
|
||
if [ $# -gt 0 ]; then | ||
echo | ||
die "$@" | ||
else | ||
exit 0 | ||
fi | ||
} | ||
|
||
main() { | ||
set -e | ||
|
||
while [ $# -gt 0 ]; do | ||
case $1 in | ||
-h|--help) | ||
usage | ||
;; | ||
-*) | ||
usage "Unknown option: $1" | ||
;; | ||
*) | ||
break | ||
;; | ||
esac | ||
done | ||
|
||
if [ $# -eq 0 ]; then | ||
die "Missing file to send" | ||
fi | ||
if [ $# -gt 2 ]; then | ||
usage "Too many arguments" | ||
fi | ||
|
||
show "$@" | ||
} | ||
main "$@" |
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
Oops, something went wrong.