我正在编写一个 perl 脚本,使用 Curses 库在终端中显示动画,但当终端大小改变时,我无法让它重新动画。目标是当终端大小调整时,动画从终端中间重新启动。
这是我尝试过的代码,但即使终端尺寸发生变化,它也永远不会重新绘制实体:
#!/usr/bin/env perl
use Term::Animation;
use Term::Animation::Entity;
use Curses;
# Declaration of $anim1 and addition of entity
my ($width, $height, $assumed_size) = $anim1->screen_size();
while (1) {
# Animate the current frame
$anim1->animate();
# Check for terminal resize and update if necessary
my ($new_width, $new_height) = $anim1->screen_size();
if ($new_width != $width || $new_height != $height) {
# Update terminal size variables
$width = $new_width;
$height = $new_height;
# Update entity position based on new terminal size
my $entity->{position} = [$height / 2 - 7, $width / 2 - 25, 0];
# Redraw all entities
$anim1->update_term_size();
}
# Handle user input
my $input = getch();
if ($input && $input eq 'c') {
last; # Exit the loop if 'c' is pressed
}
}
在终端 curses 库中,调用来改变终端大小的函数是 resize_term() 或 resizeterm()。
该函数在Term::Animation使用的Perl Curses 模块中实现,但该函数从未在 Term::Animation 的代码中使用过。
实际上作者把它放在了模块代码中:
换句话说:这个模块的作者似乎从未实现过调整大小的功能。