是否不可能编写一个运行正常的 tmux shell 脚本来启动 ncmpcpp 的 3 个面板?

Is it impossible to write a functioning tmux shell script that launches 3 panes of ncmpcpp?

这是我第二次发布这个问题,因为整个情况比我想象的还要混乱。

我正在尝试制作一个脚本来打开 tmux,将其分成 3 个窗格,使它们水平大小相等,然后在所有窗格中启动 ncmpcpp。这是我设法编写的脚本:

#!/bin/sh

tmux new-session -d 'ncmpcpp'
printf 'creating session\n'
tmux split-window -h 'ncmpcpp'
printf 'window split 1\n'
tmux split-window -h 'ncmpcpp'
printf 'window split 2\n'
tmux select-layout even-horizontal
printf 'evening out panes\n'
tmux a #
printf 'attaching session\n'

我完全是编写脚本的初学者,如果这里的错误很明显,我深表歉意。 printfs 在那里,这样我就可以确定脚本在何处使 tmux 崩溃。显然,它在 tmux a # 崩溃了。

奇怪的是,崩溃是瞬间发生的。由于它消失的速度太快,我无法截屏。

terminate called after throwing an instance of `std::logic_error
what() : constructed window couldn't fit into terminal'

基本上,窗格太小,无法在其中启动 ncmpcpp。尽管我可以在更小的终端中手动启动 ncmpcpp。

然而,经过一番折腾,我无意中"kind of"解决了问题

我把脚本搞砸了,结果是这样的:

#!/bin/sh

printf '3[8;29;252t'
tmux new-session -d 

tmux select-layout even-horizontal
tmux send-keys -t 0 'tput cols' Enter
tmux split-window -h 

sleep 1

tmux select-layout even-horizontal
tmux send-keys -t 1 'tput cols' Enter
tmux split-window -h 

sleep 1

tmux select-layout even-horizontal
tmux send-keys -t 2 'tput cols' Enter

sleep 1

tmux send-keys -t 0 'ncmpcpp' Enter
tmux send-keys -t 1 'ncmpcpp' Enter
tmux send-keys -t 2 'ncmpcpp' Enter
tmux a #

开始时暂停 3 秒,然后立即启动 tmux,有 3 个面板,所有面板上都有 ncmpcpp。

...有时。

它并非一直有效,有时某些窗格会彻底崩溃,有时则不会。

老实说,我对这里发生的事情感到很困惑。我的猜测是窗格的大小没有记录好,足以搞砸每个窗格有多少 columns/rows 的值。但老实说,我很难过。

这就是我问这个问题的原因 - ncmpcpp 仅在终端足够大时启动。由于 tmux 混淆了说明列是什么的实际值,是否不可能执行我在标题中所说的内容?

我以为不可能,但我错过了一些东西。

tmux new-session -d

为了使 ncmpcpp 工作,需要指定实际会话的大小。这就是打乱价值观的原因。为了修复它,我这样做了:

tmux new-session -d -x 252 -y 29

之后效果很好。

这是最终脚本:

 #!/bin/sh

printf '3[8;29;252t'
tmux new-session -d -x 252 -y 29

tmux select-layout even-horizontal
tmux split-window -h 

tmux select-layout even-horizontal
tmux split-window -h 

tmux select-layout even-horizontal

tmux send-keys -t 0 'ncmpcpp' Enter
tmux send-keys -t 1 'ncmpcpp' Enter
tmux send-keys -t 2 'ncmpcpp' Enter

tmux send-keys -t 1 '='
tmux send-keys -t 2 '8'

tmux a #

所以

#!/bin/sh

printf '3[8;29;252t'
tmux new-session -d -x 252 -y 29

tmux select-layout even-horizontal
tmux send-keys -t 0 'tput cols' Enter
tmux split-window -h 

sleep 1

tmux select-layout even-horizontal
tmux send-keys -t 1 'tput cols' Enter
tmux split-window -h 

sleep 1

tmux select-layout even-horizontal
tmux send-keys -t 2 'tput cols' Enter

sleep 1

tmux send-keys -t 0 'ncmpcpp -s playlist' Enter
tmux send-keys -t 1 'ncmpcpp -s media_library' Enter
tmux send-keys -t 2 'ncmpcpp -s clock' Enter

# help,playlist,browser,search_engine,media_library,
# playlist_editor,tag_editor,outputs,visualizer,clock

tmux a #

是你自己的答案。您是否尝试过更改 ncmpcpp 布局: (用 -c 调用)

ncmpcpp -c ~/.my_ncmpcpp.conf

#~/.my_ncmpcpp.conf
#edited from https://bbs.archlinux.org/viewtopic.php?pid=1236890#p1236890
song_list_format = "{%a }{%t}|{%f}$R{%b }{%4l}
song_status_format = "{{%a{ \"%b\"{ (%y)}} - }{%t}}|{%f}"
song_library_format = "{%N - }{%t}|{%f}"
now_playing_prefix = "$b"
alternative_header_first_line_format = "$b$aqqu$/a {%t}|{%f} $atqq$/a$/b"
alternative_header_second_line_format = "$b$aqqu$/a {%b}|{%f} $atqq$/a$/b"
song_columns_list_format = "(4)[blue]{n:#} (25)[blue]{a} (40)[blue]{t|f} (30)[blue]{b} (7f)[blue]    {l}"
playlist_show_remaining_time = "yes"
playlist_separate_albums = "yes"
playlist_display_mode = "columns" (classic/columns)
browser_display_mode = "columns" (classic/columns)
search_engine_display_mode = "columns" (classic/columns)
progressbar_look = "✠❱•"
default_place_to_search_in = "database"
user_interface = "alternative"
header_visibility = "no"
titles_visibility = "yes"
show_hidden_files_in_local_browser = "no"
screen_switcher_mode = "browser, search_engine"
startup_screen = "search_engine"
ask_before_clearing_playlists =  "yes"
clock_display_seconds = "yes"
display_volume_level = "yes"
display_bitrate = "no"
display_remaining_time = "yes"
regular_expressions = "extended"
ignore_leading_the = "yes"
mouse_support = "no"
enable_window_title = "yes"
allow_for_physical_item_deletion = "no"
external_editor = "vim"
use_console_editor = "yes"
colors_enabled = "yes"
header_window_color = "default"
main_window_color = "white"
progressbar_color = "green"
alternative_ui_separator_color = "red"
active_column_color = "red"

或者也许这是可以让你得到你想要的东西。

#!/bin/bash
SESSION=ncmpcpp
#url=https://bbs.archlinux.org/viewtopic.php?pid=1167179#p1167179
tmux -2 has-session -t $SESSION
if [ $? -eq 0 ]; then
    tmux -2 attach -t $SESSION
    exit 0;
fi

tmux -2 new-session -d -s $SESSION 'ncmpcpp -s playlist'

for i in "C-l" "Enter" "2"
    do tmux -2 send-keys -t $SESSION:0.0 "$i"
done

tmux -2 set -t $SESSION -g status off

tmux -2 attach-session -t $SESSION