lscolors

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

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

Go to latest
Published: Mar 27, 2024 License: GPL-3.0 Imports: 5 Imported by: 0

README

ls-colors-go

ls-colors-go is a library for LS_COLORS environment variable.

Installation

$ go get -u github.com/kurusugawa-computer/ls-colors-go

Usage

This is a simple example to parse and print environment variable LS_COLORS.

// example/main.go
package main

import (
	"fmt"
	"os"

	lscolors "github.com/kurusugawa-computer/ls-colors-go"
)

func printStringPointer(s *string) string {
	if s == nil {
		return "undefined"
	} else {
		return *s
	}
}

func main() {
	lsColors := os.Getenv("LS_COLORS")
	result, err := lscolors.ParseLS_COLORS(lsColors, false)
	if err != nil {
		fmt.Printf("error: %s", err.Error())
		return
	}
	fmt.Printf("no: %s\n", printStringPointer(result.Normal))
	fmt.Printf("fi: %s\n", printStringPointer(result.FileDefault))
	fmt.Printf("di: %s\n", printStringPointer(result.Directory))
	fmt.Printf("ln: %s\n", printStringPointer(result.Symlink))
	fmt.Printf("pi: %s\n", printStringPointer(result.Pipe))
	fmt.Printf("so: %s\n", printStringPointer(result.Socket))
	fmt.Printf("bd: %s\n", printStringPointer(result.BlockDevice))
	fmt.Printf("cd: %s\n", printStringPointer(result.CharDevice))
	fmt.Printf("mi: %s\n", printStringPointer(result.MissingFile))
	fmt.Printf("or: %s\n", printStringPointer(result.OrphanedSymlink))
	fmt.Printf("ex: %s\n", printStringPointer(result.Executable))
	fmt.Printf("do: %s\n", printStringPointer(result.Door))
	fmt.Printf("su: %s\n", printStringPointer(result.SetUID))
	fmt.Printf("sg: %s\n", printStringPointer(result.SetGID))
	fmt.Printf("st: %s\n", printStringPointer(result.Sticky))
	fmt.Printf("ow: %s\n", printStringPointer(result.OtherWritable))
	fmt.Printf("tw: %s\n", printStringPointer(result.OtherWritableSticky))
	fmt.Printf("ca: %s\n", printStringPointer(result.Cap))
	fmt.Printf("mh: %s\n", printStringPointer(result.MultiHardLink))

	for _, ext := range result.Extensions {
		if ext.ExactMatch {
			fmt.Printf("*%s: %s (case sensitive)\n", ext.Extension, ext.Sequence)
		} else {
			fmt.Printf("*%s: %s\n", ext.Extension, ext.Sequence)
		}
	}
}
$ eval "$(dircolors -b)" ; go run ./example
no: undefined
fi: undefined
di: 01;34
ln: 01;36
pi: 40;33
so: 01;35
bd: 40;33;01
cd: 40;33;01
mi: 00
or: 40;31;01
ex: 01;32
do: 01;35
su: 37;41
sg: 30;43
st: 37;44
ow: 34;42
tw: 30;42
ca: 30;41
mh: 00
*.xspf: 00;36
*.spx: 00;36
*.opus: 00;36
*.oga: 00;36
# ...

Documentation

Index

Constants

This section is empty.

Variables

This section is empty.

Functions

This section is empty.

Types

type ErrorWithPosition

type ErrorWithPosition struct {
	// contains filtered or unexported fields
}

func (*ErrorWithPosition) Error

func (e *ErrorWithPosition) Error() string

func (*ErrorWithPosition) Position

func (e *ErrorWithPosition) Position() int

func (*ErrorWithPosition) Unwrap

func (e *ErrorWithPosition) Unwrap() error

type LSColors

type LSColors struct {
	LeftOfColorSequence  *string              // lc
	RightOfColorSequence *string              // rc
	EndColor             *string              // ec
	ResetOrdinaryColor   *string              // rs
	Normal               *string              // no
	FileDefault          *string              // fi
	Directory            *string              // di
	Symlink              *string              // ln
	Pipe                 *string              // pi
	Socket               *string              // so
	BlockDevice          *string              // bd
	CharDevice           *string              // cd
	MissingFile          *string              // mi
	OrphanedSymlink      *string              // or
	Executable           *string              // ex
	Door                 *string              // do
	SetUID               *string              // su
	SetGID               *string              // sg
	Sticky               *string              // st
	OtherWritable        *string              // ow
	OtherWritableSticky  *string              // tw
	Cap                  *string              // ca
	MultiHardLink        *string              // mh
	ClearToEndOfLine     *string              // cl
	Extensions           []LSColorsExtensions // *.xxx
	Unknowns             map[string][]string
}

func LSColorsDefault

func LSColorsDefault() LSColors

func ParseLS_COLORS

func ParseLS_COLORS(s string, allowUnknown bool) (*LSColors, error)

ParseLS_COLORS parse string as LS_COLORS environment variable. if allowUnknown is true, it accepts unknown indicator and add to .Unknowns field. if fails, it returns *ErrorWithPosition error. error position can be got by calling Position()

func (*LSColors) GetColorIndicator

func (c *LSColors) GetColorIndicator(fi os.FileInfo) *string

GetColorIndicator returns a color sequence corresponding FileInfo

type LSColorsExtensions

type LSColorsExtensions struct {
	Extension  string
	Sequence   string
	ExactMatch bool
}

Directories

Path Synopsis

Jump to

Keyboard shortcuts

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