Skip to content

Add option to open slide in editor #314

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Open
wants to merge 4 commits into
base: main
Choose a base branch
from
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
72 changes: 72 additions & 0 deletions internal/editor/editor.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,72 @@
package editor

import (
"fmt"
"os"
"os/exec"
"strings"

tea "github.com/charmbracelet/bubbletea"
"github.com/muesli/coral"
)

var program = func() string {
if editor := os.Getenv("EDITOR"); editor != "" {
return editor
}
return "vim"
}()

// Opens the current slide as a split window in tmux.
func OpenNewWindow(fileName string, slide string) tea.Cmd {
var c *exec.Cmd

editorName := GetEditorName(program)

switch editorName {
case "vim", "nvim":
c = exec.Command(program, fmt.Sprintf("+%d", GetLineNumber(fileName, slide)), fileName)
case "code":
c = exec.Command(program, fileName, "--go-to", fmt.Sprintf("+%d", GetLineNumber(fileName, slide)))
default:
c = exec.Command(program, fileName)
}

return tea.ExecProcess(c, nil)
}

func linesMatch(lines []string, sLines []string, start int) bool {
for j := range sLines {
if strings.TrimSpace(lines[start+j]) != strings.TrimSpace(sLines[j]) {
return false
}
}
return true
}

func GetLineNumber(fileName string, slide string) int {
b, err := os.ReadFile(fileName)
if err != nil {
return -1
}

lines := strings.Split(string(b), "\n")
sLines := strings.Split(slide, "\n")

for i := range lines {
if linesMatch(lines, sLines, i) {
return i
}
}

return -1
}

func GetEditorName(editorPath string) string {
parts := strings.Split(editorPath, "/")
return parts[len(parts)-1]
}

func InitEditorFlag(rootCmd *coral.Command) {
rootCmd.PersistentFlags().StringVarP(&program, "editor", "e", program, "Specify the editor to use")
}
4 changes: 4 additions & 0 deletions internal/model/model.go
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,7 @@ import (
"time"

"github.com/atotto/clipboard"
"github.com/maaslalani/slides/internal/editor"
"github.com/maaslalani/slides/internal/file"
"github.com/maaslalani/slides/internal/navigation"
"github.com/maaslalani/slides/internal/process"
Expand Down Expand Up @@ -177,6 +178,9 @@ func (m Model) Update(msg tea.Msg) (tea.Model, tea.Cmd) {
return m, nil
case "ctrl+c", "q":
return m, tea.Quit
case "ctrl+o":
// Opens the current slide in an editor
return m, editor.OpenNewWindow(m.FileName, m.Slides[m.Page])
default:
newState := navigation.Navigate(navigation.State{
Buffer: m.buffer,
Expand Down
2 changes: 2 additions & 0 deletions main.go
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,7 @@ import (

tea "github.com/charmbracelet/bubbletea"
"github.com/maaslalani/slides/internal/cmd"
"github.com/maaslalani/slides/internal/editor"
"github.com/maaslalani/slides/internal/model"
"github.com/maaslalani/slides/internal/navigation"
"github.com/muesli/coral"
Expand Down Expand Up @@ -47,6 +48,7 @@ func init() {
rootCmd.AddCommand(
cmd.ServeCmd,
)
editor.InitEditorFlag(rootCmd)
rootCmd.CompletionOptions.DisableDefaultCmd = true
}

Expand Down