Git Product home page Git Product logo

dirlist's Introduction

DirList.go

package dirlist

import (
	"encoding/json"
	"io/fs"
	"os"
	"sort"
	"time"
)

type FileInfo struct {
	Name  string      // base name of the file
	Size  int64       // length in bytes for regular files; system-dependent for others
	IsDir bool        // abbreviation for Mode().IsDir()
	Mode  fs.FileMode // file mode bits
	Mtime time.Time
}

type DirList struct {
	Path string
	List []*FileInfo
}

func New() *DirList {
	return &DirList{
		List: make([]*FileInfo, 0),
	}
}

func (d *DirList) Read(path string) []*FileInfo {
	d.Path = path
	dlist, err := os.ReadDir(path)
	if err != nil {
		return nil
	}
	files := make([]*FileInfo, 0)
	for _, entry := range dlist {
		info, err := entry.Info()
		if err == nil {
			f := new(FileInfo)
			f.Name = info.Name()
			f.Mtime = info.ModTime()
			f.Size = info.Size()
			f.Mode = info.Mode()
			f.IsDir = info.IsDir()
			files = append(files, f)
		}
	}
	d.List = files
	return files
}

func (d *DirList) SortByName() []*FileInfo {
	files := d.List
	sort.Slice(files, func(i, j int) bool {
		return files[i].Name > files[j].Name
	})
	return files
}

func (d *DirList) SortBySize() []*FileInfo {
	files := d.List
	sort.Slice(files, func(i, j int) bool {
		return files[i].Size > files[j].Size
	})
	return files
}

func (d *DirList) SortByTime() []*FileInfo {
	files := d.List
	sort.Slice(files, func(i, j int) bool {
		return files[i].Mtime.Before(files[j].Mtime)
	})
	return files
}

func (d *DirList) SortByType() []*FileInfo {
	files := d.List
	sort.Slice(files, func(i, j int) bool {
		return files[i].IsDir
	})
	return files
}

func (d *DirList) Json() string {
	j, err := json.MarshalIndent(d.SortByType(), "", "    ")
	if err != nil {
		return ""
	}
	return string(j)
}

dirlist's People

Watchers

 avatar

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    🖖 Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. 📊📈🎉

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google ❤️ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.