Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

add default terminal implementation #5199

Closed
wants to merge 2 commits into from

Conversation

QiBaobin
Copy link
Contributor

@QiBaobin QiBaobin commented Jul 3, 2024

it seems that most of terminal support -e option, let use it as a fallback implementation so that we can use terminal and new on other undefined terminal like alacritty, Terminal.app.

@mawww
Copy link
Owner

mawww commented Jul 8, 2024

I am not sure how much $TERM can be used to find the actual binary of the current terminal, the TERM env var is used for terminfo resolution and common values that come to mind will not resolve correctly (say xterm-256color which would not find xterm or rxvt-unicode which would not match urxvt)

@QiBaobin
Copy link
Contributor Author

QiBaobin commented Jul 8, 2024

I am not sure how much $TERM can be used to find the actual binary of the current terminal, the TERM env var is used for terminfo resolution and common values that come to mind will not resolve correctly (say xterm-256color which would not find xterm or rxvt-unicode which would not match urxvt)

That's a good point, I made another change to ensure we have '$TERM' exists using which, could you take another look?

@Screwtapello
Copy link
Contributor

Do alacritty or Terminal.app even set $TERM to something that could be used to launch them?

My understanding is that most terminals set $TERM to "xterm" or "xterm-256color" for compatibility reasons, regardless of what the terminal's actual name is.

@QiBaobin
Copy link
Contributor Author

QiBaobin commented Jul 8, 2024

Do alacritty or Terminal.app even set $TERM to something that could be used to launch them?

My understanding is that most terminals set $TERM to "xterm" or "xterm-256color" for compatibility reasons, regardless of what the terminal's actual name is.

I tested, alacritty works as it set TERM to 'alacritty', and some others also works based on http://jdebp.uk./Softwares/nosh/guide/TERM.html , of course there are still a lot of others don't work.

In this patch, I added this to the last to only give it a try if none of others doesn't work.

@QiBaobin
Copy link
Contributor Author

QiBaobin commented Jul 9, 2024

@mawww , @Screwtapello , do you think using ps to get parent of parent of the kak client is a better idea?

ps -o 'args=' -p $(ps -o 'ppid=' -p %val{client_pid}) # the parent is the shell starting the kak, the parent of the shell would be the terminal

@QiBaobin
Copy link
Contributor Author

QiBaobin commented Jul 9, 2024

don't figure out a good way, let's close it for now

@QiBaobin QiBaobin closed this Jul 9, 2024
@QiBaobin
Copy link
Contributor Author

here is the logic from neofetch, it looks like complex.

get_term() {
    # If function was run, stop here.
    ((term_run == 1)) && return

    # Workaround for macOS systems that
    # don't support the block below.
    case $TERM_PROGRAM in
        "iTerm.app")    term="iTerm2" ;;
        "Terminal.app") term="Apple Terminal" ;;
        "Hyper")        term="HyperTerm" ;;
        *)              term="${TERM_PROGRAM/\.app}" ;;
    esac

    # Most likely TosWin2 on FreeMiNT - quick check
    [[ "$TERM" == "tw52" || "$TERM" == "tw100" ]] && term="TosWin2"
    [[ "$SSH_CONNECTION" ]] && term="$SSH_TTY"
    [[ "$WT_SESSION" ]]     && term="Windows Terminal"

    # Check $PPID for terminal emulator.
    while [[ -z "$term" ]]; do
        parent="$(get_ppid "$parent")"
        [[ -z "$parent" ]] && break
        name="$(get_process_name "$parent")"

        case ${name// } in
            "${SHELL/*\/}"|*"sh"|"screen"|"su"*|"newgrp") ;;

            "login"*|*"Login"*|"init"|"(init)")
                term="$(tty)"
            ;;

            "ruby"|"1"|"tmux"*|"systemd"|"sshd"*|"python"*|\
            "USER"*"PID"*|"kdeinit"*|"launchd"*|"bwrap")
                break
            ;;

            "gnome-terminal-") term="gnome-terminal" ;;
            "urxvtd")          term="urxvt" ;;
            *"nvim")           term="Neovim Terminal" ;;
            *"NeoVimServer"*)  term="VimR Terminal" ;;

            *)
                # Fix issues with long process names on Linux.
                [[ $os == Linux ]] && term=$(realpath "/proc/$parent/exe")

                term="${name##*/}"

                # Fix wrapper names in Nix.
                [[ $term == .*-wrapped ]] && {
                   term="${term#.}"
                   term="${term%-wrapped}"
                }
            ;;
        esac
    done

    # Log that the function was run.
    term_run=1
}

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

3 participants