textwidth

package module
v0.0.0-...-397b9d3 Latest Latest
Warning

This package is not in the latest version of its module.

Go to latest
Published: Feb 24, 2023 License: MIT Imports: 2 Imported by: 1

README

go-textwidth

Go library to determine the required columns and rows for a Unicode string to display in a terminal

Features

The key feature of this library is that it supports East Asian languages, where some characters may occupy 2 columns on a terminal.

Usage

package main

import (
    "fmt"
    textwidth "github.com/m13253/go-textwidth"
)

func main() {
    const s = "Go(又称 Golang)是 Google 开发的一种静态强类型、编译型、并发"+
        "型,并具有垃圾回收功能的编程语言。Go 的主要特色在于易于使用的并行设"+
        "计,叫做 Goroutine,透过 Goroutine 能够让程序以异步的方式运行,而不需"+
        "要担心一个函数导致程序中断,因此 Go 也非常地适合网络服务。\t————维基"+
        "百科 Wikipedia"
    row, col := textwidth.GetTextOffset(s, 0, 80)
    total := textwidth.GetTextWidth(s, 0, 80)

    fmt.Printf("rows = %d, columns = %d, total = %d", row, col, total)
}

The paragraph takes 3 lines and 46 more columns. 3×80+44 = 286.

<--------10--------20--------30--------40--------50--------60--------70-------->
Go(又称 Golang)是 Google 开发的一种静态强类型、编译型、并发型,并具有垃圾回收
功能的编程语言。Go 的主要特色在于易于使用的并行设计,叫做 Goroutine,透过 Gorout
ine 能够让程序以异步的方式运行,而不需要担心一个函数导致程序中断,因此 Go 也非常
地适合网络服务。        ————维基百科 Wikipedia

Therefore, the result is:

rows = 3, columns = 44, total = 284

Bugs

This library does not support Middle East languages requiring special BiDi shaping algorithms. Maybe the library will work out-of-the box, but maybe it will not work.

Contributions are welcome.

License

go-textwidth is released under MIT license. Please see LICENSE for more imformation.

Documentation

Index

Constants

This section is empty.

Variables

This section is empty.

Functions

func GetTextOffset

func GetTextOffset(s string, startColumn, wrapColumn int) (rows, endColumn int)

GetTextOffset calculates how many rows and columns a string takes on a terminal.

`startColumn` specifies the column that the string starts to print out. `wrapColumn` specified the number of columns that the terminal has.

The algorithm takes care of East Asian characters which takes 2 columns to display, and inserts a single space at the end of each row when necessary.

To disable text wrapping, specify `wrapColumn = 0`. In this mode, “\v` and `\f` behave as if the line is infinity in length.

However, this function does not support Middle East languages requiring special BiDi shaping algorithms.

func GetTextWidth

func GetTextWidth(s string, startColumn, wrapColumn int) int

GetTextWidth calculates how many columns a string takes on a terminal.

`startColumn` specifies the column that the string starts to print out. `wrapColumn` specified the number of columns that the terminal has.

The algorithm takes care of East Asian characters which takes 2 columns to display, and inserts a single space at the end of each row when necessary.

To disable text wrapping, specify `wrapColumn = 0`. In this mode, `\n`, `\v`, and `\f` will behave as a normal space.

However, this function does not support Middle East languages requiring special BiDi shaping algorithms.

Types

This section is empty.

Jump to

Keyboard shortcuts

? : This menu
/ : Search site
f or F : Jump to
y or Y : Canonical URL