mwpad

package module
v0.0.0-...-a8d912c Latest Latest
Warning

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

Go to latest
Published: Sep 29, 2024 License: BSD-3-Clause Imports: 2 Imported by: 0

README

github.com/WangNemo/go-mwpad

🚀 1. go-mwpad 是什么?

解决golang中对于包含中文字符场景下打印对齐padding的宽度问题 即:multiple-width padding

🧐 2.为什么用 go-mwpad?

中文字符打印场景下可能会遇到下面的问题:

  • 门店名称: Apple 王府井                |取货状态:不可取货

  • 门店名称: Apple 三里屯                |取货状态:不可取货

  • 门店名称: Apple 西单大悦城              |取货状态:不可取货

  • 门店名称: Apple 天津恒隆广场             |取货状态:不可取货

这些"|"为什么对不齐呢?……明明用Printf("%-20s",xxx)这样了

实际上因为go unicode中的【字符宽度】和【字符长度】是不同的概念,汉字字符的宽度和East Asian规定有关

go官方package里刚好没有,所以简单封装下函数

🛠️ 3.如何使用?

1. 获取package

go get github.com/WangNemo/go-mwpad

2. 使用

package main

import (
	"fmt"
	mwpad "github.com/WangNemo/go-mwpad"
)

func main() {
	s :="门店名称: Apple 王府井"
	ps, err := mwpad.StrPad(s, 20, nil)
	if err!= nil {
	    fmt.Printf("pad error %s", err.Error())
	}
	fmt.Printf("%s\n", ps)
}

3.支持模式:

  • 从左补齐
  • 从右补齐
  • 从双边补齐(左侧优先)
  • 从双边补齐(右侧优先)

Documentation

Index

Constants

View Source
const (
	STR_PAD_LEFT = iota
	STR_PAD_RIGHT
	STR_PAD_BOTH_LEFT_FIRST
	STR_PAD_BOTH_RIGHT_FIRST
)

Variables

This section is empty.

Functions

func StrPad

func StrPad(s string, width int, opt *Option) (string, error)

func WidthByEA

func WidthByEA(s string) (w int)

Types

type Option

type Option struct {
	PadRune rune
	PadType int
}

Jump to

Keyboard shortcuts

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