Пример #1
0
func (s *Session) Run() error {
	s.mode = &ModeEdit{}
	data, err := ioutil.ReadFile(s.File)
	if err != nil {
		return err
	}
	s.Buf = util.NewBuffer(data)
	s.W, s.H = termbox.Size()
	s.H--
	s.View.SetBuf(s.Buf)
	s.View.SetSize(s.W, s.H)
	s.View.SetTabwidth(s.Tabwidth)

	for {
		s.Draw()
		termbox.Flush()
		termbox.Clear(0, 0)

		ev := termbox.PollEvent()
		switch ev.Type {
		case termbox.EventKey:
			s.mode, err = s.mode.HandleKey(s, ev)
			if err != nil {
				return err
			}
		case termbox.EventResize:
			s.W, s.H = ev.Width, ev.Height-1
			s.View.SetSize(s.W, s.H)
		case termbox.EventMouse:
		case termbox.EventError:
			return ev.Err
		}
	}
}
Пример #2
0
func (m *ModeSearch) HandleKey(s *Session, ev termbox.Event) (Mode, error) {
	if m.s == nil {
		m.s = s
		m.b = util.NewBuffer([]byte{})
		m.view = &view.Wrap{}
		m.view.SetBuf(m.b)
		m.view.SetSize(s.W-1, 1)
		m.view.SetTabwidth(1)
	}

	var err error
	if ev.Ch != 0 {
		m.b.Insert(m.pos, ev.Ch)
		m.pos++
	}
	switch ev.Key {
	case termbox.KeyEnter:
		s.Search, err = regexp.Compile(string(m.b.Bytes()))
		if err != nil {
			msg := err.Error()
			for i, ch := range msg {
				termbox.SetCell(i, s.H, ch, 0, 0)
			}
			return &ModeEdit{}, nil
		}
		s.UpdSearch()
		s.NextMatch()
		return &ModeEdit{}, nil
	case termbox.KeySpace:
		m.b.Insert(m.pos, ' ')
		m.pos++
	case termbox.KeyBackspace, termbox.KeyBackspace2:
		m.b.Delete(m.pos, -1)
		m.pos--
	case termbox.KeyEsc:
		return &ModeEdit{}, nil
	}

	surf := m.view.Render()
	termbox.SetCell(0, s.H, '/', 0, 0)
	view.Draw(surf, 1, s.H)
	return m, nil
}
Пример #3
0
func testWrap(t *testing.T, i int) {
	v := &Wrap{}
	tst := viewtests[i]

	printtxt := strings.Replace(tst.text, "\n", "\\n", -1)
	printtxt = strings.Replace(printtxt, "\t", "\\t", -1)
	t.Logf("* test %v (%v): text='%v' tabw=%v (w,h) = (%v,%v)"+
		" (l,c,x,y) = (%v,%v,%v,%v",
		i, tst.name, printtxt, tst.tabw, tst.w, tst.h,
		tst.l, tst.c, tst.x, tst.y)

	b := util.NewBuffer([]byte(tst.text))
	v.SetBuf(b)
	v.SetSize(tst.w, tst.h)
	v.SetRef(tst.l, tst.c, tst.x, tst.y)
	v.SetTabwidth(tst.tabw)
	surf := v.Render()

	t.Log("\t* checking Surface.Char(x, y)")
	for y, row := range tst.expectch {
		for x, ch := range row {
			if got := surf.Char(x, y); ch != got {
				t.Errorf("\t\t[--] for x,y = %v,%v: expected %v, got %v",
					x, y, ch, got)
			} else {
				t.Logf("\t\t[OK] for x,y = %v,%v: expected %v, got %v",
					x, y, ch, got)
			}
		}
	}

	t.Log("\t* checking Surface.Line(x, y)")
	for y, row := range tst.expectl {
		for x, l := range row {
			if got := surf.Line(x, y); l != got {
				t.Errorf("\t\t[--] for x,y = %v,%v: expected %v, got %v",
					x, y, l, got)
			} else {
				t.Logf("\t\t[OK] for x,y = %v,%v: expected %v, got %v",
					x, y, l, got)
			}
		}
	}

	t.Log("\t* checking Surface.X(l, ch)")
	for l, row := range tst.expectx {
		for ch, x := range row {
			if got := surf.X(l, ch); x != got {
				t.Errorf("\t\t[--] for l,c = %v,%v: expected %v, got %v",
					l, ch, x, got)
			} else {
				t.Logf("\t\t[OK] for l,c = %v,%v: expected %v, got %v",
					l, ch, x, got)
			}
		}
	}

	t.Log("\t* checking Surface.Y(l, ch)")
	for l, row := range tst.expecty {
		for ch, y := range row {
			if got := surf.Y(l, ch); y != got {
				t.Errorf("\t\t[--] for l,c = %v,%v: expected %v, got %v",
					l, ch, y, got)
			} else {
				t.Logf("\t\t[OK] for l,c = %v,%v: expected %v, got %v",
					l, ch, y, got)
			}
		}
	}

	printSurf(t, surf, tst, b)
}