bat-extras icon indicating copy to clipboard operation
bat-extras copied to clipboard

bat-extras image previews

Open drewauff opened this issue 4 years ago • 3 comments

Is there a possiblity of having an "image/media preview" extra? its literally the only thing missing.

drewauff avatar Jul 10 '21 02:07 drewauff

As in, running bat some.png would show an image in the terminal?

I suppose it's possible if you use batpipe with less (and add a custom viewer), but image support in terminals is very terminal-specific and not well supported.

eth-p avatar Aug 21 '21 23:08 eth-p

yes thats exactly right, but for all (or at least the most command formats), the plan is to use it via fzf.

drewauff avatar Aug 23 '21 05:08 drewauff

I don't think this is something I would want to directly add to batpipe, since there isn't any standard around displaying images inside a terminal.

If you don't mind doing a little work yourself, you can create an external batpipe viewer and have it print the appropriate escape sequences for displaying images in your terminal.

An example (for iTerm2) would be something like this:

BATPIPE_VIEWERS=(iterm2img "${BATPIPE_VIEWERS[@]}")

viewer_iterm2img_supports() {
	# Ensure it only works in iTerm and that imgcat is installed.
	[[ -n "$ITERM_SESSION_ID" ]] || return 1
	command -v "imgcat" &> /dev/null || return 1

	# Filter it to only show for png and jpg.
	case "$1" in
		*.png|*.jpg)
			return 0
	esac

	return 1
}

viewer_iterm2img_process() {
	imgcat "$1"	
}

image

Keep in mind that it doesn't actually work inside less. less doesn't forward the escape sequence (ESC ] 1337 ; ... ^G) correctly, and it just ends up printing the base64 representation of the file.

image

eth-p avatar Aug 29 '21 21:08 eth-p