如何在终端上滚动消息? [英] How do I scroll a message across the terminal?

查看:78
本文介绍了如何在终端上滚动消息?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在尝试编写一个程序,以作为使用curses.h library创建侧向滚动显示的字幕.

I'm trying to write a program to that acts as a marquee that uses the curses.h library to create a side-scrolling display.

应该发生的是,我的消息"Hello"应该似乎从终端的右侧逐个字符滚动到左侧.

What should happen is that my message "Hello" should appear to scroll from the right side of the terminal to the left, character by character.

"hello"应该看起来像这样在终端上滚动:

"hello" should appear to scroll across the terminal like so:

|                                              H| // fist frame of animation

|                                             He| //2nd

|                                            Hel| //3rd
                                                  ...
|             Hello                             | // some time in the middle of animation

|Hello                                          | // finished.

我的程序没有出现在终端上滚动,而是简单地在终端的左侧输出"Hello"消息,就好像它完成了一样.

Instead of appearing to scroll across the terminal my program simply outputs the "Hello" message on the left side of the terminal as if it is finished.

我认为在每帧中先打印适当数量的空格,然后再打印适当数量的字符串字符.

I thought that printing the appropriate number of spaces then the appropriate number of characters of the string each frame would work.

我在做什么错了?

到目前为止,以下是我的代码:

Below is my code so far:

#include    <curses.h>
#include    <string.h> 
main()
{
    char    message[] = "Hello";
    int     max_y, max_x; // max dimensions of terminal window
    int     text_length;
    int     i,row=0,col=0,spaces=0;

    // Get text length
    text_length = strlen(message);

    // Get terminal dimensions
    getmaxyx(stdscr, max_y, max_x);

    // num of spaces needed to print
    spaces = max_x -1; 

    initscr(); // initialize curses
    clear(); // clear screen to begin

    while(1)
    {
        clear(); // clear last drawn iteration
        move(5,col);
        // print spaces as necessary
        for(i=0;i<spaces;i++)
        {
            addch(' ');
        }
        refresh();
        // print appropriate number of characters of the message            
        for(i=0;i<text_length || i<max_x; i++)
        {
            addch(message[i]);
        }
        refresh();          
        usleep(50000); // wait some time
        spaces = spaces-1; //adjust spaces need for next iteration
    }
}

推荐答案

第一个问题是您在initscr()之前调用getmaxyx().在这种情况下,stdscr尚未初始化,因此getmaxyx()返回的值没有意义. (对于每个值,我得到-1,又称ERR.)

The first problem is that you call getmaxyx() before initscr(). In this situation, stdscr has not been initialized, so the values returned by getmaxyx() are meaningless. (I get -1 for each value, aka ERR.)

该程序已修复,该程序基本上可以运行,但是在"Hello"字符串之后显示了垃圾.您可以通过将for循环测试text_length || i<max_x更改为text_length && i<max_x来解决该问题,尽管结果可能仍然不是您想要的.但我会留给您找出答案.

That fixed, the program basically works, but prints junk after the "Hello" string. You can solve that by changing the for loop test, text_length || i<max_x, to text_length && i<max_x, although the result is still probably not quite what you want. But I'll leave it to you to figure that one out.

最后,作为样式问题,我建议使用curses自己的napms()函数而不是usleep()(即,用napms(50)代替usleep(50000)).但是,如果您坚持使用usleep(),则应在顶部添加#include <unistd.h>.

Finally, as a stylistic matter, I'd suggest using curses' own napms() function instead of usleep() (i.e., napms(50) instead of usleep(50000)). But if you do stick with usleep(), you should add #include <unistd.h> at the top.

这篇关于如何在终端上滚动消息?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆