package main import ( "fmt" "log/slog" "os" "os/exec" "strings" "github.com/spf13/cobra" ) func main() { var rootCmd = &cobra.Command{ Use: "tmux_popup", Short: "A CLI tool for various actions", Long: "tmux_popup is a CLI tool that supports various actions like tmux, lazygit, harlequin, and postings. It opens these programs in a floating tmux pane.", } rootCmd.PersistentFlags().String("width", "80%", "Width of the popup (e.g., 80%)") rootCmd.PersistentFlags().String("height", "80%", "Height of the popup (e.g., 80%)") rootCmd.AddCommand(tmuxCmd) rootCmd.AddCommand(lazygitCmd) rootCmd.AddCommand(harlequinCmd) rootCmd.AddCommand(postingsCmd) rootCmd.AddCommand(checkCmd) if err := rootCmd.Execute(); err != nil { fmt.Println("Error:", err) os.Exit(1) } } var tmuxCmd = &cobra.Command{ Use: "tmux", Short: "Opens a tmux popup", Run: func(cmd *cobra.Command, args []string) { width, _ := cmd.Flags().GetString("width") height, _ := cmd.Flags().GetString("height") openPopup("popup", "", width, height) }, } var lazygitCmd = &cobra.Command{ Use: "lazygit", Short: "Opens lazygit in a popup", Run: func(cmd *cobra.Command, args []string) { width, _ := cmd.Flags().GetString("width") height, _ := cmd.Flags().GetString("height") openPopup("lazygit", "lazygit", width, height) }, } var harlequinCmd = &cobra.Command{ Use: "harlequin", Short: "Opens harlequin in a popup", Run: func(cmd *cobra.Command, args []string) { width, _ := cmd.Flags().GetString("width") height, _ := cmd.Flags().GetString("height") openPopup("harlequin", "harlequin", width, height) }, } var postingsCmd = &cobra.Command{ Use: "postings", Short: "Opens postings in a popup", Run: func(cmd *cobra.Command, args []string) { width, _ := cmd.Flags().GetString("width") height, _ := cmd.Flags().GetString("height") openPopup("postings", "posting", width, height) }, } var checkCmd = &cobra.Command{ Use: "check", Short: "Checks the dependencies", Run: func(cmd *cobra.Command, args []string) { if err := CheckDependencies(); err != nil { fmt.Println("Error checking dependencies:", err) os.Exit(1) } fmt.Println("All dependencies are present.") }, } func openPopup(sessionName, command string, width, height string) { currentSession, err := exec.Command("tmux", "display-message", "-p", "-F", "#{session_name}").Output() if err != nil { fmt.Println("Error retrieving the current session:", err) return } if strings.TrimSpace(string(currentSession)) == sessionName { if err := exec.Command("tmux", "detach-client").Run(); err != nil { slog.Error("Failed to execute tmux command", "error", err) return } } else { args := []string{"popup", "-d", "#{pane_current_path}", "-xC", "-yC", fmt.Sprintf("-w%s", width), fmt.Sprintf("-h%s", height), "-E"} if command == "" { args = append(args, fmt.Sprintf("tmux attach -t %s || tmux new -s %s", sessionName, sessionName)) } else { args = append(args, fmt.Sprintf("tmux attach -t %s || tmux new -s %s '%s'", sessionName, sessionName, command)) } if err := exec.Command("tmux", args...).Run(); err != nil { slog.Error("Failed to execute tmux command", "error", err) return } } } func CheckDependencies() error { deps := []string{"lazygit", "harlequin", "posting"} args := []string{"--version"} for _, dep := range deps { if dep == "posting" { if err := runCmd(dep, []string{"--help"}); err != nil { return err } } else { if err := runCmd(dep, args); err != nil { return err } } } return nil } func runCmd(dep string, args []string) error { cmd := exec.Command(dep, args...) if err := cmd.Run(); err != nil { return fmt.Errorf("Error checking %s: %w", dep, err) } return nil }