File: tab.go

package info (click to toggle)
amfora 1.11.0-1
  • links: PTS, VCS
  • area: main
  • in suites: forky, sid
  • size: 3,272 kB
  • sloc: python: 71; sh: 42; makefile: 39
file content (582 lines) | stat: -rw-r--r-- 16,190 bytes parent folder | download
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
package display

import (
	"fmt"
	"net/url"
	"path"
	"strconv"
	"strings"

	"code.rocketnine.space/tslocum/cview"
	"github.com/atotto/clipboard"
	"github.com/gdamore/tcell/v2"
	"github.com/makeworld-the-better-one/amfora/config"
	"github.com/makeworld-the-better-one/amfora/structs"
)

type tabMode int

const (
	tabModeDone tabMode = iota
	tabModeLoading
	tabModeSearch
)

// tabHistoryPageCache is fields from the Page struct, cached here to solve #122
// See structs/structs.go for an explanation of the fields.
type tabHistoryPageCache struct {
	row        int
	column     int
	selected   string
	selectedID string
	mode       structs.PageMode
}

type tabHistory struct {
	urls      []string
	pos       int // Position: where in the list of URLs we are
	pageCache []*tabHistoryPageCache
}

// tab hold the information needed for each browser tab.
type tab struct {
	page             *structs.Page
	view             *cview.TextView
	history          *tabHistory
	mode             tabMode
	barLabel         string // The bottomBar label for the tab
	barText          string // The bottomBar text for the tab
	preferURLHandler bool   // For #143, use URL handler over proxy
}

// makeNewTab initializes an tab struct with no content.
func makeNewTab() *tab {
	t := tab{
		page:    &structs.Page{Mode: structs.ModeOff},
		view:    cview.NewTextView(),
		history: &tabHistory{},
		mode:    tabModeDone,
	}
	t.view.SetDynamicColors(true)
	t.view.SetRegions(true)
	t.view.SetScrollable(true)
	t.view.SetWrap(false)
	t.view.SetScrollBarVisibility(config.ScrollBar)
	t.view.SetScrollBarColor(config.GetColor("scrollbar"))
	t.view.SetChangedFunc(func() {
		App.Draw()
	})
	t.view.SetDoneFunc(func(key tcell.Key) {
		// Altered from:
		// https://gitlab.com/tslocum/cview/-/blob/1f765c8695c3f4b35dae57f469d3aee0b1adbde7/demos/textview/main.go
		// Handles being able to select and "click" links with the enter and tab keys

		tab := curTab // Don't let it change in the middle of the code

		if tabs[tab].mode != tabModeDone {
			return
		}

		if key == tcell.KeyEsc {
			// Stop highlighting
			bottomBar.SetLabel("")
			bottomBar.SetText(tabs[tab].page.URL)
			tabs[tab].clearSelected()
			tabs[tab].saveBottomBar()
			return
		}

		if len(tabs[tab].page.Links) == 0 {
			// No links on page
			return
		}

		currentSelection := tabs[tab].view.GetHighlights()
		numSelections := len(tabs[tab].page.Links)

		if key == tcell.KeyEnter && len(currentSelection) > 0 {
			// A link is selected and enter was pressed: "click" it and load the page it's for
			bottomBar.SetLabel("")
			linkN, _ := strconv.Atoi(currentSelection[0])
			tabs[tab].page.Selected = tabs[tab].page.Links[linkN]
			tabs[tab].page.SelectedID = currentSelection[0]
			tabs[tab].preferURLHandler = false // Reset in case
			go followLink(tabs[tab], tabs[tab].page.URL, tabs[tab].page.Links[linkN])
			return
		}
		if len(currentSelection) == 0 && (key == tcell.KeyEnter || key == tcell.KeyTab) {
			// They've started link highlighting
			tabs[tab].page.Mode = structs.ModeLinkSelect

			tabs[tab].view.Highlight("0")
			tabs[tab].scrollToHighlight()
			// Display link URL in bottomBar
			bottomBar.SetLabel("[::b]Link: [::-]")
			bottomBar.SetText(tabs[tab].page.Links[0])
			tabs[tab].saveBottomBar()
			tabs[tab].page.Selected = tabs[tab].page.Links[0]
			tabs[tab].page.SelectedID = "0"
		}

		if len(currentSelection) > 0 {
			// There's still a selection, but a different key was pressed, not Enter

			index, _ := strconv.Atoi(currentSelection[0])
			if key == tcell.KeyTab {
				index = (index + 1) % numSelections
			} else if key == tcell.KeyBacktab {
				index = (index - 1 + numSelections) % numSelections
			} else {
				return
			}
			tabs[tab].view.Highlight(strconv.Itoa(index))
			tabs[tab].scrollToHighlight()
			// Display link URL in bottomBar
			bottomBar.SetLabel("[::b]Link: [::-]")
			bottomBar.SetText(tabs[tab].page.Links[index])
			tabs[tab].saveBottomBar()
			tabs[tab].page.Selected = tabs[tab].page.Links[index]
			tabs[tab].page.SelectedID = strconv.Itoa(index)
		}
	})
	t.view.SetInputCapture(func(event *tcell.EventKey) *tcell.EventKey {
		// Capture scrolling and change the left margin size accordingly, see #197
		// This was also touched by #222
		// This also captures any tab-specific events now

		if t.mode != tabModeDone {
			// Any events that should be caught when the tab is loading is handled in display.go
			return nil
		}

		cmd := config.TranslateKeyEvent(event)

		// Cmds that aren't single row/column scrolling
		//nolint:exhaustive
		switch cmd {
		case config.CmdBookmarks:
			Bookmarks(&t)
			t.addToHistory("about:bookmarks")
			return nil
		case config.CmdAddBookmark:
			go addBookmark()
			return nil
		case config.CmdPgup:
			t.pageUp()
			return nil
		case config.CmdPgdn:
			t.pageDown()
			return nil
		case config.CmdSave:
			if t.hasContent() {
				savePath, err := downloadPage(t.page)
				if err != nil {
					go Error("Download Error", fmt.Sprintf("Error saving page content: %v", err))
				} else {
					go Info(fmt.Sprintf("Page content saved to %s. ", savePath))
				}
			} else {
				go Info("The current page has no content, so it couldn't be downloaded.")
			}
			return nil
		case config.CmdBack:
			histBack(&t)
			return nil
		case config.CmdForward:
			histForward(&t)
			return nil
		case config.CmdSub:
			Subscriptions(&t, "about:subscriptions")
			tabs[curTab].addToHistory("about:subscriptions")
			return nil
		case config.CmdCopyPageURL:
			currentURL := tabs[curTab].page.URL
			err := clipboard.WriteAll(currentURL)
			if err != nil {
				go Error("Copy Error", err.Error())
				return nil
			}
			return nil
		case config.CmdCopyTargetURL:
			currentURL := t.page.URL
			selectedURL := t.highlightedURL()
			if selectedURL == "" {
				return nil
			}
			u, _ := url.Parse(currentURL)
			copiedURL, err := u.Parse(selectedURL)
			if err != nil {
				err := clipboard.WriteAll(selectedURL)
				if err != nil {
					go Error("Copy Error", err.Error())
					return nil
				}
				return nil
			}
			err = clipboard.WriteAll(copiedURL.String())
			if err != nil {
				go Error("Copy Error", err.Error())
				return nil
			}
			return nil
		case config.CmdURLHandlerOpen:
			currentSelection := t.view.GetHighlights()
			t.preferURLHandler = true
			// Copied code from when enter key is pressed
			if len(currentSelection) > 0 {
				bottomBar.SetLabel("")
				linkN, _ := strconv.Atoi(currentSelection[0])
				t.page.Selected = t.page.Links[linkN]
				t.page.SelectedID = currentSelection[0]
				go followLink(&t, t.page.URL, t.page.Links[linkN])
			}
			return nil
		}
		// Number key: 1-9, 0, LINK1-LINK10
		if cmd >= config.CmdLink1 && cmd <= config.CmdLink0 {
			if int(cmd) <= len(t.page.Links) {
				// It's a valid link number
				t.preferURLHandler = false // Reset in case
				go followLink(&t, t.page.URL, t.page.Links[cmd-1])
				return nil
			}
		}
		// Command key: 1-9, 0, COMMAND1
		if cmd >= config.CmdCommand1 && cmd <= config.CmdCommand0 {
			commandNum := int(cmd - config.CmdCommand1)
			tabURL := tabs[curTab].page.URL
			CustomCommand(commandNum, tabURL)
			return nil
		}
		// Target link command key: 1-9, 0, COMMANDTARGET1-COMMANDTARGET10
		if cmd >= config.CmdCommandTarget1 && cmd <= config.CmdCommandTarget0 {
			commandNum := int(cmd - config.CmdCommandTarget1)
			currentURL := t.page.URL
			selectedURL := t.highlightedURL()

			if selectedURL == "" {
				return nil
			}
			u, _ := url.Parse(currentURL)
			parsedURL, err := u.Parse(selectedURL)
			if err == nil {
				CustomCommand(commandNum, parsedURL.String())
			}
			return nil
		}

		// Scrolling stuff
		// Copied in scrollTo

		key := event.Key()
		mod := event.Modifiers()
		height, width := t.view.GetBufferSize()
		_, _, boxW, boxH := t.view.GetInnerRect()

		// Make boxW accurate by subtracting one if a scrollbar is covering the last
		// column of text
		if config.ScrollBar == cview.ScrollBarAlways ||
			(config.ScrollBar == cview.ScrollBarAuto && height > boxH) {
			boxW--
		}

		if cmd == config.CmdMoveRight || (key == tcell.KeyRight && mod == tcell.ModNone) {
			// Scrolling to the right

			if t.page.Column >= leftMargin() {
				// Scrolled right far enough that no left margin is needed
				if (t.page.Column-leftMargin())+boxW >= width {
					// And scrolled as far as possible to the right
					return nil
				}
			} else {
				// Left margin still exists
				if boxW-(leftMargin()-t.page.Column) >= width {
					// But still scrolled as far as possible
					return nil
				}
			}
			t.page.Column++
		} else if cmd == config.CmdMoveLeft || (key == tcell.KeyLeft && mod == tcell.ModNone) {
			// Scrolling to the left
			if t.page.Column == 0 {
				// Can't scroll to the left anymore
				return nil
			}
			t.page.Column--
		} else if cmd == config.CmdMoveUp || (key == tcell.KeyUp && mod == tcell.ModNone) {
			// Scrolling up
			if t.page.Row > 0 {
				t.page.Row--
			}
			return event
		} else if cmd == config.CmdMoveDown || (key == tcell.KeyDown && mod == tcell.ModNone) {
			// Scrolling down
			if t.page.Row < height {
				t.page.Row++
			}
			return event
		} else if cmd == config.CmdBeginning {
			t.page.Row = 0
			// This is required because cview will also set the column (incorrectly)
			// if it handles this event itself
			t.applyScroll()
			App.Draw()
			return nil
		} else if cmd == config.CmdEnd {
			t.page.Row = height
			t.applyScroll()
			App.Draw()
			return nil
		} else {
			// Some other key, stop processing it
			return event
		}

		t.applyHorizontalScroll()
		App.Draw()
		return nil
	})

	return &t
}

// historyCachePage caches certain info about the current page in the tab's history,
// see #122 for details.
func (t *tab) historyCachePage() {
	if t.page == nil || t.page.URL == "" || t.history.pageCache == nil || len(t.history.pageCache) == 0 {
		return
	}
	t.history.pageCache[t.history.pos] = &tabHistoryPageCache{
		row:        t.page.Row,
		column:     t.page.Column,
		selected:   t.page.Selected,
		selectedID: t.page.SelectedID,
		mode:       t.page.Mode,
	}
}

// addToHistory adds the given URL to history.
// It assumes the URL is currently being loaded and displayed on the page.
func (t *tab) addToHistory(u string) {
	if t.history.pos < len(t.history.urls)-1 {
		// We're somewhere in the middle of the history instead, with URLs ahead and behind.
		// The URLs ahead need to be removed so this new URL is the most recent item in the history
		t.history.urls = t.history.urls[:t.history.pos+1]
		// Same for page cache
		t.history.pageCache = t.history.pageCache[:t.history.pos+1]
	}
	t.history.urls = append(t.history.urls, u)
	t.history.pos++

	// Cache page info for #122
	t.history.pageCache = append(t.history.pageCache, &tabHistoryPageCache{}) // Add new spot
	t.historyCachePage()                                                      // Fill it with data
}

// pageUp scrolls up 75% of the height of the terminal, like Bombadillo.
func (t *tab) pageUp() {
	t.page.Row -= termH / 2
	if t.page.Row < 0 {
		t.page.Row = 0
	}
	t.applyScroll()
}

// pageDown scrolls down 75% of the height of the terminal, like Bombadillo.
func (t *tab) pageDown() {
	height, _ := t.view.GetBufferSize()

	t.page.Row += termH / 2
	if t.page.Row > height {
		t.page.Row = height
	}

	t.applyScroll()
}

// hasContent returns false when the tab's page is malformed,
// has no content or URL.
func (t *tab) hasContent() bool {
	if t.page == nil || t.view == nil {
		return false
	}
	if t.page.URL == "" {
		return false
	}
	if t.page.Content == "" {
		return false
	}
	return true
}

// isAnAboutPage returns true when the tab's page is an about page
func (t *tab) isAnAboutPage() bool {
	return strings.HasPrefix(t.page.URL, "about:")
}

// applyHorizontalScroll handles horizontal scroll logic including left margin resizing,
// see #197 for details. Use applyScroll instead.
//
// In certain cases it will still use and apply the saved Row.
func (t *tab) applyHorizontalScroll() {
	i := tabNumber(t)
	if i == -1 {
		// Tab is not actually being used and should not be (re)added to the browser
		return
	}
	if t.page.Column >= leftMargin() {
		// Scrolled to the right far enough that no left margin is needed
		browser.AddTab(
			strconv.Itoa(i),
			t.label(),
			makeContentLayout(t.view, 0),
		)
		t.view.ScrollTo(t.page.Row, t.page.Column-leftMargin())
	} else {
		// Left margin is still needed, but is not necessarily at the right size by default
		browser.AddTab(
			strconv.Itoa(i),
			t.label(),
			makeContentLayout(t.view, leftMargin()-t.page.Column),
		)
	}
}

// applyScroll applies the saved scroll values to the page and tab.
// It should only be used when going backward and forward.
func (t *tab) applyScroll() {
	t.view.ScrollTo(t.page.Row, 0)
	t.applyHorizontalScroll()
}

// scrollTo scrolls the current tab to specified position. Like
// cview.TextView.ScrollTo but using the custom scrolling logic required by #196.
func (t *tab) scrollTo(row, col int) {
	height, width := t.view.GetBufferSize()

	// Keep row and col within limits

	if row < 0 {
		row = 0
	} else if row > height {
		row = height
	}
	if col < 0 {
		col = 0
	} else if col > width {
		col = width
	}

	t.page.Row = row
	t.page.Column = col
	t.applyScroll()
	App.Draw()
}

// scrollToHighlight scrolls the current tab to specified position. Like
// cview.TextView.ScrollToHighlight but using the custom scrolling logic
// required by #196.
func (t *tab) scrollToHighlight() {
	t.view.ScrollToHighlight()
	App.Draw()
	t.scrollTo(t.view.GetScrollOffset())
}

// saveBottomBar saves the current bottomBar values in the tab.
func (t *tab) saveBottomBar() {
	t.barLabel = bottomBar.GetLabel()
	t.barText = bottomBar.GetText()
}

// applyBottomBar sets the bottomBar using the stored tab values
func (t *tab) applyBottomBar() {
	bottomBar.SetLabel(t.barLabel)
	bottomBar.SetText(t.barText)
}

// clearSelected turns off any selection that was going on.
// It does not affect the bottomBar.
func (t *tab) clearSelected() {
	t.page.Mode = structs.ModeOff
	t.page.Selected = ""
	t.page.SelectedID = ""
	t.view.Highlight("")
}

// applySelected selects whatever is stored as the selected element in the struct,
// and sets the mode accordingly.
// It is safe to call if nothing was selected previously.
//
// applyBottomBar should be called after, as this func might set some bottomBar values.
func (t *tab) applySelected() {
	if t.page.Mode == structs.ModeOff {
		// Just in case
		t.page.Selected = ""
		t.page.SelectedID = ""
		t.view.Highlight("")
		return
	} else if t.page.Mode == structs.ModeLinkSelect {
		t.view.Highlight(t.page.SelectedID)

		if t.mode == tabModeDone {
			// Page is not loading so bottomBar can change
			t.barLabel = "[::b]Link: [::-]"
			t.barText = t.page.Selected
		}
	}
}

// applyAll uses applyScroll and applySelected to put a tab's TextView back the way it was.
// It also uses applyBottomBar if this is the current tab.
func (t *tab) applyAll() {
	t.applySelected()
	t.applyScroll()
	if t == tabs[curTab] {
		t.applyBottomBar()
	}
}

// highlightedURL returns the currently selected URL
func (t *tab) highlightedURL() string {
	currentSelection := tabs[curTab].view.GetHighlights()

	if len(currentSelection) > 0 {
		linkN, _ := strconv.Atoi(currentSelection[0])
		selectedURL := tabs[curTab].page.Links[linkN]
		return selectedURL
	}
	return ""
}

// label returns the label to use for the tab name
func (t *tab) label() string {
	tn := tabNumber(t)
	if tn < 0 {
		// Invalid tab, shouldn't happen
		return ""
	}

	// Increment so there's no tab 0 in the label
	tn++

	if t.page.URL == "" || t.page.URL == "about:newtab" {
		// Just use tab number
		// Spaces around to keep original Amfora look
		return fmt.Sprintf(" %d ", tn)
	}
	if strings.HasPrefix(t.page.URL, "about:") {
		// Don't look for domain, put the whole URL except query strings
		return strings.SplitN(t.page.URL, "?", 2)[0]
	}
	if strings.HasPrefix(t.page.URL, "file://") {
		// File URL, use file or folder as tab name
		return cview.Escape(path.Base(t.page.URL[7:]))
	}
	// Otherwise, it's a Gemini URL
	pu, err := url.Parse(t.page.URL)
	if err != nil {
		return fmt.Sprintf(" %d ", tn)
	}
	return pu.Host
}