8000 feat(terminal): show file links in the file panel by Sparky579 · Pull Request #402 · t8y2/motty · GitHub
[go: up one dir, main page]

Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 2 additions & 0 deletions internal/i18n/chinese.go
Original file line number Diff line number Diff line change
Expand Up @@ -1550,6 +1550,7 @@ var chineseTools = ToolsMessages{
FilesSearchUnavailableFmt: "无法搜索子文件夹:%v",
FilesSearchSearching: "正在搜索…",
FilesSearchNoMatches: "此文件夹及其子文件夹中没有匹配项",
FilesRevealMissingFmt: "此文件夹中没有 %s",
FilesKeepTimes: "保留修改时间",
FilesKeepTimesDescription: "传输后的文件沿用原文件的修改时间。权限始终保留。",
FilesFollow: "跟随终端所在目录",
Expand Down Expand Up @@ -1700,6 +1701,7 @@ var chineseTools = ToolsMessages{
ActionFilesFmt: "在文件面板中显示 %s",
ActionExtractFmt: "解压 %s",
ActionCopyFmt: "复制 %s",
ActionDownloadFmt: "下载 %s",
DiagnosticsRow: "诊断报告",
DiagnosticsDescription: "把版本、设置和最近日志存成文本,用于问题反馈。主机和账号已隐藏。",
DiagnosticsExport: "导出",
Expand Down
2 changes: 2 additions & 0 deletions internal/i18n/english.go
Original file line number Diff line number Diff line change
Expand Up @@ -1550,6 +1550,7 @@ var englishTools = ToolsMessages{
FilesSearchUnavailableFmt: "Cannot search the subfolders: %v",
FilesSearchSearching: "Searching…",
FilesSearchNoMatches: "No matches in this folder or its subfolders",
FilesRevealMissingFmt: "%s is not in this folder",
FilesKeepTimes: "Keep modification times",
FilesKeepTimesDescription: "A copied file gets the time of the original. Permissions are always kept.",
FilesFollow: "Follow the terminal's folder",
Expand Down Expand Up @@ -1700,6 +1701,7 @@ var englishTools = ToolsMessages{
ActionFilesFmt: "Show %s in Files",
ActionExtractFmt: "Unpack %s",
ActionCopyFmt: "Copy %s",
ActionDownloadFmt: "Download %s",
DiagnosticsRow: "Diagnostics report",
DiagnosticsDescription: "Save the version, settings and recent log as text for a bug report. Hosts and accounts are hidden.",
DiagnosticsExport: "Export",
Expand Down
2 changes: 2 additions & 0 deletions internal/i18n/terminal_tools.go
Original file line number Diff line number Diff line change
Expand Up @@ -329,6 +329,7 @@ type ToolsMessages struct {
FilesSearchUnavailableFmt string // the error
FilesSearchSearching string
FilesSearchNoMatches string
FilesRevealMissingFmt string
FilesKeepTimes string
FilesKeepTimesDescription string
FilesFollow string
Expand Down Expand Up @@ -479,6 +480,7 @@ type ToolsMessages struct {
ActionFilesFmt string
ActionExtractFmt string
ActionCopyFmt string
ActionDownloadFmt string
DiagnosticsRow string
DiagnosticsDescription string
DiagnosticsExport string
Expand Down
106 changes: 106 additions & 0 deletions internal/term/selection.go
8000
Original file line number Diff line number Diff line change
Expand Up @@ -243,3 +243,109 @@ func (t *Terminal) TokenAt(position Position) string {
text = strings.TrimLeft(text, "([{<\"'`")
return strings.TrimRight(text, ")]}>\"'`,;:!?.")
}

// maxUnderlinedRun bounds how many cells UnderlinedTextAt walks either way.
const maxUnderlinedRun = 1024

// UnderlinedTextAt returns the run of underlined text in one colour around a
// visible cell: how a program marks a link it could not make a real one, as a
// coding agent that thinks the terminal has no hyperlinks still underlines the
// file name it would have linked. Unlike TokenAt the run may hold spaces, and it
// follows soft wraps onto the rows before and after. It is empty when the cell
// is not underlined.
func (t *Terminal) UnderlinedTextAt(position Position) string {
position = t.clampPosition(position)
rows := make(map[int][]Cell, 3)
cells := func(row int) []Cell {
found, ok := rows[row]
if !ok {
found = t.Row(row).Cells
rows[row] = found
}
return found
}
start := cells(position.Row)
if position.Column >= len(start) {
return ""
}
mark := start[position.Column]
if mark.Attr&WideTail != 0 && position.Column > 0 {
mark = start[position.Column-1]
}
if mark.Attr&Underline == 0 || mark.R == 0 {
return ""
}
// A wide character's tail holds no character of its own and goes with it.
same := func(at Position) bool {
row := cells(at.Row)
if at.Column < 0 || at.Column >= len(row) {
return false
}
cell := row[at.Column]
return cell.Attr&WideTail != 0 || cell.R != 0 && cell.Attr&Underline != 0 && cell.FG == mark.FG
}
// A row that wraps early, before a wide character that did not fit, ends
// in blank cells; the run goes on after its last character.
lastCell := func(row int) int {
line := cells(row)
for column := min(len(line), t.cols) - 1; column >= 0; column-- {
if line[column].R != 0 || line[column].Attr&WideTail != 0 {
return column
}
}
return -1
}
wraps := func(row int) bool { return t.Row(row).Wrapped }
first, last := position, position
for steps := 0; steps < maxUnderlinedRun; steps++ {
previous := Position{Row: first.Row, Column: first.Column - 1}
if previous.Column < 0 {
if previous.Row == 0 || !wraps(previous.Row-1) {
break
}
previous = Position{Row: previous.Row - 1, Column: lastCell(previous.Row - 1)}
}
if !same(previous) {
break
}
first = previous
}
for steps := 0; steps < maxUnderlinedRun; steps++ {
next := Position{Row: last.Row, Column: last.Column + 1}
if next.Column >= t.cols || next.Column > lastCell(last.Row) && wraps(last.Row) {
if next.Row+1 >= t.rows || !wraps(last.Row) {
break
}
next = Position{Row: next.Row + 1, Column: 0}
}
if !same(next) {
break
}
last = next
}
// The text is read from the cells rather than as a selection would copy it,
// which drops the blanks at the end of a wrapped row: a space where the row
// wrapped is still part of a name.
var text strings.Builder
for row := first.Row; row <= last.Row; row++ {
from, to := 0, lastCell(row)
if row == first.Row {
from = first.Column
}
if row == last.Row {
to = last.Column
}
marks := t.Row(row).Marks
for column, line := from, cells(row); column <= to && column < len(line); column++ {
if cell := line[column]; cell.Attr&WideTail == 0 && cell.R != 0 {
text.WriteRune(cell.R)
}
for _, mark := range marks {
if int(mark.Col) == column {
text.WriteRune(mark.R)
}
}
}
}
return strings.TrimSpace(text.String())
}
24 changes: 24 additions & 0 deletions internal/term/terminal_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -1002,6 +1002,30 @@ func TestTokenAtIsTheRunOfTextUnderACellWithoutWrappingPunctuation(t *testing.T)
}
}

func TestUnderlinedTextAtIsTheUnderlinedRunInOneColourAcrossWraps(t *testing.T) {
screen := New(20, 4, 0)
// An underlined name with spaces and wide characters, soft-wrapped onto the
// next row, then text in the same colour that is not underlined, then an
// underlined word in another colour right after it.
screen.Write([]byte("Done: \x1b[4;34mWorld Models 总结.md\x1b[24m。\x1b[4;31mnext\x1b[0m\r\n"))
for _, test := range []struct {
row, column int
want string
}{
{0, 6, "World Models 总结.md"}, // its first cell
{0, 12, "World Models 总结.md"}, // the space inside it
{1, 0, "World Models 总结.md"}, // past the wrap
{0, 0, ""}, // not underlined
} {
if got := screen.UnderlinedTextAt(Position{Row: test.row, Column: test.column}); got != test.want {
t.Errorf("UnderlinedTextAt(%d, %d) = %q, want %q", test.row, test.column, got, test.want)
}
}
if got := screen.UnderlinedTextAt(Position{Row: 1, Column: 10}); got != "next" {
t.Errorf("a run in another colour = %q", got)
}
}

func TestALongPlainASCIITextRunKeepsItsCellsAndColor(t *testing.T) {
term := New(200, 3, 0)
text := strings.Repeat("x", 150)
Expand Down
4 changes: 4 additions & 0 deletions internal/workbench/sftp.go
Original file line number Diff line number Diff line change
Expand Up @@ -179,6 +179,9 @@ type sftpView struct {
terminalPath string // an explicit navigation, never a shell report or background refresh
terminalPromptVersion uint64
terminalSyncSkipped bool

// revealed is what the latest reveal came to, until settleReveal reads it.
revealed sftpRevealed
}

// sftpListing is one folder as it was last read.
Expand Down Expand Up @@ -895,6 +898,7 @@ func (state *State) applySFTPListingNavigation(view *sftpView, remotePath string
if nav.id != 0 && nav.id != view.navigation.id {
return
}
defer state.settleReveal(view)
place, restore := view.sftpFolder.acceptNavigation(remotePath, nav)
view.acceptedListID = listID
view.preparingID = 0
Expand Down
7 changes: 5 additions & 2 deletions internal/workbench/sftp_filter.go
Original file line number Diff line number Diff line change
Expand Up @@ -38,8 +38,11 @@ func (folder *sftpFolder) filterInputEditor() *text.Editor {
return &folder.filterEditor
}

func (folder *sftpFolder) clearFilter() {
folder.filterEditor = *text.NewEditor("")
func (folder *sftpFolder) clearFilter() { folder.setFilter("") }

// setFilter puts a query in the folder's filter.
func (folder *sftpFolder) setFilter(query string) {
folder.filterEditor = *text.NewEditor(query)
folder.filterEditor.Restrict(sftpFilterRune, sftpFilterMaxRunes)
folder.filterReady = true
}
Expand Down
4 changes: 3 additions & 1 deletion internal/workbench/sftp_history.go
Original file line number Diff line number Diff line change
Expand Up @@ -186,7 +186,9 @@ func (state *State) finishSFTPNavigation(view *sftpView, local bool, place sftpP
if restore {
folder.restorePlace(place)
}
folder.applyReveal(nav.reveal)
if landed, found := folder.applyReveal(nav.reveal); landed {
view.revealed = sftpRevealed{sftpReveal: nav.reveal, found: found, local: local}
}
if w := view.detached; w != nil {
side := sideIndex(local)
// Same-path refresh keeps its precise offset; navigation restores a place.
Expand Down
1 change: 1 addition & 0 deletions internal/workbench/sftp_local.go
Original file line number Diff line number Diff line change
Expand Up @@ -302,6 +302,7 @@ func (state *State) applyLocalListing(view *sftpView, listing localListing, seq
if listing.navigation.id != 0 && listing.navigation.id != view.lfolder.navigation.id {
return
}
defer state.settleReveal(view)
if view.detached != nil {
view.detached.pendingLocal = ""
}
Expand Down
2 changes: 1 addition & 1 deletion internal/workbench/sftp_panel_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -182,7 +182,7 @@ func TestSFTPToolbarButtonTogglesPanel(t *testing.T) {
func TestShowInFilesKeepsAnOpenPanelOpen(t *testing.T) {
state, session, _, _ := openTestSFTP(t)
waitSFTP(t, state, session, func(view *sftpView) bool { return !view.busy && view.path == "/home/test" })
state.showInFiles(session, "/home/test/docs/guide.md")
state.showInFiles(session, "/home/test/docs/guide.md", sftpReveal{})
waitSFTP(t, state, session, func(view *sftpView) bool { return !view.busy && view.path == "/home/test/docs" })
if context, _ := state.deck.Context(state.deck.ActiveWorkspace()); !context.Open || context.Kind != layoutcore.ContextSFTP {
t.Fatalf("showing a folder closed the panel: %+v", context)
Expand Down
66 changes: 55 additions & 11 deletions internal/workbench/sftp_search.go
Original file line number Diff line number Diff line change
Expand Up @@ -384,33 +384,77 @@ func (state *State) revealSFTPSearchResult(view *sftpView, folder *sftpFolder, i
}
// The result is in another folder; the search is over, and a new one starts
// from wherever the person goes next.
state.revealSFTPEntry(view, folder, sftpReveal{dir: target, name: name})
}

// revealSFTPEntry goes to the folder of a reveal and selects its entry once the
// folder is listed. The filter is cleared first: what it hid could be the entry.
func (state *State) revealSFTPEntry(view *sftpView, folder *sftpFolder, reveal sftpReveal) {
folder.clearFilter()
stopSFTPSearch(folder)
state.applySFTPFilter(view, folder, false)
folder.reveal = sftpReveal{dir: target, name: name}
if local {
_ = state.navigateLocalDirectory(view, target)
folder.reveal = reveal
if folder == &view.lfolder {
_ = state.navigateLocalDirectory(view, reveal.dir)
} else {
state.navigateSFTPDirectory(view, target)
state.navigateSFTPDirectory(view, reveal.dir)
}
// A navigation that was not started leaves nothing behind to claim it.
folder.reveal = sftpReveal{}
}

// sftpReveal is the entry to select once the folder a result led to is listed.
// sftpReveal is the entry to select once the folder a search result or a
// terminal link led to is listed. download asks for it to be downloaded then;
// search, for a name that may be in a folder further down, asks for the
// subfolders to be searched for it when it is not in this one.
type sftpReveal struct {
dir, name string
dir, name string
download, search bool
}

// applyReveal selects the entry a search result led to, when the listing that
// arrived is the folder it is in, and brings it into view.
func (folder *sftpFolder) applyReveal(reveal sftpReveal) {
// sftpRevealed is what a reveal came to when its folder was listed, kept until
// the listing has written the panel's status (see settleReveal).
type sftpRevealed struct {
sftpReveal
found, local bool
}

// applyReveal selects the entry a reveal is for, when the listing that arrived
// is the folder it is in, and brings it into view. It reports whether the
// listing was that folder, and whether the entry was in it.
func (folder *sftpFolder) applyReveal(reveal sftpReveal) (landed, found bool) {
if reveal.name == "" || reveal.dir != folder.path {
return
return false, false
}
index := sftpRowNameIndex(folder.rows, reveal.name)
if index < 0 {
return
return true, false
}
folder.restorePlace(sftpPlace{current: reveal.name, anchor: reveal.name, selected: []string{reveal.name}, first: index - folder.visibleRows/2})
return true, true
}

// settleReveal finishes a reveal once its listing has written the panel's
// status: it says so when the entry was not in its folder, and starts the
// download that was asked for, as the panel's Download would.
func (state *State) settleReveal(view *sftpView) {
revealed := view.revealed
if revealed.name == "" {
return
}
view.revealed = sftpRevealed{}
folder := &view.sftpFolder
if revealed.local {
folder = &view.lfolder
}
switch {
case !revealed.found && revealed.search && sftpSearchAvailable(view, revealed.local):
folder.setFilter(revealed.name)
folder.filterOpen, folder.deep = true, true
state.applySFTPFilter(view, folder, false)
case !revealed.found:
view.status = fmt.Sprintf(state.messages.Tools.FilesRevealMissingFmt, revealed.name)
case revealed.download && !revealed.local:
state.downloadSFTP(view)
}
}
4 changes: 2 additions & 2 deletions internal/workbench/terminal.go
Original file line number Diff line number Diff line change
Expand Up @@ -275,15 +275,15 @@ func (state *State) configureTerminal(session *Session) {
session.term.resetFolderSync()
}
session.term.screen.SetPromptToken(session.term.folderToken)
session.term.openLink = state.openTerminalLink
sessionID := session.ID
session.term.openLink = func(target string) { state.openSessionLink(sessionID, target) }
session.term.clock = state.now
// An image decoded off the window thread asks for the frame that shows it.
session.term.screen.SetImageWake(state.options.Invalidate)
state.attachHistory(session)
state.attachCompletion(session)
state.stampTerminal(session.term)
state.reportTerminalColors(session.term)
sessionID := session.ID
session.term.screen.SetSyncHold(func(on bool) {
if current, ok := state.sessionByIDPointer(sessionID); ok && current.term != nil {
current.term.holdSync(on)
Expand Down
17 changes: 12 additions & 5 deletions internal/workbench/terminal_action.go
Original file line number Diff line number Diff line change
Expand Up @@ -17,18 +17,22 @@ import (
type actionKind uint8

const (
actionSSH actionKind = iota // connect to Target
actionBrowser // open Target, a web address, in the browser
actionCopy // copy Target
actionFiles // show the folder of Target in the file browser
actionExtract // type the command that unpacks Target
actionSSH actionKind = iota // connect to Target
actionBrowser // open Target, a web address, in the browser
actionCopy // copy Target
actionFiles // show Target, selected in its folder, in the file browser
actionExtract // type the command that unpacks Target
actionDownload // download Target, a file on the server
)

// detected is one thing the menu offers for the text under the pointer.
type detected struct {
kind actionKind
target string // what the action works on
label string // what the menu shows of it: the text as the user sees it
// search, for actionFiles, says the path is a guess: when it is not in its
// folder, the folder's subfolders are searched for its name.
search bool
}

var (
Expand Down Expand Up @@ -172,6 +176,9 @@ func classifyFile(token string) []detected {
}
if strings.HasPrefix(filename, "/") && len(filename) > 1 {
found = append(found, detected{kind: actionFiles, target: filename, label: filename})
if !strings.HasSuffix(filename, "/") {
found = append(found, detected{kind: actionDownload, target: filename, label: path.Base(filename)})
}
}
if (absolute && len(filename) > 1 || relative && len(filename) > 2) || len(found) > 0 {
found = append(found, detected{kind: actionCopy, target: filename, label: filename})
Expand Down
Loading
Loading
0