Skip to content
Snippets Groups Projects
Select Git revision
  • 2d28debd45902eef816bd3a349a136c0ae7a94c4
  • master default protected
  • classdiag
  • 13.2.1 protected
  • 13.2.0 protected
  • 13.1.2 protected
  • 13.1.1 protected
  • 13.1.0 protected
  • 13.0.1 protected
  • 13.0.0 protected
  • 10.0.1 protected
  • 10.0.0 protected
  • 7.0.0 protected
  • 6.0.4 protected
  • 6.0.2 protected
  • 6.0.1 protected
  • 6.0.0 protected
  • 5.3.0 protected
  • 5.2.8 protected
  • 5.2.5 protected
  • 5.1.2 protected
  • 5.1.0 protected
  • 5.1.1 protected
23 results

filelist.go

Blame
  • filelist.go 2.42 KiB
    package main
    
    import (
    	"fmt"
    	"git.sch.bme.hu/insert-epic-projlab-team-name-here/tooling/plab/helpers"
    	"io/fs"
    	"io/ioutil"
    	"os/exec"
    	"path/filepath"
    	"strconv"
    	"strings"
    	"time"
    	"unicode"
    )
    
    type fileinfo struct {
    	name  string
    	size  string
    	date  string
    	notes string
    }
    
    var Filelist = helpers.Subcommand{
    	Name: "filelist",
    	Command: func(args []string) {
    		if len(args) != 2 {
    			fmt.Println("usage: <dirname> <filelist>")
    			return
    		}
    
    		list := readFile(args[1])
    		list_map := mapizator(list)
    		files := getData(args[0], list_map)
    		genLatex(files)
    	},
    	Help: "generate file list", // TODO
    }
    
    func readFile(filename string) []string {
    	b, e := ioutil.ReadFile(filename)
    	if e != nil {
    		panic(e)
    	}
    	str := string(b)
    	return strings.Split(str, "\n")
    }
    
    func mapizator(list []string) map[string]string {
    	list_map := make(map[string]string)
    
    	for _, v := range list {
    		elements := strings.Split(v, "=")
    		list_map[elements[0]] = elements[1]
    	}
    	return list_map
    }
    
    func getCreateTime(filepath string) string {
    	b, e := exec.Command("git", "log", "--format=%ai", filepath).Output()
    	if e != nil {
    		b = make([]byte, 0)
    	}
    
    	str := string(b)
    	lines := strings.Split(str, "\n")
    	str = lines[len(lines)-1]
    
    	if strings.Contains(str, "") {
    		str = time.Now().Format("2006-01-02 15:04:05")
    	}
    
    	return str
    }
    
    func latexizeName(name string) string {
    	cop := ""
    	for i, ch := range name {
    		if i != 0 && unicode.IsUpper(ch) {
    			cop += `\-`
    		}
    		cop += string(ch)
    	}
    	return cop
    }
    
    func getData(dirname string, list map[string]string) []fileinfo {
    	var files []fileinfo
    
    	e := filepath.Walk(dirname, func(path string, info fs.FileInfo, err error) error {
    		if err != nil {
    			return err
    		}
    
    		if data, ok := list[info.Name()]; ok {
    			file := fileinfo{
    				name:  latexizeName(info.Name()),
    				size:  strconv.FormatInt(info.Size(), 10) + " byte",
    				date:  getCreateTime(path + info.Name()),
    				notes: data,
    			}
    			files = append(files, file)
    		}
    
    		return nil
    	})
    
    	if e != nil {
    		panic(e)
    	}
    
    	return files
    }
    
    func genLatex(files []fileinfo) {
    	fmt.Println("\\newcolumntype{P}[1]{>{\\hspace{0pt}}p{#1}}") // TODO: rm % if needed
    	fmt.Println("\\noindent\\begin{xltabular}{\\textwidth}{|P|l|l|X|}")
    	fmt.Println("\\hline Fájlnév & Méret & Keletkezés ideje & Tartalom \\\\ \\hline \\hline \\endhead")
    
    	for _, file := range files {
    		fmt.Printf("%s & %s & %s & %s \\\\ \\hline\n", file.name, file.size, file.date, file.notes)
    	}
    
    	fmt.Println("\\end{xltabular}")
    
    }