~psic4t/qcal

81f1e2dedc8983ccba0337f0aacbac17023f4daf — psic4t 9 months ago da91dd5
parallelize calendar listing
3 files changed, 35 insertions(+), 22 deletions(-)

M defines.go
M helpers.go
M main.go
M defines.go => defines.go +10 -1
@@ 27,7 27,7 @@ var colorBlock string = "█"
var elements []Event
var Colors = [10]string{"\033[0;31m", "\033[0;32m", "\033[1;33m", "\033[1;34m", "\033[1;35m", "\033[1;36m", "\033[1;37m", "\033[1;38m", "\033[1;39m", "\033[1;40m"}
var showColor bool = true
var qcalversion string = "0.8.2"
var qcalversion string = "0.8.3"

const (
	ConfigDir      = ".config/qcal"


@@ 64,6 64,8 @@ type configStruct struct {
}

type props struct {
	calNo        string
	Url          string
	XMLName      xml.Name `xml:"multistatus"`
	Href         string   `xml:"response>href"`
	DisplayName  string   `xml:"response>propstat>prop>displayname"`


@@ 73,6 75,13 @@ type props struct {
	LastModified string   `xml:"response>propstat>prop>getlastmodified"`
}

type calProps struct {
	calNo       int
	displayName string
	url         string
	color       string
}

type Caldata struct {
	XMLName xml.Name     `xml:"multistatus"`
	Caldata []Calelement `xml:"response"`

M helpers.go => helpers.go +24 -20
@@ 12,6 12,7 @@ import (
	"os"
	"path"
	"regexp"
	"sort"
	"strconv"
	"strings"
	"sync"


@@ 29,38 30,34 @@ func getConf() *configStruct {
	err = json.Unmarshal(configData, &conf)
	//fmt.Println(conf)
	if err != nil {
		fmt.Println("error:", err)
		log.Fatal(err)
	}

	return &conf
}

func getProp() {
	p := props{}
	p := []calProps{}

	var wg sync.WaitGroup
	wg.Add(len(config.Calendars)) // waitgroup length = num calendars

	for i := range config.Calendars {
		//var p = props{}
		getCalProp(i, &p, &wg)

		//fmt.Printf(xml.Unmarshal(xmlContent, &p))
		/*fmt.Println(`[` + fmt.Sprintf("%v", i) + `] - ` + Colors[i] + colorBlock + ColDefault +
		` ` + p.DisplayName + ` (` + config.Calendars[i].Url + `)`)*/
		go getCalProp(i, &p, &wg)
	}
	wg.Wait()

	fmt.Println(p.DisplayName)
	for i := range p.DisplayName {
		fmt.Println(i)
		//fmt.Println(p.DisplayName[i])
		/*fmt.Println(`[` + fmt.Sprintf("%v", i) + `] - ` + Colors[i] + colorBlock + ColDefault +
		` ` + p.DisplayName[i] + ` (` + config.Calendars[i].Url + `)`)*/
	sort.Slice(p, func(i, j int) bool {
		return p[i].calNo < (p[j].calNo)
	})

	for i := range p {
		fmt.Println(`[` + fmt.Sprintf("%v", i) + `] - ` + Colors[i] + colorBlock + ColDefault +
			` ` + p[i].displayName + ` (` + config.Calendars[i].Url + `)`)
	}
}

func getCalProp(calNo int, p *props, wg *sync.WaitGroup) {
func getCalProp(calNo int, p *[]calProps, wg *sync.WaitGroup) {
	req, err := http.NewRequest("PROPFIND", config.Calendars[calNo].Url, nil)
	req.SetBasicAuth(config.Calendars[calNo].Username, config.Calendars[calNo].Password)



@@ 78,17 75,24 @@ func getCalProp(calNo int, p *props, wg *sync.WaitGroup) {
	xmlContent, _ := ioutil.ReadAll(resp.Body)
	defer resp.Body.Close()

	var displayName string
	if config.Calendars[calNo].Username == "" {
		p.DisplayName = parseIcalName(string(xmlContent))
		fmt.Println(p.DisplayName)
		displayName = parseIcalName(string(xmlContent))
	} else {
		err = xml.Unmarshal(xmlContent, p)
		xmlProps := props{}
		err = xml.Unmarshal(xmlContent, &xmlProps)
		if err != nil {
			log.Fatal(err)
		}
		fmt.Println(p.DisplayName)
		displayName = xmlProps.DisplayName
	}

	thisCal := calProps{
		calNo:       calNo,
		displayName: displayName,
		url:         config.Calendars[calNo].Url,
	}
	//fmt.Println(p.DisplayName)
	*p = append(*p, thisCal)

	wg.Done()
}

M main.go => main.go +1 -1
@@ 132,7 132,7 @@ func showAppointments(singleCal string) {
	})

	if len(elements) == 0 {
		log.Fatal("nothing found") // get out if nothing found
		log.Fatal("no events") // get out if nothing found
	}

	// pretty print