util

package
v0.0.7 Latest Latest
Warning

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

Go to latest
Published: Aug 25, 2024 License: BSD-3-Clause Imports: 10 Imported by: 0

Documentation

Index

Examples

Constants

View Source
const (
	LOG_LEVEL_DEBUG = "DEBUG"
	LOG_LEVEL_INFO  = "INFO"
	LOG_LEVEL_WARN  = "WARN"
	LOG_LEVEL_ERROR = "ERROR"
)

Variables

This section is empty.

Functions

func CamelToKebab added in v0.0.5

func CamelToKebab(s string) string
Example
package main

import (
	"fmt"

	"github.com/OutOfBedlam/tine/util"
)

func main() {
	fmt.Println(util.CamelToKebab("CamelToSnake"))
}
Output:

camel-to-snake

func CamelToSep added in v0.0.5

func CamelToSep(s string, sep string) string

func CamelToSnake added in v0.0.5

func CamelToSnake(s string) string
Example
package main

import (
	"fmt"

	"github.com/OutOfBedlam/tine/util"
)

func main() {
	fmt.Println(util.CamelToSnake("CamelToSnake"))
}
Output:

camel_to_snake

func FormatCount added in v0.0.2

func FormatCount(count int, unit CountUnit) string
Example
package main

import (
	"fmt"

	"github.com/OutOfBedlam/tine/util"
)

func main() {
	fmt.Println(util.FormatCount(0, util.CountUnitLines))
	fmt.Println(util.FormatCount(1, util.CountUnitLines))
	fmt.Println(util.FormatCount(2, util.CountUnitLines))
}
Output:

0 lines
1 line
2 lines

func FormatFileSize

func FormatFileSize(bytes int64) string

ByteSize returns a human-readable byte size string.

Example
package main

import (
	"fmt"

	"github.com/OutOfBedlam/tine/util"
)

func main() {
	fmt.Println(util.FormatFileSizeInt(0))
	fmt.Println(util.FormatFileSize(1))
	fmt.Println(util.FormatFileSizeInt(1024))
	fmt.Println(util.FormatFileSize(1024 * 1024))
	fmt.Println(util.FormatFileSize(1024 * 1024 * 1024))
	fmt.Println(util.FormatFileSize(1024 * 1024 * 1024 * 1024))
	fmt.Println(util.FormatFileSize(1024 * 1024 * 1024 * 1024 * 1024))
}
Output:

0B
1B
1.00KB
1.00MB
1.00GB
1.00TB
1.00PB

func FormatFileSizeInt

func FormatFileSizeInt(bytes int) string

func NewLogger

func NewLogger(conf LogConfig) *slog.Logger
Example
// This example demonstrates how to create a new logger with different log levels.
// The logger is configured to write to a file and the log level is set to INFO.
conf := LogConfig{
	Path:       "-", // "-" writes to stdout, "" discard logging, otherwise writes to a file
	Level:      LOG_LEVEL_WARN,
	NoColor:    true,
	Timeformat: "no-time-for-test",
}
logger := NewLogger(conf)
logger.Debug("This is a debug message")
logger.Info("This is an info message")
logger.Warn("This is a warning message")
logger.Error("This is an error message")
Output:

no-time-for-test WRN This is a warning message
no-time-for-test ERR This is an error message

func NewLoggerWithWriter added in v0.0.5

func NewLoggerWithWriter(conf LogConfig, lw io.Writer) *slog.Logger

Types

type CountUnit added in v0.0.2

type CountUnit [2]string
var (
	CountUnitLines CountUnit = [2]string{"line", "lines"}
)

type JsonFloat added in v0.0.6

type JsonFloat struct {
	Value   float64
	Decimal int
}

func (JsonFloat) MarshalJSON added in v0.0.6

func (l JsonFloat) MarshalJSON() ([]byte, error)
Example
package main

import (
	"fmt"

	"github.com/OutOfBedlam/tine/util"
)

func main() {
	jf := util.JsonFloat{Value: 3.1415926535, Decimal: 2}
	b, err := jf.MarshalJSON()
	if err != nil {
		panic(err)
	}
	fmt.Println(string(b))
}
Output:

3.14

type LogConfig

type LogConfig struct {
	Path       string `toml:"path"`
	NoColor    bool   `toml:"no_color,omitempty"`
	AddSource  bool   `toml:"add_source,omitempty"`
	Timeformat string `toml:"timeformat,omitempty"`
	Level      string `toml:"level"`
	MaxSize    int    `toml:"max_size"`
	MaxAge     int    `toml:"max_age"`
	MaxBackups int    `toml:"max_backups"`
	Compress   bool   `toml:"compress,omitempty"`
	Chown      string `toml:"chown,omitempty"`
}

Jump to

Keyboard shortcuts

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