-
Notifications
You must be signed in to change notification settings - Fork 5
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
Showing
5 changed files
with
107 additions
and
48 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,95 @@ | ||
package component | ||
|
||
import ( | ||
"github.com/gdamore/tcell/v2" | ||
"github.com/rivo/tview" | ||
) | ||
|
||
type showModalFunc func(tview.Primitive, string) *tview.TextView | ||
|
||
type hideModalFunc func(tview.Primitive) | ||
|
||
const MODAL_TITLE = "Scroll (Ctrl+J, Ctrl+K)" | ||
|
||
func attachModalForTreeAttributes(tree *tview.TreeView, showFn showModalFunc, hideFn hideModalFunc) { | ||
var currentModalNode *tview.TreeNode = nil | ||
tree.SetSelectedFunc(func(node *tview.TreeNode) { | ||
if len(node.GetChildren()) > 0 { | ||
node.SetExpanded(!node.IsExpanded()) | ||
return | ||
} | ||
if currentModalNode == node { | ||
hideFn(tree) | ||
currentModalNode = nil | ||
return | ||
} | ||
textView := showFn(tree, node.GetText()) | ||
textView.SetTitle(MODAL_TITLE) | ||
currentModalNode = node | ||
tree.SetInputCapture(func(event *tcell.EventKey) *tcell.EventKey { | ||
switch event.Key() { | ||
case tcell.KeyCtrlJ: | ||
row, col := textView.GetScrollOffset() | ||
textView.ScrollTo(row+1, col) | ||
return nil | ||
case tcell.KeyCtrlK: | ||
row, col := textView.GetScrollOffset() | ||
textView.ScrollTo(row-1, col) | ||
return nil | ||
} | ||
return event | ||
}) | ||
}) | ||
tree.SetChangedFunc(func(node *tview.TreeNode) { | ||
if currentModalNode != nil { | ||
hideFn(tree) | ||
currentModalNode = nil | ||
} | ||
}) | ||
} | ||
|
||
type tableModalMapper interface { | ||
// GetColumnIdx returns the column index for getting the content to be shown | ||
// in the modal | ||
GetColumnIdx() int | ||
} | ||
|
||
func attachModalForTableRows(table *tview.Table, mapper tableModalMapper, showFn showModalFunc, hideFn hideModalFunc) { | ||
if mapper == nil { | ||
return | ||
} | ||
|
||
var currentRow = -1 | ||
|
||
table.SetSelectedFunc(func(row, column int) { | ||
if currentRow == row { | ||
hideFn(table) | ||
currentRow = -1 | ||
return | ||
} | ||
currentRow = row | ||
if cell := table.GetCell(row, mapper.GetColumnIdx()); cell != nil { | ||
textView := showFn(table, cell.Text) | ||
textView.SetTitle(MODAL_TITLE) | ||
table.SetInputCapture(func(event *tcell.EventKey) *tcell.EventKey { | ||
switch event.Key() { | ||
case tcell.KeyCtrlJ: | ||
row, col := textView.GetScrollOffset() | ||
textView.ScrollTo(row+1, col) | ||
return nil | ||
case tcell.KeyCtrlK: | ||
row, col := textView.GetScrollOffset() | ||
textView.ScrollTo(row-1, col) | ||
return nil | ||
} | ||
return event | ||
}) | ||
} | ||
}) | ||
table.SetSelectionChangedFunc(func(row, column int) { | ||
if currentRow != -1 { | ||
hideFn(table) | ||
currentRow = -1 | ||
} | ||
}) | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file was deleted.
Oops, something went wrong.