vincentaxhe has asked for the wisdom of the Perl Monks concerning the following question:

I use Term::ReadLine, but It handle wide character(chinese) poorly, though the input is recognized correctly, is there a better module be able to deal wide character input, if just like type in a normal console

state $term = Term::ReadLine->new('note'); my $prompt = "LINE:"; chomp($note = $term->readline($prompt)); $note = decode 'UTF-8', $note; $term->addhistory($note) if $note =~ /\S/;

Replies are listed 'Best First'.
Re: Term::ReadLine handle wide character poorly
by The_Dj (Scribe) on May 14, 2024 at 03:33 UTC
    Hi

    I tested your code above without the decode and it worked perfectly* as I expected.

    By default it was using Term::ReadLine::Gnu but I also tried using Term::ReadLine::Perl5 and it also worked exactly the same

    * I don't have a chinese keyboard, but I was copying 弛 and 地 from wikipedia

    What exactly is it doing wrong for you?

      It swallow wide character to squeeze in a non-wide character length, it start to do that after input eight or nine wide characters, it keeps doing that, then wrap line upward, very clumsy line input environment

      Can you paste a string here I could test with?

        Its behavior varies depending you paste repeatly and type in, paste will show '[27m', type in will squeeze character, neither is desirable. try paste '一生'.

        I use fcitx in arch, you will need 'fcitx5 fcitx5-chinese-addons fcitx5-configtool fcitx5-gtk-git fcitx5-pinyin-zhwiki fcitx5-qt', relogin and some environment settings. here is my hyprland env settings related to fcitx. make it work takes some efforts.

        env = XMODIFIERS, @im=fcitx # env = GTK_IM_MODULE, fcitx env = QT_IM_MODULE, fcitx env = SDL_IM_MODULE, fcitx env = ECORE_IMF_MODULE,xim
        sorry, its alacritty I use, kitty and foot works fine.
Re: Term::ReadLine handle wide character poorly
by The_Dj (Scribe) on May 15, 2024 at 03:38 UTC
    I see an issue in your sample code;
    You don't need $term->addhistory($note) if $note =~ /\S/; - The readline method already adds to the history.
    But using addhistory you are doubling everything in your history.

      indeed, I preload some history by     $term->addhistory($_) foreach split "\n", $tasks[$selectedtask]{note} // ""; I assume its all manually, and perldoc also give example like this. thx
Re: Term::ReadLine handle wide character poorly
by ForgotPasswordAgain (Vicar) on May 14, 2024 at 16:24 UTC
    I haven't tried it, but from what I read you might be able to do: binmode($term->IN, ':utf8');
      sorry, its alacritty I use, kitty and foot works fine.

        That does rather point the finger at alacritty then. Are you on the latest version? If so, maybe raise the issue with them?


        🦛