Coder Social home page Coder Social logo

Comments (8)

axgle avatar axgle commented on May 21, 2024 1

work on winxp,and ssh on ubuntu.

package main

import (
    "github.com/nsf/termbox-go"
)

func rune_width(r rune) int {
    if r >= 0x1100 &&
        (r <= 0x115f || r == 0x2329 || r == 0x232a ||
            (r >= 0x2e80 && r <= 0xa4cf && r != 0x303f) ||
            (r >= 0xac00 && r <= 0xd7a3) ||
            (r >= 0xf900 && r <= 0xfaff) ||
            (r >= 0xfe30 && r <= 0xfe6f) ||
            (r >= 0xff00 && r <= 0xff60) ||
            (r >= 0xffe0 && r <= 0xffe6) ||
            (r >= 0x20000 && r <= 0x2fffd) ||
            (r >= 0x30000 && r <= 0x3fffd)) {
        return 2
    }
    return 1
}
func main() {
    err := termbox.Init()
    if err != nil {
        panic(err)
    }
    defer termbox.Close()

    //cn := []rune{'你', '好', ',', '世', '界', '!'}
    cn := "你好, 世界!"
    en := []rune{'a', 'b', 'c', 'd', 'e', 'f'}
    x := 0

    for _, v := range cn {
        termbox.SetCell(x, 0, v, termbox.ColorBlack, termbox.ColorWhite)
        x += rune_width(v)
    }
    for i := range en {
        termbox.SetCell(i, 1, en[i], termbox.ColorRed, termbox.ColorWhite)
    }
    termbox.Flush()
    termbox.PollEvent()
}

from termbox-go.

nsf avatar nsf commented on May 21, 2024

The problem is that with termbox it has no control over the terminal which in turn draws runes. It's possible to make a termbox implementation which will be drawing runes by itself, but it was never done so far, at the moment I have only a terminal-based backend. So, termbox simply assumes that each rune takes exactly one terminal cell and I know this assumption is false for some CJK rune cases. But I will need to read more about it, when and why terminals draw double-width runes. Will think what can I do about this.

from termbox-go.

nsf avatar nsf commented on May 21, 2024

Here's what I think. Even though it's possible to hack termbox so that it understands that CJK runes take 2 termbox cells, this approach isn't portable (on windows probably it doesn't work like that) and every termbox-based application should be aware of that fact too and handle the stuff in a special manner. So, I don't think I will do anything about it. If you want CJK support, hack termbox to suit your needs (it's a very simple library after all).

Honestly I just don't know how to make it work using current termbox abstractions. Termbox was designed around a notion of "cells" and this thing breaks it really hard. Perhaps there are terminals which render CJK runes with width == 1, but not sure how readable is that.

from termbox-go.

songgao avatar songgao commented on May 21, 2024

Thanks for getting back to it. Sure I'll try hacking it and send a pull request or something if I work out.

I don't think terminals that render CJK with width == 1 would be readable at all...

from termbox-go.

nsf avatar nsf commented on May 21, 2024

See also: #21

Should work now on linux/darwin. You can install https://github.com/nsf/godit to test it out.

from termbox-go.

songgao avatar songgao commented on May 21, 2024

Looks like it's working now. Just tested on OS X with iTerm2. Thanks!

from termbox-go.

wang0z avatar wang0z commented on May 21, 2024

I have to say the real output sequence is somthing like '\033[1;1H你\033[1;3H好\033[1;5H世\033[1;7H界' which inlcudes unnecessary CSI code. Anyway it is better than nothing.

from termbox-go.

shia86 avatar shia86 commented on May 21, 2024

I recommend to use go-runewidth

from termbox-go.

Related Issues (20)

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    🖖 Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. 📊📈🎉

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google ❤️ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.