Beispiel #1
0
func main() {
	cmd.UnixIO("err")
	c := cmd.AppCtx()
	opts.NewFlag("D", "debug", &c.Debug)
	opts.NewFlag("u", "unix IO", &ux)
	opts.NewFlag("g", "get contents", &gflag)
	if cmd.Args()[0] == "gf" {
		gflag = true
	}
	args := opts.Parse()
	if ux {
		cmd.UnixIO()
	}
	if len(args) == 0 {
		args = append(args, ".,1")
	}

	var dc <-chan face{}
	if !gflag {
		dc = cmd.Dirs(args...)
	} else {
		dc = cmd.Files(args...)
	}

	out := cmd.Out("out")
	var err error
	for m := range dc {
		cmd.Dprintf("got %T\n", m)
		switch m := m.(type) {
		case error:
			err = m
			cmd.Warn("%s", m)
			if !ux {
				m := fmt.Errorf("%s: %s", cmd.Args()[0], m)
				if ok := out <- m; !ok {
					close(dc, cerror(out))
				}
			}
		case zx.Dir:
			if !ux {
				if ok := out <- m; !ok {
					close(dc, cerror(out))
				}
			} else {
				printf("%s\n", m.Fmt())
			}
		case []byte:
			if ok := out <- m; !ok {
				close(dc, cerror(out))
			}
		}
	}
	if err := cerror(dc); err != nil {
		if !ux {
			out <- fmt.Errorf("%s: %s", cmd.Args()[0], err)
		}
		cmd.Exit(err)
	}
	cmd.Exit(err)
}
Beispiel #2
0
func col() {
	if wid == 0 {
		wid, _ = tty.Cols()
		if wid == 0 {
			wid = 70
		}
	}
	colwid := maxwid + 2
	if ncols == 0 {
		ncols = wid / colwid
	}
	if ncols == 0 {
		ncols = 1
	}
	var buf bytes.Buffer
	for i, w := range words {
		nw := utf8.RuneCountInString(w)
		spcs := ""
		if nw < colwid-1 {
			spcs = strings.Repeat(" ", colwid-1-nw)
		}
		fmt.Fprintf(&buf, "%s%s", w, spcs)
		if (i+1)%ncols == 0 {
			fmt.Fprintf(&buf, "\n")
		}
	}
	if len(words)%ncols != 0 {
		fmt.Fprintf(&buf, "\n")
	}
	cmd.Out("out") <- buf.Bytes()
}
Beispiel #3
0
func main() {
	opts := opt.New("[file]")
	c := cmd.AppCtx()
	opts.NewFlag("D", "debug", &c.Debug)
	rdonly := false
	opts.NewFlag("r", "read only", &rdonly)
	opts.NewFlag("e", "do edits for testing", &doedits)
	cmd.UnixIO("err")
	args := opts.Parse()
	var t *ink.Txt
	inkout := cmd.Out("ink")
	if inkout == nil {
		cmd.UnixIO()
	}
	if len(args) == 0 {
		t = ink.NewTxt("1234", "abc")
	} else {
		dat, err := zx.GetAll(cmd.NS(), cmd.AbsPath(args[0]))
		if err != nil {
			cmd.Fatal(err)
		}
		t = ink.NewTxt(args[0]+" Del", string(dat))
	}
	go edit(t)
	if rdonly {
		t.NoEdits()
	}
	ink.UsePort("8182")
	bs := ink.NewButtonSet(&ink.Button{Tag: "One", Name: "one"},
		&ink.Button{Tag: "Two", Name: "two"},
		&ink.Button{Tag: "B", Name: "b", Value: &bold},
		&ink.Button{Tag: "I", Name: "i", Value: &italic})
	rs := ink.NewRadioSet(&style, &ink.Button{Tag: "R", Name: "r"},
		&ink.Button{Tag: "T", Name: "t"})
	go buttons(bs, rs, t)

	pg := ink.NewPg("/", "Example text editing:", bs, rs, t)
	pg.Tag = "Clive's iedit"
	if doedits {
		go edits(t)
	}
	go ink.Serve()
	if inkout != nil {
		// TODO: ctlrs must use unique ids sytem-wide, or
		// controls written won't work because of their ids.
		inkout <- []byte(`<tt>Hi there, this is HTML</tt>`)
		var buf bytes.Buffer
		bs.WriteTo(&buf)
		inkout <- buf.Bytes()
		inkout <- []byte("https://localhost:8182")
	}
	t.Wait()
	for rs := range t.Get(0, -1) {
		cmd.Printf("%s", string(rs))
	}
}
Beispiel #4
0
func runFiles(fn func(nm string, c chan []byte, dc chan bool)) {
	var lnc chan []byte
	var dc chan bool
	in := cmd.Lines(cmd.In("in"))
	out := cmd.Out("out")
	nm := "in"
	for m := range in {
		cmd.Dprintf("got %T\n", m)
		switch m := m.(type) {
		case zx.Dir:
			if pflag {
				p := m["Upath"]
				if p == "" {
					p = m["path"]
				}
				nm = p
			}
			if dc != nil {
				close(lnc)
				<-dc
			}
			dc = nil
			lnc = nil
			if ok := out <- m; !ok {
				close(in, cerror(out))
			}
		case []byte:
			if dc == nil {
				lnc = make(chan []byte)
				dc = make(chan bool, 1)
				go fn(nm, lnc, dc)
			}
			if ok := lnc <- m; !ok {
				close(in, cerror(lnc))
				close(out, cerror(lnc))
			}
		default:
			if dc != nil {
				close(lnc)
				<-dc
			}
			dc = nil
			lnc = nil
			if ok := out <- m; !ok {
				close(lnc, cerror(out))
				close(in, cerror(out))
			}
		}
	}
	if dc != nil {
		close(lnc, cerror(in))
		<-dc
	}
	cmd.Exit(cerror(in))
}
Beispiel #5
0
// Run echo in the current app context.
func main() {
	cmd.UnixIO("err")
	c := cmd.AppCtx()
	opts.NewFlag("D", "debug", &c.Debug)
	opts.NewFlag("n", "don't add a final newline", &nflag)
	opts.NewFlag("m", "issue one message per arg", &mflag)
	opts.NewFlag("u", "use unix out", &ux)
	opts.NewFlag("o", "chan: output to this chan (for testing other tools)", &oname)
	opts.NewFlag("i", "chan: echo input from this chan (for testing other tools)", &iname)
	args := opts.Parse()
	if ux {
		cmd.UnixIO(oname)
	}
	var b bytes.Buffer
	out := cmd.Out(oname)
	if out == nil {
		cmd.Fatal("no output chan '%s'", oname)
	}
	mflag = mflag || iname != ""
	for i, arg := range args {
		if mflag {
			ok := out <- []byte(arg)
			if !ok {
				cmd.Fatal("out: %s", cerror(out))
			}
		} else {
			b.WriteString(arg)
			if i < len(args)-1 {
				b.WriteString(" ")
			}
		}
	}
	if iname != "" {
		for x := range cmd.In(iname) {
			x := x
			if b, ok := x.([]byte); ok {
				ok := out <- []byte(b)
				if !ok {
					cmd.Fatal("out: %s", cerror(out))
				}
			}
		}
	}
	if mflag {
		cmd.Exit(nil)
	}
	if !nflag {
		b.WriteString("\n")
	}
	ok := out <- b.Bytes()
	if !ok {
		cmd.Fatal("out: %s", cerror(out))
	}
}
Beispiel #6
0
// Run print lines in the current app context.
func main() {
	c := cmd.AppCtx()
	cmd.UnixIO("err")
	opts.NewFlag("D", "debug", &c.Debug)
	opts.NewFlag("1", "collect all files (not one msg per file)", &one)
	ux := false
	opts.NewFlag("u", "use unix out", &ux)
	args := opts.Parse()
	if ux {
		cmd.UnixIO("out")
	}
	if len(args) != 0 {
		cmd.SetIn("in", cmd.Files(args...))
	}
	buf := &bytes.Buffer{}
	in := cmd.In("in")
	out := cmd.Out("out")
	for m := range in {
		switch m := m.(type) {
		case []byte:
			buf.Write(m)
		case zx.Dir:
			if !one && buf.Len() > 0 {
				if ok := out <- buf.Bytes(); !ok {
					close(in, cerror(out))
					break
				}
				buf = &bytes.Buffer{}
			}
			if !one && !ux {
				if ok := out <- m; !ok {
					close(in, cerror(out))
					break
				}
			}
		case error:
			cmd.Warn("%s", m)
		default:
			cmd.Dprintf("ignored %T\n", m)
			if !ux {
				if ok := out <- m; !ok {
					close(in, cerror(out))
					break
				}
			}
		}
	}
	if buf.Len() > 0 {
		out <- buf.Bytes()
	}
	if err := cerror(in); err != nil {
		cmd.Fatal(err)
	}
}
Beispiel #7
0
func main() {
	cmd.UnixIO("err")
	cmd.UnixIO("in")
	args := opts.Parse()
	if len(args) != 0 {
		in := cmd.Files(args...)
		cmd.SetIn("in", in)
	}
	out := cmd.Out("out")
	in := cmd.In("in")
	for m := range in {
		if ok := out <- m; !ok {
			close(in, cerror(out))
		}
	}
	if err := cerror(in); err != nil {
		cmd.Fatal(err)
	}
}
Beispiel #8
0
func kq(dir, wcmd string) {
	w, err := fswatch.New()
	if err != nil {
		cmd.Fatal("kqueue: %s", err)
	}

	cmd.Dprintf("(re)read %s\n", dir)
	if err := w.Add(dir); err != nil {
		cmd.Fatal("kqueue: %s", err)
	}
	if err != nil {
		cmd.Fatal("watch: %s", err)
	}
	var pc chan string
	if once {
		w.Once()
	}
	pc = w.Changes()
	for p := range pc {
		if wcmd == "" {
			cmd.Warn("%s", p)
			continue
		}
		cln := strings.Replace(wcmd, "%", p, -1)
		cmd.Dprintf("run %s\n", cln)
		out, err := exec.Command("sh", "-c", cln).CombinedOutput()
		cmd.Out("out") <- out
		if err != nil {
			cmd.Warn("run: %s", err)
		}
	}
	if err := cerror(pc); err != nil {
		cmd.Fatal(err)
	}
	cmd.Exit(nil)
}