<commit_before>package term

import (
	"io"
	"unicode"
	"unicode/utf8"

	"golang.org/x/mobile/event/key"
	"golang.org/x/mobile/event/mouse"

	"github.com/gdamore/tcell"
	"github.com/mibk/syd/ui"
)

var (
	whitebg = tcell.StyleDefault.Background(tcell.ColorWhite)

	dirtystyle  = tcell.StyleDefault.Background(tcell.GetColor("#e5083c"))
	borderstyle = tcell.StyleDefault.Background(tcell.GetColor("#83835c"))

	tagbg  = tcell.StyleDefault.Background(tcell.GetColor("#eaffff"))
	taghl  = tcell.StyleDefault.Background(tcell.GetColor("#90e0e0"))
	bodybg = tcell.StyleDefault.Background(tcell.GetColor("#ffffea"))
	bodyhl = tcell.StyleDefault.Background(tcell.GetColor("#e0e090"))

	testbg = tcell.StyleDefault.Background(tcell.GetColor("#ffe0ff"))
)

// TODO: Move to package ui.
type WindowMovedHandler func(win *Window, from *Column)

type UI struct {
	screen        tcell.Screen
	wasBtnPressed bool

	y      int
	width  int
	height int

	firstCol *Column

	grabbedCol *Column // grabbed col or nil
	grabbedWin *Window // grabbed win or nil
	activeText *Text   // will receive key events
}

func (t *UI) Init() error {
	sc, err := tcell.NewScreen()
	if err != nil {
		return err
	}
	if err := sc.Init(); err != nil {
		return err
	}
	sc.EnableMouse()
	t.screen = sc

	// TODO: Just for testing purposes.
	t.y = 1
	w, h := t.Size()
	t.width = w - 1
	t.height = h - 2

	go t.translateEvents()
	return nil
}

func (t *UI) Close() error {
	t.screen.Fini()
	return nil
}

func (t *UI) Size() (w, h int) { return t.screen.Size() }

func (t *UI) Flush() {
	col := t.firstCol
	for col != nil {
		col.flush()
		col = col.nextCol
	}
	t.screen.Show()
}

// TODO: This is for temporary reasons. Remove it.
func (t *UI) Push_Mouse_Event(ev mouse.Event) {
	col := t.firstCol
	for col != nil {
		if int(ev.X) < col.x+col.width() {
			col.handleMouseEvent(ev)
			return
		}
		col = col.nextCol
	}
	panic("column not found")
}

func (t *UI) Push_Key_Event(ev key.Event) {
	t.activeText.keyEventHandler(ev)
}

func (t *UI) NewColumn() *Column {
	col := &Column{ui: t}
	if t.firstCol == nil {
		t.firstCol = col
	} else {
		prev := t.lastCol()
		col.x = prev.x + prev.width()/2
		prev.nextCol = col
	}
	return col
}

func (t *UI) lastCol() *Column {
	col := t.firstCol
	if col == nil {
		return nil
	}
	for col.nextCol != nil {
		col = col.nextCol
	}
	return col
}

func (t *UI) moveGrabbedCol(x int) {
	gc := t.grabbedCol
	t.grabbedCol = nil

	// TODO: If there are no columns.

	target := t.firstCol
	for target != nil {
		if x >= target.x && x < target.x+target.width() {
			break
		}<commit_msg>ui/term: Change the ratio of the newly created column size<commit_after>package term

import (
	"io"
	"unicode"
	"unicode/utf8"

	"golang.org/x/mobile/event/key"
	"golang.org/x/mobile/event/mouse"

	"github.com/gdamore/tcell"
	"github.com/mibk/syd/ui"
)

var (
	whitebg = tcell.StyleDefault.Background(tcell.ColorWhite)

	dirtystyle  = tcell.StyleDefault.Background(tcell.GetColor("#e5083c"))
	borderstyle = tcell.StyleDefault.Background(tcell.GetColor("#83835c"))

	tagbg  = tcell.StyleDefault.Background(tcell.GetColor("#eaffff"))
	taghl  = tcell.StyleDefault.Background(tcell.GetColor("#90e0e0"))
	bodybg = tcell.StyleDefault.Background(tcell.GetColor("#ffffea"))
	bodyhl = tcell.StyleDefault.Background(tcell.GetColor("#e0e090"))

	testbg = tcell.StyleDefault.Background(tcell.GetColor("#ffe0ff"))
)

// TODO: Move to package ui.
type WindowMovedHandler func(win *Window, from *Column)

type UI struct {
	screen        tcell.Screen
	wasBtnPressed bool

	y      int
	width  int
	height int

	firstCol *Column

	grabbedCol *Column // grabbed col or nil
	grabbedWin *Window // grabbed win or nil
	activeText *Text   // will receive key events
}

func (t *UI) Init() error {
	sc, err := tcell.NewScreen()
	if err != nil {
		return err
	}
	if err := sc.Init(); err != nil {
		return err
	}
	sc.EnableMouse()
	t.screen = sc

	// TODO: Just for testing purposes.
	t.y = 1
	w, h := t.Size()
	t.width = w - 1
	t.height = h - 2

	go t.translateEvents()
	return nil
}

func (t *UI) Close() error {
	t.screen.Fini()
	return nil
}

func (t *UI) Size() (w, h int) { return t.screen.Size() }

func (t *UI) Flush() {
	col := t.firstCol
	for col != nil {
		col.flush()
		col = col.nextCol
	}
	t.screen.Show()
}

// TODO: This is for temporary reasons. Remove it.
func (t *UI) Push_Mouse_Event(ev mouse.Event) {
	col := t.firstCol
	for col != nil {
		if int(ev.X) < col.x+col.width() {
			col.handleMouseEvent(ev)
			return
		}
		col = col.nextCol
	}
	panic("column not found")
}

func (t *UI) Push_Key_Event(ev key.Event) {
	t.activeText.keyEventHandler(ev)
}

func (t *UI) NewColumn() *Column {
	col := &Column{ui: t}
	if t.firstCol == nil {
		t.firstCol = col
	} else {
		prev := t.lastCol()
		col.x = prev.x + prev.width()*3/5
		prev.nextCol = col
	}
	return col
}

func (t *UI) lastCol() *Column {
	col := t.firstCol
	if col == nil {
		return nil
	}
	for col.nextCol != nil {
		col = col.nextCol
	}
	return col
}

func (t *UI) moveGrabbedCol(x int) {
	gc := t.grabbedCol
	t.grabbedCol = nil

	// TODO: If there are no columns.

	target := t.firstCol
	for target != nil {
		if x >= target.x && x < target.x+target.width() {
			break
		}
{'fraction_non_alphanumeric': 0.15081652257444764, 'fraction_numerical': 0.009029779058597502, 'mean_word_length': 3.3275145469659186, 'pattern_counts': {'":': 0, '<': 7, '<?xml version=': 0, '>': 5, 'https://': 0, 'lorem ipsum': 0, 'www.': 0, 'xml': 0}, 'pii_count': 0, 'substrings_counts': 0, 'word_list_counts': {'cursed_substrings.json': 0, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'dir': 'git-commits-cleaned', 'id': '1949447', 'n_tokens_mistral': 2147, 'n_tokens_neox': 2039, 'n_words': 501}