summaryrefslogtreecommitdiff
path: root/listWindow.go
blob: 3f87050a580c5f8b9afa823d23dee96402241033 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
// This is a simple example
package main

import (
	"io/ioutil"
	"net/http"
	"os"
	"strings"

	"go.wit.com/gui"
	"go.wit.com/log"

	"go.wit.com/lib/gadgets"
)

var lw *gadgets.BasicWindow
var allsections []*section

type witRepo struct {
	sec       *section
	path      *gui.Node
	downloadB *gui.Node
}

type section struct {
	name     string
	parent   *gui.Node
	box      *gui.Node
	group    *gui.Node
	grid     *gui.Node // where the repos are listed
	hideCB   *gui.Node
	witRepos []*witRepo
}

func listWindow() {
	if lw != nil {
		lw.Toggle()
		return
	}
	lw = gadgets.NewBasicWindow(me.myGui, "go.wit.com repositories")
	lw.Custom = func() {
		log.Warn("got to close")
	}

	lw.Make()
	lw.StandardClose()
	lw.Draw()
	box := lw.Box()
	group := box.NewGroup("list")
	group.NewButton("blah", func() {})

	var lines []string
	var curs *section

	lines = dumpURL("https://go.wit.com/list")
	for i, line := range lines {
		if line == "" {
			continue
		}
		if line[0] == '#' {
			curs = NewSection(group, line)
			log.Warn("new group:", line)
			continue
		}
		log.Warn(i, line)
		parts := strings.Split(line, " ")
		if curs != nil {
			curs.add(parts[0])
		}
	}
}

func (s *section) add(path string) {
	if s == nil {
		return
	}
	tmp := new(witRepo)
	tmp.sec = s
	tmp.path = s.grid.NewLabel(path)
	tmp.downloadB = s.grid.NewButton("download", func() {
		lw.Disable()
		log.Info("downloading", tmp.path.String(), "here")
		os.Setenv("GO111MODULE", "off")
		fullpath := "/home/jcarr/go/src/go.wit.com/"
		quickCmd(fullpath, []string{"go", "get", "-v", tmp.path.String()})
		lw.Enable()
	})

	s.witRepos = append(s.witRepos, tmp)
}

func NewSection(parent *gui.Node, path string) *section {
	news := new(section)
	news.parent = parent
	news.box = news.parent.NewBox("bw vbox", true)
	news.group = news.box.NewGroup(path)
	news.hideCB = news.box.NewCheckbox("hide")
	news.hideCB.Custom = func() {
		news.toggle()
	}
	news.grid = news.parent.NewGrid("sections", 2, 1)
	allsections = append(allsections, news)
	return news
}

func (s *section) toggle() {
	log.Warn(s.name)
	for i, wrepo := range s.witRepos {
		log.Warn(i, wrepo.path.String())
		wrepo.path.Hide()
		wrepo.downloadB.Hide()
	}
}

/*
func dumpURL(url string) string {
	resp, err := http.Get(url)
	if err != nil {
		return ""
	}
	defer resp.Body.Close()

	return resp.Body.String()

		_, err = io.Copy(os.Stdout, resp.Body)
		if err != nil {
			return ""
		}
}
*/

func dumpURL(url string) []string {
	resp, err := http.Get(url)
	if err != nil {
		return nil
	}
	defer resp.Body.Close()

	bodyBytes, err := ioutil.ReadAll(resp.Body)
	if err != nil {
		return nil
	}

	return strings.Split(string(bodyBytes), "\n")
}