info

package
v1.0.1 Latest Latest
Warning

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

Go to latest
Published: Jan 10, 2024 License: LGPL-3.0 Imports: 31 Imported by: 0

Documentation

Overview

Package info that extracts and returns file statistics and metadata.

Index

Examples

Constants

This section is empty.

Variables

View Source
var (
	ErrFmt  = errors.New("format is not known")
	ErrName = errors.New("name value cannot be empty")
)

Functions

func Info

func Info(w io.Writer, name, format string, chksums bool) error

Info parses the named file and writes the details in a formal syntax.

Example
package main

import (
	"fmt"
	"strings"

	"github.com/bengarrett/retrotxtgo/info"
)

func main() {
	s := strings.Builder{}
	_ = info.Info(&s, "testdata/example.txt", "text", false)
	x := strings.Split(s.String(), "\n")
	for _, v := range x {
		if strings.Contains(v, "SHA256 checksum") {
			fmt.Print(v)
		}
	}
}
Output:

SHA256 checksum  4b187b0e6bc12541659eed5845d9dbe0914d4fc026f849bd03c255775a97d878

func Marshal

func Marshal(w io.Writer, name string, chksums bool, f Format) error

Marshal and write the metadata and system details of a named file.

Example
package main

import (
	"encoding/json"
	"fmt"
	"strings"

	"github.com/bengarrett/retrotxtgo/info"
)

func main() {
	s := strings.Builder{}
	_ = info.Marshal(&s, "testdata/example.txt", true, info.JSON)
	fmt.Printf("%d bytes and json? %t", len(s.String()), json.Valid([]byte(s.String())))
}
Output:

2345 bytes and json? true

func Stream

func Stream(w io.Writer, format string, b ...byte) error

Stream parses piped data and writes out the details in a specific syntax.

Example
package main

import (
	"fmt"
	"os"
	"strings"

	"github.com/bengarrett/retrotxtgo/info"
)

func main() {
	s := strings.Builder{}
	file, _ := os.Open("testdata/example.txt")

	b := make([]byte, 25)
	i, _ := file.Read(b)
	_ = info.Stream(&s, "text", b...)

	stdin := strings.Contains(s.String(), "n/a (stdin)")
	fmt.Printf("stdin? %t, %d bytes", stdin, i)
}
Output:

stdin? true, 25 bytes

func ValidText

func ValidText(mime string) bool

ValidText reports whether the MIME content-type value is valid for text files.

Types

type Checksums

type Checksums struct {
	CRC32  string `json:"crc32"  xml:"crc32"`  // CRC32 is a cyclic redundancy check of the file.
	CRC64  string `json:"crc64"  xml:"crc64"`  // CRC64 is a cyclic redundancy check of the file.
	MD5    string `json:"md5"    xml:"md5"`    // MD5 is a weak cryptographic hash function.
	SHA256 string `json:"sha256" xml:"sha256"` // SHA256 is a strong cryptographic hash function.
}

Checksums act as a fingerprint of the file for uniqueness and data corruption checks.

type Content

type Content struct {
	Type  string `json:"-"        xml:"-"`
	Media string `json:"media"    xml:"media"`     // Media is the MIME media type.
	Sub   string `json:"subMedia" xml:"sub_media"` // Sub is the MIME sub type.
	Commt string `json:"comment"  xml:"comment"`   // Commt is the MIME comment.
}

Content metadata from either MIME content type and magic file data.

type Detail

type Detail struct {
	XMLName    xml.Name     `json:"-"          xml:"file"`
	Name       string       `json:"filename"   xml:"name"`          // Name is the file name.
	Unicode    string       `json:"unicode"    xml:"unicode,attr"`  // Unicode is the file encoding if in Unicode.
	LineBreak  nl.LineBreak `json:"lineBreak"  xml:"line_break"`    // LineBreak is the line break used in the file.
	Count      Stats        `json:"counts"     xml:"counts"`        // Count is the file content statistics.
	Size       Sizes        `json:"size"       xml:"size"`          // Size is the file size in multiples.
	Lines      int          `json:"lines"      xml:"lines"`         // Lines is the number of lines in the file.
	Width      int          `json:"width"      xml:"width"`         // Width is the number of characters per line in the file, this may be inaccurate.
	Modified   ModDates     `json:"modified"   xml:"last_modified"` // Modified is the last modified date of the file.
	Sums       Checksums    `json:"checksums"  xml:"checksums"`     // Sums are the checksums of the file.
	Mime       Content      `json:"mime"       xml:"mime"`          // Mime is the file content metadata.
	Slug       string       `json:"slug"       xml:"id,attr"`       // Slug is the file name slugified.
	Sauce      sauce.Record `json:"sauce"      xml:"sauce"`         // Sauce is the SAUCE metadata.
	ZipComment string       `json:"zipComment" xml:"zip_comment"`   // ZipComment is the zip file comment.
	UTF8       bool         `json:"-"          xml:"-"`             // UTF8 is true if the file is UTF-8 encoded.
	LegacySums bool         `json:"-"          xml:"-"`             // LegacySums is true if the user requests legacy checksums.
	// contains filtered or unexported fields
}

Detail is the exported file details.

func (*Detail) Ctrls

func (d *Detail) Ctrls(name string) error

Ctrls counts the number of ANSI escape controls in the named file.

Example
package main

import (
	"fmt"

	"github.com/bengarrett/retrotxtgo/info"
)

func main() {
	d := info.Detail{}
	_ = d.Ctrls("testdata/example.ans")
	fmt.Printf("%d controls\n", d.Count.Controls)
}
Output:

12 controls

func (*Detail) Len

func (d *Detail) Len(name string) error

Len counts the number of characters used per line in the named file.

func (*Detail) Marshal

func (d *Detail) Marshal(w io.Writer, f Format) error

Marshal writes the Detail data in a given format syntax.

Example
package main

import (
	"bytes"
	"encoding/json"
	"fmt"

	"github.com/bengarrett/retrotxtgo/info"
)

func main() {
	d := info.Detail{
		Name: "example.ans",
	}
	b := bytes.Buffer{}
	_ = d.Marshal(&b, info.JSON)
	fmt.Printf("%d bytes, is json = %t", b.Len(), json.Valid(b.Bytes()))
}
Output:

2130 bytes, is json = true

func (*Detail) MimeUnknown

func (d *Detail) MimeUnknown()

MimeUnknown detects non-Standard legacy data.

func (*Detail) Parse

func (d *Detail) Parse(name string, data ...byte) error

Parse the file and the raw data content.

func (*Detail) Read

func (d *Detail) Read(name string) error

Read and parse the named file and content.

func (*Detail) Words

func (d *Detail) Words(name string) error

Words counts the number of words used in the named file.

type Format

type Format int

Format of the output text.

const (
	ColorText Format = iota // ColorText is ANSI colored text.
	PlainText               // PlainText is standard text.
	JSON                    // JSON data-interchange format.
	JSONMin                 // JSONMin is JSON data minified.
	XML                     // XML markup data.
)

type ModDates

type ModDates struct {
	Time  time.Time `json:"iso"   xml:"date"`       // Time is the last modified date of the file.
	Epoch int64     `json:"epoch" xml:"epoch,attr"` // Epoch is the last modified date of the file in seconds since the Unix epoch.
}

ModDates is the file last modified dates in multiple output formats.

type Sizes

type Sizes struct {
	Bytes   int64  `json:"bytes"   xml:"bytes"`        // Bytes is the size of the file in bytes.
	Decimal string `json:"decimal" xml:"decimal,attr"` // Decimal is the size of the file with decimal units.
	Binary  string `json:"binary"  xml:"binary,attr"`  // Binary is the size of the file with binary units.
}

Sizes of the file in multiples.

type Stats

type Stats struct {
	Chars    int `json:"characters"   xml:"characters"`    // Chars is the number of characters in the file.
	Controls int `json:"ansiControls" xml:"ansi_controls"` // Controls is the number of ANSI escape controls in the file.
	Words    int `json:"words"        xml:"words"`         // Words is the number of words in the file, this may be inaccurate.
}

Stats are the text file content statistics and counts.

Jump to

Keyboard shortcuts

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