"refactor(common): 重构序列化工具包,将serialize重命名为utils并添加bitset组件"
This commit is contained in:
43
common/utils/log/term/env.go
Normal file
43
common/utils/log/term/env.go
Normal file
@@ -0,0 +1,43 @@
|
||||
// Copyright 2013 Apcera Inc. All rights reserved.
|
||||
|
||||
package term
|
||||
|
||||
import (
|
||||
"os"
|
||||
"strconv"
|
||||
)
|
||||
|
||||
// GetEnvWindowSize returns the window Size, as determined by process
|
||||
// environment; if either LINES or COLUMNS is present, and whichever is
|
||||
// present is also numeric, the Size will be non-nil. If Size is nil,
|
||||
// there's insufficient data in environ. If one entry is 0, that means
|
||||
// that the environment does not include that data. If a value is
|
||||
// negative, we treat that as an error.
|
||||
func GetEnvWindowSize() *Size {
|
||||
lines := os.Getenv("LINES")
|
||||
columns := os.Getenv("COLUMNS")
|
||||
if lines == "" && columns == "" {
|
||||
return nil
|
||||
}
|
||||
|
||||
nLines := 0
|
||||
nColumns := 0
|
||||
var err error
|
||||
if lines != "" {
|
||||
nLines, err = strconv.Atoi(lines)
|
||||
if err != nil || nLines < 0 {
|
||||
return nil
|
||||
}
|
||||
}
|
||||
if columns != "" {
|
||||
nColumns, err = strconv.Atoi(columns)
|
||||
if err != nil || nColumns < 0 {
|
||||
return nil
|
||||
}
|
||||
}
|
||||
|
||||
return &Size{
|
||||
Lines: nLines,
|
||||
Columns: nColumns,
|
||||
}
|
||||
}
|
||||
54
common/utils/log/term/getsize.go
Normal file
54
common/utils/log/term/getsize.go
Normal file
@@ -0,0 +1,54 @@
|
||||
// Copyright 2013 Apcera Inc. All rights reserved.
|
||||
|
||||
package term
|
||||
|
||||
import (
|
||||
"os"
|
||||
)
|
||||
|
||||
// Size is the size of a terminal, expressed in character cells, as Lines and
|
||||
// Columns. This might come from environment variables or OS-dependent
|
||||
// resources.
|
||||
type Size struct {
|
||||
Lines int
|
||||
Columns int
|
||||
}
|
||||
|
||||
// GetSize will return the terminal window size.
|
||||
//
|
||||
// We prefer environ $LINES/$COLUMNS, then fall back to tty-held information.
|
||||
// We do not support use of termcap/terminfo to derive default size information.
|
||||
func GetSize() (*Size, error) {
|
||||
envSize := GetEnvWindowSize()
|
||||
if envSize != nil && envSize.Lines != 0 && envSize.Columns != 0 {
|
||||
return envSize, nil
|
||||
}
|
||||
|
||||
fh, err := os.Open("/dev/tty")
|
||||
if err != nil {
|
||||
// no tty, no point continuing; we only let the environ
|
||||
// avoid an error in this case because if someone has faked
|
||||
// up an environ with LINES/COLUMNS _both_ set, we should let
|
||||
// them
|
||||
return nil, err
|
||||
}
|
||||
|
||||
size, err := GetTerminalWindowSize(fh)
|
||||
if err != nil {
|
||||
if envSize != nil {
|
||||
return envSize, nil
|
||||
}
|
||||
return nil, err
|
||||
}
|
||||
if envSize == nil {
|
||||
return size, err
|
||||
}
|
||||
|
||||
if envSize.Lines == 0 {
|
||||
envSize.Lines = size.Lines
|
||||
}
|
||||
if envSize.Columns == 0 {
|
||||
envSize.Columns = size.Columns
|
||||
}
|
||||
return envSize, nil
|
||||
}
|
||||
35
common/utils/log/term/sizes_unix.go
Normal file
35
common/utils/log/term/sizes_unix.go
Normal file
@@ -0,0 +1,35 @@
|
||||
// Copyright 2013 Apcera Inc. All rights reserved.
|
||||
|
||||
// +build !windows
|
||||
|
||||
package term
|
||||
|
||||
import (
|
||||
"errors"
|
||||
"os"
|
||||
"syscall"
|
||||
"unsafe"
|
||||
)
|
||||
|
||||
// ErrGetWinsizeFailed indicates that the system call to extract the size of
|
||||
// a Unix tty from the kernel failed.
|
||||
var ErrGetWinsizeFailed = errors.New("term: syscall.TIOCGWINSZ failed")
|
||||
|
||||
// GetTerminalWindowSize returns the terminal size maintained by the kernel
|
||||
// for a Unix TTY, passed in as an *os.File. This information can be seen
|
||||
// with the stty(1) command, and changes in size (eg, terminal emulator
|
||||
// resized) should trigger a SIGWINCH signal delivery to the foreground process
|
||||
// group at the time of the change, so a long-running process might reasonably
|
||||
// watch for SIGWINCH and arrange to re-fetch the size when that happens.
|
||||
func GetTerminalWindowSize(file *os.File) (*Size, error) {
|
||||
// Based on source from from golang.org/x/crypto/ssh/terminal/util.go
|
||||
var dimensions [4]uint16
|
||||
if _, _, err := syscall.Syscall6(syscall.SYS_IOCTL, file.Fd(), uintptr(syscall.TIOCGWINSZ), uintptr(unsafe.Pointer(&dimensions)), 0, 0, 0); err != 0 {
|
||||
return nil, err
|
||||
}
|
||||
|
||||
return &Size{
|
||||
Lines: int(dimensions[0]),
|
||||
Columns: int(dimensions[1]),
|
||||
}, nil
|
||||
}
|
||||
57
common/utils/log/term/sizes_windows.go
Normal file
57
common/utils/log/term/sizes_windows.go
Normal file
@@ -0,0 +1,57 @@
|
||||
// Copyright 2013 Apcera Inc. All rights reserved.
|
||||
|
||||
// +build windows
|
||||
|
||||
package term
|
||||
|
||||
// Used when we have no other source for getting platform-specific information
|
||||
// about the terminal sizes available.
|
||||
|
||||
import (
|
||||
"os"
|
||||
"syscall"
|
||||
"unsafe"
|
||||
)
|
||||
|
||||
// Based on source from from golang.org/x/crypto/ssh/terminal/util_windows.go
|
||||
var (
|
||||
kernel32 = syscall.NewLazyDLL("kernel32.dll")
|
||||
|
||||
procGetConsoleScreenBufferInfo = kernel32.NewProc("GetConsoleScreenBufferInfo")
|
||||
)
|
||||
|
||||
type (
|
||||
short int16
|
||||
word uint16
|
||||
|
||||
coord struct {
|
||||
x short
|
||||
y short
|
||||
}
|
||||
smallRect struct {
|
||||
left short
|
||||
top short
|
||||
right short
|
||||
bottom short
|
||||
}
|
||||
consoleScreenBufferInfo struct {
|
||||
size coord
|
||||
cursorPosition coord
|
||||
attributes word
|
||||
window smallRect
|
||||
maximumWindowSize coord
|
||||
}
|
||||
)
|
||||
|
||||
// GetTerminalWindowSize returns the width and height of a terminal in Windows.
|
||||
func GetTerminalWindowSize(file *os.File) (*Size, error) {
|
||||
var info consoleScreenBufferInfo
|
||||
_, _, e := syscall.Syscall(procGetConsoleScreenBufferInfo.Addr(), 2, file.Fd(), uintptr(unsafe.Pointer(&info)), 0)
|
||||
if e != 0 {
|
||||
return nil, error(e)
|
||||
}
|
||||
return &Size{
|
||||
Lines: int(info.size.y),
|
||||
Columns: int(info.size.x),
|
||||
}, nil
|
||||
}
|
||||
23
common/utils/log/term/wrapper.go
Normal file
23
common/utils/log/term/wrapper.go
Normal file
@@ -0,0 +1,23 @@
|
||||
// Copyright 2013 Apcera Inc. All rights reserved.
|
||||
|
||||
//go:build ignore
|
||||
// +build ignore
|
||||
|
||||
package main
|
||||
|
||||
import (
|
||||
"fmt"
|
||||
"os"
|
||||
|
||||
"github.com/apcera/termtables/term"
|
||||
)
|
||||
|
||||
func main() {
|
||||
size, err := term.GetSize()
|
||||
if err != nil {
|
||||
fmt.Fprintf(os.Stderr, "failed: %s\n", err)
|
||||
os.Exit(1)
|
||||
}
|
||||
|
||||
fmt.Printf("Lines %d Columns %d\n", size.Lines, size.Columns)
|
||||
}
|
||||
Reference in New Issue
Block a user