microsoft / terminal

The new Windows Terminal and the original Windows console host, all in the same place!
MIT License
95.26k stars 8.27k forks source link

wt new-tab open near current tab #14687

Open KalleOlaviNiemitalo opened 1 year ago

KalleOlaviNiemitalo commented 1 year ago

Description of the new feature/enhancement

I have a Bash function that opens a tab with PowerShell in the current directory:

vspwsh ()
{
    wt new-tab --profile "Developer PowerShell for VS 2017" --startingDirectory .
}

I'd like this to place the new PowerShell tab next to the Bash tab in which I run the command. That way, it would be easier for me to keep track of the purpose of each tab, keeping tabs near each other when they are for working on the same directory.

This is somewhat related to https://github.com/microsoft/terminal/issues/3158 but would not depend on any escape sequences emitted by the shell.

Proposed technical implementation details (optional)

Add a --near-current-tab option to wt new-tab. When specified, use the WT_SESSION environment variable to find the session, tab, and window in which the command was run, and place the new tab near that one.

If both --near-current-tab and --window are specified, then ignore WT_SESSION and instead use the selected tab of the specified window.

237dmitry commented 1 year ago

There is the parameter in Preview version:

"newTabPosition": "afterCurrentTab" //  or "afterLastTab" (default)

Command will open new tab near current tab:

wt -w 0 nt -p "<profile>" -d .
KalleOlaviNiemitalo commented 1 year ago

newTabPosition https://github.com/microsoft/terminal/issues/12955 is a global option; I want afterCurrentTab just for this function and not for opening tabs via Windows Terminal key combinations.

It would be okay to reuse the enum type though. wt new-tab --position afterCurrentTab

KalleOlaviNiemitalo commented 1 year ago

This function is somewhat like duplicating a tab https://github.com/microsoft/terminal/issues/14313 but I'm specifying a different profile for the duplicate. OTOH, if I had manually changed the color of the Bash tab, it would be okay for the new PowerShell tab to inherit that.

237dmitry commented 1 year ago

newTabPosition is a global option

Changing the configuration via settings.json for most settings happens on the fly. I am not against --near switch. This more convenient. As a workaround I would add the necessary actions to function.

Something like:


# pwsh
# $WT_SETTINGS is the custom environment variable

function vsbash {
param (
    [Parameter()]
    [switch] $near
)

    $json = Get-Content $WT_SETTINGS | ConvertFrom-Json

    if ($near) { $json.newTabPosition = 'afterCurrentTab' }
    else { $json.newTabPosition = 'afterLastTab' }

    $json | ConvertTo-Json -Depth 5 | Set-Content $WT_SETTINGS

    wt --window 0 new-tab --profile "bash" --startdirectory "."
}
$ vsbash -near
zadjii-msft commented 1 year ago

Okay there's two thoughts here:

We might want to workshop the parameters a bit. --position feels like it should be shortened to -p, which is already used by --profile. But maybe it doesn't need to be shortened at all! wt new-tab --newTabPosition ... does seem a little redundant.

Vague todo list:

fxxxxr commented 1 year ago

HEY ! CAN I HAVE MY HANDS ON THIS ISSUE ?

zadjii-msft commented 1 year ago

GO FOR IT! FEEL FREE TO COMMENT IF YOU HAVE ANY QUESTIONS ☺️

daemon-reconfig commented 1 year ago

hello is this issue still open?

lhecker commented 1 year ago

Yes.

AlexJMercer commented 10 months ago

Hello,

I'd like to take this up as my very first contribution to get into open source. I've not done this before, and I'd really appreciate if you could help me out with getting started.

After cloning the repo and opening the OpenConsole.sln (assuming that's what I'm supposed to open) in VS2017:

  1. How do I understand what files I should be looking out for?
  2. How to test my changes? (I assume I need to build the application but I don't understand how to do that even after going through the CONTRIBUTING.MD)

Any pointers or links to external references would be really helpful. Thank you for your time.

lhecker commented 9 months ago

As a prerequisite you need to use VS 2022: https://github.com/microsoft/terminal#prerequisites If I remember correctly, the relevant function for new-tab insertion is this one: https://github.com/microsoft/terminal/blob/b02316b37c78fd2efefdfba676a504e816176fdf/src/cascadia/TerminalApp/TabManagement.cpp#L228 It already has an index parameter which defaults to -1 (= unspecified), so only the callers need to be updated to use the current tab-index instead. To test your changes simply build WindowsTerminal and run it. We'll then test the changes again around the time we merge your PR.

KalleOlaviNiemitalo commented 9 months ago

Please note the request is for opening a new tab near the "tab in which I run the command", which might not be the selected tab of the window. For example, say I have a window with three tabs A, B, and C, and I run sleep 10 && wt new-tab --position afterCurrentTab in tab A. The command should then create the new tab D near tab A, and the resulting order of tabs should be A D B C, even if I select tab C during the 10-second delay.

nivowski commented 8 months ago

I have seen that you can open a tab with a shortcut keybind but how do you achieve to open a new tab using wt command? Whenever I use wt new-tab, it always open a new Terminal instance.So, is there actually a different command or option to achieve this behaviour? Thank you in advance

zadjii-msft commented 8 months ago

So, wt new-tab will obey your "New instance behavior" setting. So by default (as of 1.20), this will open a new window. But you can override that with something like wt -w 0 new-tab, where -w 0 says "run this wt command in the current window"

nivowski commented 8 months ago

I'm currently working on this feature request here: https://github.com/microsoft/terminal/compare/main...nivowski:terminal:feature/wt-new_tab_position

It run with success but doesn't work when trying to pass the --tabPosition parameter(wt -w 0 new-tab --tabPosition 4):

image

Any help would be much appreciated

ezParth commented 6 months ago

vspwsh () { local near_current_tab=0 local window_id=""

while [[ "$#" -gt 0 ]]; do
    case $1 in
        --near-current-tab)
            near_current_tab=1
            shift
            ;;
        --window)
            window_id="$2"
            shift 2
            ;;
        *)
            echo "Unknown option: $1"
            return 1
            ;;
    esac
done

if [[ $near_current_tab -eq 1 && -n $window_id ]]; then
    echo "Error: Both --near-current-tab and --window cannot be specified together."
    return 1
fi

local wt_args=("--profile" "Developer PowerShell for VS 2017" "--startingDirectory" ".")

if [[ $near_current_tab -eq 1 ]]; then
    if [[ -n $WT_SESSION ]]; then
        local session_id=$(echo "$WT_SESSION" | cut -d";" -f1)
        local tab_id=$(echo "$WT_SESSION" | cut -d";" -f2)
        local window_id=$(echo "$WT_SESSION" | cut -d";" -f3)
        wt_args+=("--window" "$window_id" "--tab" "$tab_id")
    else
        echo "Error: WT_SESSION environment variable not set."
        return 1
    fi
elif [[ -n $window_id ]]; then
    wt_args+=("--window" "$window_id")
fi

wt new-tab "${wt_args[@]}"

}

Ismith507 commented 6 months ago

Hi is this issue still open?

zadjii-msft commented 6 months ago

Hi is this issue still open?

Yep. a screenshot of this issue still being open, based on the fact that it's labeled as open