Skip to content

Commit

Permalink
feat: add copy to clipboard to sidebar
Browse files Browse the repository at this point in the history
  • Loading branch information
jorgerojas26 committed Dec 3, 2024
1 parent ab19ca5 commit dde61eb
Show file tree
Hide file tree
Showing 4 changed files with 20 additions and 2 deletions.
3 changes: 2 additions & 1 deletion app/Keymap.go
Original file line number Diff line number Diff line change
Expand Up @@ -94,7 +94,7 @@ var Keymaps = KeymapSystem{
Bind{Key: Key{Char: 'b'}, Cmd: cmd.GotoPrev, Description: "Go to previous cell"},
Bind{Key: Key{Char: '$'}, Cmd: cmd.GotoEnd, Description: "Go to last cell"},
Bind{Key: Key{Char: '0'}, Cmd: cmd.GotoStart, Description: "Go to first cell"},
Bind{Key: Key{Char: 'y'}, Cmd: cmd.Copy, Description: "Copy cell to clipboard"},
Bind{Key: Key{Char: 'y'}, Cmd: cmd.Copy, Description: "Copy cell value to clipboard"},
Bind{Key: Key{Char: 'o'}, Cmd: cmd.AppendNewRow, Description: "Append new row"},
Bind{Key: Key{Char: 'J'}, Cmd: cmd.SortDesc, Description: "Sort descending"},
Bind{Key: Key{Char: 'R'}, Cmd: cmd.Refresh, Description: "Refresh the current table"},
Expand Down Expand Up @@ -134,6 +134,7 @@ var Keymaps = KeymapSystem{
Bind{Key: Key{Code: tcell.KeyEnter}, Cmd: cmd.CommitEdit, Description: "Add edit to pending changes"},
Bind{Key: Key{Code: tcell.KeyEscape}, Cmd: cmd.DiscardEdit, Description: "Discard edit"},
Bind{Key: Key{Char: 'C'}, Cmd: cmd.SetValue, Description: "Toggle value menu to put values like NULL, EMPTY or DEFAULT"},
Bind{Key: Key{Char: 'y'}, Cmd: cmd.Copy, Description: "Copy value to clipboard"},
},
},
}
6 changes: 5 additions & 1 deletion components/ResultsTable.go
Original file line number Diff line number Diff line change
Expand Up @@ -234,6 +234,9 @@ func (table *ResultsTable) subscribeToSidebarChanges() {
table.AppendNewChange(models.DmlUpdateType, row, changedColumnIndex, cellValue)

App.ForceDraw()
case eventSidebarError:
errorMessage := stateChange.Value.(string)
table.SetError(errorMessage, nil)
}
}
}
Expand Down Expand Up @@ -952,6 +955,7 @@ func (table *ResultsTable) StartEditingCell(row int, col int, callback func(newV
inputField.SetText(cell.Text)
inputField.SetFieldBackgroundColor(app.Styles.PrimaryTextColor)
inputField.SetFieldTextColor(app.Styles.PrimitiveBackgroundColor)
inputField.SetBorder(true)

inputField.SetDoneFunc(func(key tcell.Key) {
table.SetIsEditing(false)
Expand Down Expand Up @@ -1001,7 +1005,7 @@ func (table *ResultsTable) StartEditingCell(row int, col int, callback func(newV
})

x, y, width := cell.GetLastPosition()
inputField.SetRect(x, y, width+1, 1)
inputField.SetRect(x-1, y-1, width+3, 3)
table.Page.AddPage(pageNameTableEditCell, inputField, false, true)
App.SetFocus(inputField)
}
Expand Down
12 changes: 12 additions & 0 deletions components/Sidebar.go
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,7 @@ import (

"github.com/jorgerojas26/lazysql/app"
"github.com/jorgerojas26/lazysql/commands"
"github.com/jorgerojas26/lazysql/lib"
"github.com/jorgerojas26/lazysql/models"
)

Expand Down Expand Up @@ -289,6 +290,17 @@ func (sidebar *Sidebar) inputCapture(event *tcell.EventKey) *tcell.EventKey {
list.Show(x, y, 30)

return nil
case commands.Copy:
currentItemIndex := sidebar.GetCurrentFieldIndex()
item := sidebar.Flex.GetItem(currentItemIndex).(*tview.TextArea)
text := item.GetText()

clipboard := lib.NewClipboard()

err := clipboard.Write(text)
if err != nil {
sidebar.Publish(models.StateChange{Key: eventSidebarError, Value: err.Error()})
}
}
return event
}
Expand Down
1 change: 1 addition & 0 deletions components/constants.go
Original file line number Diff line number Diff line change
Expand Up @@ -45,6 +45,7 @@ const (
eventSidebarUnfocusing string = "UnfocusingSidebar"
eventSidebarToggling string = "TogglingSidebar"
eventSidebarCommitEditing string = "CommitEditingSidebar"
eventSidebarError string = "ErrorSidebar"

eventSQLEditorQuery string = "Query"
eventSQLEditorEscape string = "Escape"
Expand Down

0 comments on commit dde61eb

Please sign in to comment.