【问题标题】:How to get the full current command in Fish Shell如何在 Fish Shell 中获取完整的当前命令
【发布时间】:2021-09-20 12:48:17
【问题描述】:

我想使用terminal-notifier 构建包含有关当前命令的信息的通知,但我找不到获取当前命令的方法,在下面标记为(????)

alias notify=terminal-notifier -message "Done with [(????)]" -title (if test $status = 0; echo "$_ suceeded"; else; echo "$_ exited with error [$status]"; end) -sound default -ignoreDnD

我将其用作echo 1; notify,消息将是Done with [echo 1;notify]

我尝试过$_status current-commandcommandline,但既不包括参数也不包括管道后面的内容。 history | head -n1 在执行之前没有命令。

【问题讨论】:

    标签: fish


    【解决方案1】:

    为了确保您捕获整个命令行(而不仅仅是命令),我想我会做一些类似的事情:

    function notify
        eval "$argv"
        set s $status
        if test $s = 0
            set message Succeeded
        else
            set message "Exited with error [$s]"
        end
        echo "Done with '$argv' - $message"
    end
    
    > notify echo 1
    Done with 'echo 1' - Succeeded
    
    > notify false
    Done with 'false' - Exited with error [1]
    
    # Put multiple commands in quotes, if needed
    > notify "echo 1; echo 2"
    Done with 'echo 1; echo 2' - Succeeded
    

    为简单起见,我用基本的echo 替换了您的terminal-notifier,因为我不在Mac 上。

    虽然 commandline 基于手册页/文档看起来很有希望,但 iirc 它实际上只在键绑定和/或完成功能中有用。

    我猜你已经知道这一点,但如果不知道(或其他读者),请将 notify 函数放入 ~/.config/fish/functions/notify.fish 以使其持久化(并按需延迟加载)。

    【讨论】:

      猜你喜欢
      • 2015-03-31
      • 2015-09-23
      • 2010-10-11
      • 1970-01-01
      • 2015-01-14
      • 2014-08-05
      • 1970-01-01
      • 2010-10-26
      • 2011-01-28
      相关资源
      最近更新 更多