~adnano/astronaut

703a88cd3e76703ff5309f702e1a78c444db4998 — Adnan Maolood 24 days ago 716e8b7
tab: Refactor cloning
3 files changed, 15 insertions(+), 21 deletions(-)

M browser.go
M command.go
M tab.go
M browser.go => browser.go +5 -2
@@ 81,6 81,7 @@ func NewBrowser(ui *ui.UI, config *Config) *Browser {
		statusView: ui.View(),
		inputView:  ui.View(),
		config:     config,
		tab:        -1,
	}

	b.input.SetView(b.inputView)


@@ 112,7 113,6 @@ func NewBrowser(ui *ui.UI, config *Config) *Browser {

	// Insert new tab
	tab := b.NewTab()
	b.InsertTab(0, tab)
	tab.Do(&gemini.Request{
		URL: &url.URL{
			Scheme: "about",


@@ 563,11 563,14 @@ func (b *Browser) Next() {

// NewTab returns a new tab.
func (b *Browser) NewTab() *Tab {
	return &Tab{
	tab := &Tab{
		view:    b.tabView,
		status:  b.statusView,
		browser: b,
	}
	b.tab++
	b.InsertTab(b.tab, tab)
	return tab
}

// InsertTab inserts a tab at the position i.

M command.go => command.go +3 -6
@@ 53,8 53,6 @@ var commands = map[string]Command{
	},
	"newtab": func(b *Browser, args ...string) error {
		tab := b.NewTab()
		b.tab++
		b.InsertTab(b.tab, tab)
		tab.Do(&gemini.Request{
			URL: &url.URL{
				Scheme: "about",


@@ 65,10 63,9 @@ var commands = map[string]Command{
		return nil
	},
	"clone": func(b *Browser, args ...string) error {
		t := b.tabs[b.tab]
		clone := t.Clone()
		b.tab++
		b.InsertTab(b.tab, clone)
		tab := b.tabs[b.tab]
		clone := b.NewTab()
		clone.Clone(tab)
		clone.Reload()
		b.makeTabVisible()
		return nil

M tab.go => tab.go +7 -13
@@ 146,23 146,17 @@ func (t *Tab) setText(text *Text) {
	}
}

// Clone returns a new tab with the same URL and navigation history.
func (t *Tab) Clone() *Tab {
// Clone clones the given tab.
func (t *Tab) Clone(other *Tab) {
	t.mu.RLock()
	defer t.mu.RUnlock()
	clone := &Tab{
		view:    t.view,
		status:  t.status,
		pages:   make([]Page, len(t.pages)),
		page:    t.page,
		browser: t.browser,
	}
	for i := range clone.pages {
		clone.pages[i] = Page{
			URL: t.pages[i].URL,
	t.pages = make([]Page, len(other.pages))
	t.page = other.page
	for i := range t.pages {
		t.pages[i] = Page{
			URL: other.pages[i].URL,
		}
	}
	return clone
}

// Busy reports whether the tab is currently performing a request.