使用 Curses 开发套件 (Cdk) 时打印原始数据

发布于 2024-09-15 09:43:36 字数 988 浏览 6 评论 0原文

我的 Perl 程序需要在 ncurses 和原始控制台之间跳转,因为它在循环中执行系统调用,如下所示(为了强调问题而简化):

  1. init Cdk
  2. 显示菜单
  3. deinit Cdk
  4. 运行系统调用

不幸的是,Cdk 似乎在处初始化 ncurses >使用Cdk导入而不是实例化 (Cdk::init()),所以我不知道如何禁用它。这种行为在我看来是完全荒谬的;除了上面描述的循环之外,我的程序还有一个特殊的模式,其中数据库被初始化 - 这部分不提供任何类型的菜单(仅原始控制台输出),但似乎我现在必须用菜单格式化所有输出- 甚至不使用菜单!再说一次,现在只是为了打印程序帮助输出似乎需要我生成一个 Cdk 小部件 - 太疯狂了。

使用 Cdk 时是否有办法轻松跳入和跳出 ncurses 还是我被搞砸了?

一些示例代码:

#!/usr/bin/perl

use Cdk;

eval {popupLabel(["popup before init"]);}; # will fail as init() isn't called
print "Test after use\n";                  # won't be printed (which is a problem!)
Cdk::init();
print "Test after init\n";                 # won't be printed
popupLabel(["popup after init"]);
Cdk::end();
print "Test after end\n";                  # won't be printed

My perl program needs to jump between ncurses and a raw console as it executes a system call in a loop, like this (simplified for problem emphasis):

  1. init Cdk
  2. present menu
  3. deinit Cdk
  4. run system call

Unfortunately Cdk appears to initialize ncurses at the use Cdk import and not instantiation
(Cdk::init()) and so I don't know how to disable it. This behavior seems completely absurd to me; apart from the loop described above, my program also has a special mode where a database is initialized - this part does not present any kind of menu (only raw console output) yet it would seem that I now have to format all my output with menus - whilst not even using a menu! And again, just to print the program help output now seems to require me to generate a Cdk widget - insane.

Is there a way to easily jump in and out of ncurses when using Cdk or am I screwed?

Some example code:

#!/usr/bin/perl

use Cdk;

eval {popupLabel(["popup before init"]);}; # will fail as init() isn't called
print "Test after use\n";                  # won't be printed (which is a problem!)
Cdk::init();
print "Test after init\n";                 # won't be printed
popupLabel(["popup after init"]);
Cdk::end();
print "Test after end\n";                  # won't be printed

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(2

不奢求什么 2024-09-22 09:43:36

以下内容(使用较新的 Curses 模块)似乎可以满足您的要求: Curses 和非 Curses 的部分混合在一起:

use strict;
use warnings;
use Curses;

# Non-Curses
print "Press a key...\n";
<STDIN>;

# Curses
initscr();
addstr(14, 0, "hit a key: ");
refresh();
my $ch = getch();
endwin();

# Non-Curses
print "Hi again, press a key...\n";
<STDIN>;

# Curses again
initscr();
addstr(14, 0, "hit another key: ");
refresh();
my $ch2 = getch();
endwin();

# Non-Curses
print "bye!\n";

至少这个模块是今年 1 月发布的,而不是上个世纪(如 1998 年发布的 Cdk)......

The following (which uses the newer Curses module) seems to do what you want: bits of Curses, and bits of non-Curses intermingled:

use strict;
use warnings;
use Curses;

# Non-Curses
print "Press a key...\n";
<STDIN>;

# Curses
initscr();
addstr(14, 0, "hit a key: ");
refresh();
my $ch = getch();
endwin();

# Non-Curses
print "Hi again, press a key...\n";
<STDIN>;

# Curses again
initscr();
addstr(14, 0, "hit another key: ");
refresh();
my $ch2 = getch();
endwin();

# Non-Curses
print "bye!\n";

At least this module was released this January and not (like Cdk, released 1998) last century...

小苏打饼 2024-09-22 09:43:36

好吧,我得到了我想要的,为 Curses::UI 废弃 Cdk。感谢 mfontani 发布答案!

这是我的示例解决方案,它使用 Curses::UICurses::UI::Listbox

#!/usr/bin/perl

use Curses::UI;
use Curses::UI::Listbox;

my $cui = Curses::UI->new(-color_support => 1,
              -clear_on_exit => 0);

my $win = $cui->add('window_id', 'Window');

sub user_select {
    my ($cui, $obj) = @_;

    my $item = $obj->get_active_value();

    $cui->leave_curses();

    print "You selected $item!\n";
    sleep(1);
}

my $listbox = $win->add(
            'mylistbox', 'Listbox',
            -onchange    => sub{ user_select($cui, @_) },
            -border      => 1,
            -values      => [1, 2, 3],
            -labels      => { 1 => 'One', 
                      2 => 'Two', 
                      3 => 'Three' },
    );
$listbox->focus();
$cui->mainloop;

Well, I got what I wanted, scrapping Cdk for Curses::UI. Thanks mfontani for posting an answer!

Here's my example solution which uses Curses::UI and Curses::UI::Listbox:

#!/usr/bin/perl

use Curses::UI;
use Curses::UI::Listbox;

my $cui = Curses::UI->new(-color_support => 1,
              -clear_on_exit => 0);

my $win = $cui->add('window_id', 'Window');

sub user_select {
    my ($cui, $obj) = @_;

    my $item = $obj->get_active_value();

    $cui->leave_curses();

    print "You selected $item!\n";
    sleep(1);
}

my $listbox = $win->add(
            'mylistbox', 'Listbox',
            -onchange    => sub{ user_select($cui, @_) },
            -border      => 1,
            -values      => [1, 2, 3],
            -labels      => { 1 => 'One', 
                      2 => 'Two', 
                      3 => 'Three' },
    );
$listbox->focus();
$cui->mainloop;
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文