cat / 012
clone

a spreadsheet for the terminal: lotus 1-2-3 looks, google sheets keys and formulas, one go binary

cat/module-path
012 / internal / ui / context.go
context.go 3.1K
1package ui23import (4 tea "charm.land/bubbletea/v2"5)67// Right-click menus follow Sheets: the cell menu offers clipboard, insert8// and delete commands, and the header menus the subset for whole columns9// or rows. Right-clicking outside the selection first moves there, and10// Shift+F10 opens the cell menu from the keyboard. The menus reuse the11// dropdown, so missing commands are hidden the same way.1213var (14 clipboardItems = []menuItem{15 {cmd: "edit.cut"}, {cmd: "edit.copy"}, {cmd: "edit.paste"},16 }17 cellMenu = concat(clipboardItems, []menuItem{18 {cmd: "edit.paste_values", title: "Paste values only"}, sep,19 {cmd: "insert.row_above", title: "Insert row above"}, {cmd: "insert.row_below", title: "Insert row below"},20 {cmd: "insert.col_left", title: "Insert column left"}, {cmd: "insert.col_right", title: "Insert column right"}, sep,21 {cmd: "delete.row", title: "Delete row"}, {cmd: "delete.col", title: "Delete column"}, sep,22 {cmd: "clear"}, sep,23 {cmd: "note.edit", title: "Insert note"}, {cmd: "note.delete", title: "Delete notes"},24 {cmd: "data.protect_range", title: "Protect range"},25 })26 columnMenu = concat(clipboardItems, []menuItem{sep,27 {cmd: "insert.col_left", title: "Insert column left"}, {cmd: "insert.col_right", title: "Insert column right"}, sep,28 {cmd: "delete.col", title: "Delete column"}, {cmd: "clear"}, sep,29 {cmd: "column.width", title: "Resize column"}, {cmd: "column.reset"}, sep,30 {cmd: "data.sort_sheet_az"}, {cmd: "data.sort_sheet_za"}, sep,31 {cmd: "data.filter"}, {cmd: "data.filter_remove"},32 })33 rowMenu = concat(clipboardItems, []menuItem{sep,34 {cmd: "insert.row_above", title: "Insert row above"}, {cmd: "insert.row_below", title: "Insert row below"}, sep,35 {cmd: "delete.row", title: "Delete row"}, {cmd: "clear"}, sep,36 {cmd: "row.height", title: "Resize row"}, {cmd: "row.fit"},37 })38)3940func concat(lists ...[]menuItem) []menuItem {41 var out []menuItem42 for _, l := range lists {43 out = append(out, l...)44 }45 return out46}4748func init() {49 register(&command{id: "menu.context", macro: macroNever, title: "Cell menu", desc: "Open the right-click menu for the selection", run: func(m *Model) tea.Cmd {50 x, y := m.cellPos(m.cur)51 m.showContextMenu(m.cellMenu(), x, y+1)52 return nil53 }})54 keymap["shift+f10"] = "menu.context"55}5657// rightClick opens the menu for what's under the mouse at x, y.58func (m *Model) rightClick(x, y int) {59 h := m.hitTest(x, y)60 sel := m.selection()61 switch h.kind {62 case hitTab, hitTabAdd:63 m.tabRightClick(h, x, y)64 case hitCell, hitCheckbox, hitDropdown:65 if !m.hasRange() || !sel.Contains(h.addr) {66 m.cur = h.addr67 m.clearSelection()68 }69 m.showContextMenu(m.cellMenu(), x, y+1)70 case hitColHeader, hitColBorder, hitFilterButton:71 if m.whole != wholeCols || h.addr.Col < sel.From.Col || h.addr.Col > sel.To.Col {72 m.cur = h.addr73 m.selecting, m.whole, m.ext = true, wholeCols, h.addr74 }75 m.showContextMenu(columnMenu, x, y+1)76 case hitRowHeader:77 if m.whole != wholeRows || h.addr.Row < sel.From.Row || h.addr.Row > sel.To.Row {78 m.cur = h.addr79 m.selecting, m.whole, m.ext = true, wholeRows, h.addr80 }81 m.showContextMenu(rowMenu, x, y+1)82 }83}