kovidgoyal / kitty

Cross-platform, fast, feature-rich, GPU based terminal
https://sw.kovidgoyal.net/kitty/
GNU General Public License v3.0
24.66k stars 989 forks source link

unset command is ignored in a bash function #7960

Closed rebelxt closed 1 month ago

rebelxt commented 1 month ago

Describe the bug Code to generate a custom terminal prompt works in a mate-terminal, but not in kitty.

To Reproduce

  1. Append the attached code to .bashrc or .bash_aliases.
  2. Restart the terminal.
  3. Enter the command: echo hello
  4. You should see something like (4.2ms) for command execution time. That is the case in the mate-terminal, but in the kitty terminal you see the time since the last command.

Screenshots kitty mate-terminal bash_aliases.txt

Environment details

kitty 0.32.2 created by Kovid Goyal
Linux MM22 6.8.0-45-generic #45-Ubuntu SMP PREEMPT_DYNAMIC Fri Aug 30 12:02:04 UTC 2024 x86_64
Linux Mint 22 Wilma MM22 /dev/tty

DISTRIB_ID=LinuxMint
DISTRIB_RELEASE=22
DISTRIB_CODENAME=wilma
DISTRIB_DESCRIPTION="Linux Mint 22 Wilma on nvme0n1p5"
Running under: X11
Frozen: False
Paths:
  kitty: /usr/bin/kitty
  base dir: /usr/lib/kitty
  extensions dir: /usr/lib/kitty/kitty
  system shell: /bin/bash
Loaded config files:
  /home/dave/.config/kitty/kitty.conf

Config options different from defaults:
clipboard_control          ('write-clipboard', 'write-primary')
command_on_bell            ['aplay', '-q', 'Documents/Audio/myding.wav']
cursor_blink_interval      0.0
cursor_stop_blinking_after 0
dim_opacity                0.75
editor                     xed
enable_audio_bell          False
enabled_layouts            ['stack', 'tall', 'horizontal', 'grid', 'fat', 'vertical']
focus_follows_mouse        True
font_family                IntelOne Mono
font_size                  10.0
inactive_text_alpha        0.7
initial_window_height      (40, 'cells')
initial_window_width       (92, 'cells')
modify_font:
    cell_height 0
    cell_width 0
remember_window_size       False
scrollback_lines           2048
select_by_word_characters  :@-./_~?&=%+#
single_window_margin_width FloatEdges(left=-1000.0, top=-1000.0, right=-1000.0, bottom=-1000.0)
tab_bar_edge               1
tab_title_template         {title}
update_check_interval      0.0
visual_bell_duration       0.5
window_alert_on_bell       False
window_border_width        (1.0, 'pt')
window_margin_width        FloatEdges(left=4.0, top=4.0, right=4.0, bottom=4.0)
Added shortcuts:
    ctrl+alt+0 →  tenth_window
    ctrl+alt+1 →  first_window
    ctrl+alt+2 →  second_window
    ctrl+alt+3 →  third_window
    ctrl+alt+4 →  fourth_window
    ctrl+alt+5 →  fifth_window
    ctrl+alt+6 →  sixth_window
    ctrl+alt+7 →  seventh_window
    ctrl+alt+8 →  eighth_window
    ctrl+alt+9 →  ninth_window
    ctrl+shift+alt+f →  goto_layout fat
    ctrl+shift+alt+g →  goto_layout grid
    ctrl+shift+alt+h →  goto_layout horizontal
    ctrl+shift+alt+s →  goto_layout stack
    ctrl+shift+alt+v →  goto_layout vertical
    kitty_mod+p > n →  kitten hints --type hash --program -, kitten hints --type word --program -, kitten hints --type line --program -, kitten hints --type path, kitten hints --type path --program -, kitten hints --type hyperlink, kitten hints --type linenum
    shift+alt+1 →  goto_tab 1
    shift+alt+2 →  goto_tab 2
    shift+alt+3 →  goto_tab 3
    shift+alt+4 →  goto_tab 4
    shift+alt+5 →  goto_tab 5
    shift+alt+6 →  goto_tab 6
    shift+alt+7 →  goto_tab 7
    shift+alt+8 →  goto_tab 8
    shift+alt+9 →  goto_tab 9
Removed shortcuts:
    kitty_mod+p > f →  kitten hints --type hyperlink, kitten hints --type linenum, kitten hints --type hash --program -, kitten hints --type word --program -, kitten hints --type line --program -, kitten hints --type path, kitten hints --type path --program -
Changed shortcuts:
    kitty_mod+alt+t →  goto_layout tall
    kitty_mod+e →  kitten hints
Colors:
    foreground                 #00cc00   

Important environment variables seen by the kitty process:
    PATH                                /usr/local/sbin:/usr/local/bin:/usr/sbin:/usr/bin:/sbin:/bin:/usr/games:/usr/local/games:/snap/bin:/home/dave/sync/programs:/home/dave/.local/bin
    LANG                                en_US.UTF-8
    SHELL                               /bin/bash
    DISPLAY                             :0.0
    USER                                dave
    XDG_SEAT                            seat0
    XDG_SESSION_TYPE                    x11
    XDG_SEAT_PATH                       /org/freedesktop/DisplayManager/Seat0
    XDG_SESSION_CLASS                   user
    XDG_SESSION_ID                      c2
    XDG_SESSION_PATH                    /org/freedesktop/DisplayManager/Session0
    XDG_RUNTIME_DIR                     /run/user/1000
    XDG_CURRENT_DESKTOP                 MATE
    XDG_SESSION_DESKTOP                 mate
    XDG_GREETER_DATA_DIR                /var/lib/lightdm-data/dave
    XDG_VTNR                            7
    XDG_CONFIG_DIRS                     /etc/xdg/xdg-mate:/etc/xdg
    XDG_DATA_DIRS                       /usr/share/mate:/home/dave/.local/share/flatpak/exports/share:/var/lib/flatpak/exports/share:/usr/local/share:/usr/share

Additional context The problem persists with kitty --config NONE. If the unset timer_start statement is commented out or deleted, the mate-terminal shows the same results as kitty.

This is in Linux Mint 22 Mate, my daily driver. I also have a NixOS test system with the Cinnamon DE, and just realized the problem doesn't exist there. The debug information from NixOS is also attached. kc_nix.txt

Is this a bug in kitty, or does a change need to be made in the Mate config fille?

kovidgoyal commented 1 month ago

Turn off shell integration in kitty.conf and you will be fine.

rebelxt commented 1 month ago

Thanks for the quick response, and the accurate solution.

kovidgoyal commented 1 month ago

Since I had a few minutes, you can fix your bash code to work with shell integration as well

function timer_now {
    date +%s%N
}

builtin declare -A timer_value

function timer_start {
    timer_value[v]=$(timer_now)
}

function timer_stop {
    local delta_us=$((($(timer_now) - ${timer_value[v]}) / 1000))
    local us=$((delta_us % 1000))
    local ms=$(((delta_us / 1000) % 1000))
    local s=$(((delta_us / 1000000) % 60))
    local m=$(((delta_us / 60000000) % 60))
    local h=$((delta_us / 3600000000))
    # Goal: always show around 3 digits of accuracy
    if ((h > 0)); then timer_show=${h}h${m}m
    elif ((m > 0)); then timer_show=${m}m${s}s
    elif ((s >= 10)); then timer_show=${s}.$((ms / 100))s
    elif ((s > 0)); then timer_show=${s}.$(printf %03d $ms)s
    elif ((ms >= 100)); then timer_show=${ms}ms
    elif ((ms > 0)); then timer_show=${ms}.$((us / 100))ms
    else timer_show=${us}$'\u3bc's
    fi
}

set_prompt () {
    Last_Command_RC=$? # Must come first!
    Blue='\[\e[1m\e[38;2;0;150;250m\]'
    White='\[\e[00;97m\]'
    Red='\[\e[01;31m\]'
    Green='\[\e[38;2;0;255;150m\]'
    Yellow='\[\e[00;93m\]'
    Reset='\[\e[00m\]'
    FancyX=$'\u2718'
    Checkmark=$'\u2713'
    Blink='\[\e[05m\]'
    BlinkOff='\[\e[25m\]'
    case $TERM in
        'xterm-kitty')
            Arrow="――►"
            ;;
        'login')
            Arrow="└─►"
            ;;
        *)
            Arrow="└─>"
            ;;
    esac

    if [[ $Last_Command_RC == 0 ]]; then
        # If it was successful, print a green check mark.
        PS1="$Green$Last_Command_RC $Checkmark "
    else
        # Otherwise, print a red X.
        PS1="$Red$Last_Command_RC $Blink$FancyX$BlinkOff "
    fi

    # Add the elapsed time and current time
    timer_stop
    PS1+="$White($timer_show) $Blue\t "

    # If root, just print the host in red. Otherwise, print the current user
    # and host in green.
    if [[ $EUID == 0 ]]; then
        PS1+="$Red\\u$Green@\\h "
    else
        PS1+="$Yellow\\u@\\h "
    fi
    # Print the working directory and prompt marker in blue, and reset
    # the text color to the default.
    PS1+="$White\\w\n$Arrow $Red\\\$$Reset "
}

trap 'timer_start' DEBUG
PROMPT_COMMAND='set_prompt'
rebelxt commented 1 month ago

Very nice! Thank you.