Esempio n. 1
0
func TestLoadTheme(t *testing.T) {
	type Test struct {
		in  string
		out string
	}
	tests := []Test{
		{"testdata/Monokai.tmTheme", "testdata/Monokai.tmTheme.res"},
	}
	for _, test := range tests {
		if d, err := ioutil.ReadFile(test.in); err != nil {
			t.Logf("Couldn't load file %s: %s", test.in, err)
		} else {
			var theme Theme
			if err := loaders.LoadPlist(d, &theme); err != nil {
				t.Error(err)
			} else {
				str := fmt.Sprintf("%s", theme)
				if d, err := ioutil.ReadFile(test.out); err != nil {
					if err := ioutil.WriteFile(test.out, []byte(str), 0644); err != nil {
						t.Error(err)
					}
				} else if diff := util.Diff(string(d), str); diff != "" {
					t.Error(diff)
				}

			}
		}
	}
}
Esempio n. 2
0
func LoadTheme(filename string) (*Theme, error) {
	var scheme Theme
	if d, err := ioutil.ReadFile(filename); err != nil {
		return nil, fmt.Errorf("Unable to load colorscheme definition: %s", err)
	} else if err := loaders.LoadPlist(d, &scheme); err != nil {
		return nil, fmt.Errorf("Unable to load colorscheme definition: %s", err)
	}

	return &scheme, nil
}
Esempio n. 3
0
func (t *LanguageProvider) LanguageFromFile(fn string) (*Language, error) {
	d, err := ioutil.ReadFile(fn)
	if err != nil {
		return nil, fmt.Errorf("Couldn't load file %s: %s", fn, err)
	}
	var l Language
	if err := loaders.LoadPlist(d, &l); err != nil {
		return nil, err
	}
	t.Lock()
	defer t.Unlock()
	t.scope[l.ScopeName] = fn
	return &l, nil
}
Esempio n. 4
0
func (t *tbfe) loop() {
	ed := backend.GetEditor()
	ed.SetFrontend(t)
	//ed.LogInput(true)
	//ed.LogCommands(true)
	c := ed.Console()
	var (
		scheme textmate.Theme
	)

	if d, err := ioutil.ReadFile("../../3rdparty/bundles/TextMate-Themes/GlitterBomb.tmTheme"); err != nil {
		log4go.Error("Unable to load colorscheme definition: %s", err)
	} else if err := loaders.LoadPlist(d, &scheme); err != nil {
		log4go.Error("Unable to load colorscheme definition: %s", err)
	}

	var (
		palLut  func(col textmate.Color) termbox.Attribute
		pal     = make([]termbox.RGB, 0, 256)
		mode256 bool
	)

	if err := termbox.SetColorMode(termbox.ColorMode256); err != nil {
		log4go.Error("Unable to use 256 color mode: %s", err)
	} else {
		log4go.Debug("Using 256 color mode")
		mode256 = true
	}

	if !mode256 {
		pal = pal[:10] // Not correct, but whatever
		pal[termbox.ColorBlack] = termbox.RGB{0, 0, 0}
		pal[termbox.ColorWhite] = termbox.RGB{255, 255, 255}
		pal[termbox.ColorRed] = termbox.RGB{255, 0, 0}
		pal[termbox.ColorGreen] = termbox.RGB{0, 255, 0}
		pal[termbox.ColorBlue] = termbox.RGB{0, 0, 255}
		pal[termbox.ColorMagenta] = termbox.RGB{255, 0, 255}
		pal[termbox.ColorYellow] = termbox.RGB{255, 255, 0}
		pal[termbox.ColorCyan] = termbox.RGB{0, 255, 255}

		diff := func(i, j byte) int {
			v := int(i) - int(j)
			if v < 0 {
				return -v
			}
			return v
		}
		palLut = func(col textmate.Color) termbox.Attribute {
			mindist := 10000000
			mini := 0
			for i, c := range pal {
				if dist := diff(c.R, col.R) + diff(c.G, col.G) + diff(c.B, col.B); dist < mindist {
					mindist = dist
					mini = i
				}
			}
			return termbox.Attribute(mini)
		}
	} else {
		palLut = func(col textmate.Color) termbox.Attribute {
			tc := termbox.RGB{col.R, col.G, col.B}
			for i, c := range pal {
				if c == tc {
					return termbox.Attribute(i)
				}
			}
			l := len(pal)
			pal = append(pal, tc)
			return termbox.Attribute(l)
		}
	}
	for i, s := range scheme.Settings {
		var (
			fi = defaultFg
			bi = defaultBg
		)
		if fg, ok := s.Settings["foreground"]; ok {
			fi = palLut(fg)
			if i == 0 {
				defaultFg = fi
			}
		}
		if bg, ok := s.Settings["background"]; ok {
			bi = palLut(bg)
			if i == 0 {
				defaultBg = bi
			}
		}
		schemelut[s.Scope] = [2]termbox.Attribute{fi, bi}
	}
	if mode256 {
		termbox.SetColorPalette(pal)
	}
	defer func() {
		termbox.Close()
		fmt.Println(c.Buffer().Data())
	}()

	w := ed.NewWindow()
	t.active_window = w
	v := w.OpenFile("main.go", 0)
	v.Settings().Set("trace", true)
	c.Buffer().AddCallback(t.scroll)

	if err := v.SetSyntaxFile("../../3rdparty/bundles/GoSublime/GoSublime.tmLanguage"); err != nil {
		log4go.Error("Unable to set syntax file: %s", err)
	}
	sel := v.Sel()
	sel.Clear()
	//	end := v.Buffer().Size() - 2
	sel.Add(primitives.Region{0, 0})
	// sel.Add(primitives.Region{end - 22, end - 22})
	// sel.Add(primitives.Region{end - 16, end - 20})
	// sel.Add(primitives.Region{end - 13, end - 10})

	evchan := make(chan termbox.Event)

	go func() {
		for {
			evchan <- termbox.PollEvent()
		}
	}()

	sublime.Init()
	for {
		blink = !blink
		termbox.Clear(defaultFg, defaultBg)
		w, h := termbox.Size()

		t.renderView(0, 0, w, h-console_height, v)
		t.renderView(0, h-(console_height), w, (console_height - 1), c)
		runes := []rune(t.status_message)
		for i := 0; i < w && i < len(runes); i++ {
			termbox.SetCell(i, h-1, runes[i], defaultFg, defaultBg)
		}
		termbox.Flush()

		blink_phase := time.Second
		if p, ok := ed.Settings().Get("caret_blink_phase", 1.0).(float64); ok {
			blink_phase = time.Duration(float64(time.Second) * p)
		}

		select {
		case ev := <-evchan:
			switch ev.Type {
			case termbox.EventKey:
				var kp backend.KeyPress

				if ev.Ch != 0 {
					kp.Key = backend.Key(ev.Ch)
				} else if v2, ok := lut[ev.Key]; ok {
					kp = v2
				} else {
					break
				}

				if ev.Key == termbox.KeyCtrlQ {
					return
				}
				ed.HandleInput(kp)
				blink = false
			}
		case <-time.After(blink_phase / 2):
			// Divided by two since we're only doing a simple toggle blink
			// TODO(q): Shouldn't redraw if blink is disabled...
		}
	}
}