Created
December 8, 2023 10:30
-
-
Save surajRathi/04bc59742b2e411041cd4300b4c4c4ef to your computer and use it in GitHub Desktop.
Copy tmux selection as html (colored)
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/python3 | |
# Formats taken from the ones that Jetbrains IDEs use. | |
HTML_FORMATS = ( | |
"text/html;charset=UTF-16", | |
"text/html;charset=UTF-16BE", | |
"text/html;charset=UTF-16LE", | |
"text/html;charset=ISO-8859-1", | |
"text/html;charset=US-ASCII", | |
"text/html", | |
) | |
TEXT_FORMATS = ( | |
# "text/rtf", | |
"UTF8_STRING", | |
"TEXT", | |
"STRING", | |
"text/plain;charset=UTF-16", | |
"text/plain;charset=UTF-8", | |
"text/plain;charset=UTF-16BE", | |
"text/plain;charset=UTF-16LE", | |
"text/plain;charset=ISO-8859-1", | |
"text/plain;charset=US-ASCII", | |
"text/plain", | |
"text/plain;charset=unicode", | |
) | |
inner_command = ( | |
r"tmux capture-pane -ep " | |
r"-S #{e|-|:#{selection_start_y},#{history_size}} " | |
r"-E #{e|-|:#{selection_end_y},#{history_size}} " | |
r"| head -c -#{e|-|:#{pane_width},#{selection_end_x}} | tail -c +#{selection_start_x}" # TODO: This doesn't count escape sequences properly | |
r"| aha --pink " | |
) | |
ESCAPED_SEMICOLON = r"\;" | |
pipe_command = ( | |
" | ".join( | |
f"xclip -selection clipboard -t {html_format.replace(r';',ESCAPED_SEMICOLON )} -f" | |
for html_format in HTML_FORMATS | |
) | |
+ " > /dev/null" | |
) | |
cmd = f"bash -c \"$(tmux display-message -p '#{{?#{{&&:#{{selection_present}},#{{selection_active}}}},{inner_command},}} | {pipe_command}')\"" | |
# [[ $0 != "$BASH_SOURCE" ]] && sourced=1 || sourced=0 | |
def main(): | |
# Two functions | |
# 1. Get buffer selection from tmux (plain-text) | |
# 2. Get capture output from tmux | |
# 3. Cut the extra characters from the capture | |
# 4. Convert the capture output to html | |
# 5. Set all the relevant xclip data types | |
print(cmd) | |
pass | |
if __name__ == "__main__": | |
main() |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment