From 49547468910f360ba23ca6cb7da66e8ebecf9b7e Mon Sep 17 00:00:00 2001 From: qeesung <1245712564@qq.com> Date: Sun, 21 Oct 2018 20:53:38 +0800 Subject: [PATCH] Rewrite the go docs --- ascii/ascii.go | 5 +++-- ascii/option.go | 8 ++++---- convert/convert.go | 10 +++++----- convert/resize.go | 7 ++++--- 4 files changed, 16 insertions(+), 14 deletions(-) diff --git a/ascii/ascii.go b/ascii/ascii.go index 2b50411..85423b9 100644 --- a/ascii/ascii.go +++ b/ascii/ascii.go @@ -1,4 +1,4 @@ -// The ascii package can convert a image pixel to a raw char +// Package ascii can convert a image pixel to a raw char // base on it's RGBA value, in another word, input a image pixel // output a raw char ascii. package ascii @@ -10,7 +10,7 @@ import ( "reflect" ) -// Convert a pixel to a ASCII char string +// ConvertPixelToASCII converts a pixel to a ASCII char string func ConvertPixelToASCII(pixel color.Color, options *Options) string { defaultOptions := NewOptions() defaultOptions.mergeOptions(options) @@ -50,6 +50,7 @@ func intensity(r, g, b, a uint64) uint64 { return (r + g + b) * a / 255 } +// decorateWithColor decorate the raw char with the color base on r,g,b value func decorateWithColor(r, g, b uint64, rawChar byte) string { coloredChar := rgbterm.FgString(string([]byte{rawChar}), uint8(r), uint8(g), uint8(b)) return coloredChar diff --git a/ascii/option.go b/ascii/option.go index 71cf5da..3b6be87 100644 --- a/ascii/option.go +++ b/ascii/option.go @@ -1,25 +1,25 @@ package ascii -// Convert options +// Options convert pixel to raw char type Options struct { Pixels []byte Reverse bool Colored bool } -// Default options +// DefaultOptions that contains the default pixels var DefaultOptions = Options{ Pixels: []byte(" .,:;i1tfLCG08@"), Reverse: false, Colored: true, } -// Create a new options +// NewOptions create a new convert option func NewOptions() Options { return DefaultOptions } -// Merge options +// mergeOptions merge two options func (options *Options) mergeOptions(newOptions *Options) { options.Pixels = append([]byte{}, newOptions.Pixels...) options.Reverse = newOptions.Reverse diff --git a/convert/convert.go b/convert/convert.go index 03d067c..8a818a0 100644 --- a/convert/convert.go +++ b/convert/convert.go @@ -10,15 +10,16 @@ import ( "os" ) +// Options to convert the image to ASCII type Options struct { Ratio float64 ExpectedWidth int ExpectedHeight int FitScreen bool - Colored bool + Colored bool } -// Convert a image to ascii matrix +// Image2ASCIIMatrix converts a image to ASCII matrix func Image2ASCIIMatrix(image image.Image, imageConvertOptions *Options) []string { // Resize the convert first newImage := ScaleImage(image, imageConvertOptions) @@ -40,8 +41,7 @@ func Image2ASCIIMatrix(image image.Image, imageConvertOptions *Options) []string return rawCharValues } -// Convert a image to ascii matrix, then concat the matrix value -// to a long string for easy display +// Image2ASCIIString converts a image to ascii matrix, and the join the matrix to a string func Image2ASCIIString(image image.Image, options *Options) string { convertedPixelASCII := Image2ASCIIMatrix(image, options) var buffer bytes.Buffer @@ -52,7 +52,7 @@ func Image2ASCIIString(image image.Image, options *Options) string { return buffer.String() } -// Convert a image file to ascii string +// ImageFile2ASCIIString converts a image file to ascii string func ImageFile2ASCIIString(imageFilename string, option *Options) string { f, err := os.Open(imageFilename) if err != nil { diff --git a/convert/resize.go b/convert/resize.go index 2f5b40e..d9e8135 100644 --- a/convert/resize.go +++ b/convert/resize.go @@ -10,7 +10,7 @@ import ( "runtime" ) -// Resize the convert to expected size base on the ratio +// ScaleImage resize the convert to expected size base on the convert options func ScaleImage(image image.Image, options *Options) (newImage image.Image) { sz := image.Bounds() ratio := options.Ratio @@ -46,7 +46,7 @@ func ScaleImage(image image.Image, options *Options) (newImage image.Image) { return } -// Get the terminal char width on different system +// charWidth get the terminal char width on different system func charWidth() float64 { if isWindows() { return 0.714 @@ -54,11 +54,12 @@ func charWidth() float64 { return 0.5 } -// Check if current system is windows +// isWindows check if current system is windows func isWindows() bool { return runtime.GOOS == "windows" } +// getFitScreenSize get the current terminal screen size func getFitScreenSize() (newWidth, newHeight uint) { if !isatty.IsTerminal(os.Stdout.Fd()) && !isatty.IsCygwinTerminal(os.Stdout.Fd()) { log.Fatal("Can not detect the terminal, please disable the '-s fitScreen' option") -- 2.45.2