cmd

package
v0.0.3 Latest Latest
Warning

This package is not in the latest version of its module.

Go to latest
Published: Aug 30, 2020 License: MIT Imports: 14 Imported by: 0

Documentation

Index

Constants

This section is empty.

Variables

View Source
var AppCommand = &cli.NestedCommand{
	Command: &cobra.Command{
		Use:   "app",
		Short: "Commands for interacting with the running iTerm2 application",
	},
	Subcommands: []*cli.NestedCommand{
		SendTextCommand,
		ListSessionsCommand,
		SplitPanesCommand,
		CreateTabCommand,
	},
}
View Source
var AutolaunchCommand = &cobra.Command{
	Use:   "autolaunch",
	Short: "Generate a Python script for iTerm2's autolaunch",
	Args:  cobra.MinimumNArgs(1),
	RunE: func(cmd *cobra.Command, args []string) error {
		saveAs, err := cmd.Flags().GetString("save-as")
		if err != nil {
			return err
		}

		executablePath, err := shell.Which(args[0])
		if err != nil {
			return err
		}

		quotedPath := quote(executablePath)[0]
		quotedArgs := quote(args...)

		var br io.Reader
		var f *os.File

		bw := &bytes.Buffer{}
		br = bw

		if saveAs != "" {
			autolaunchPath, err := homedir.Expand(autolaunchDir)
			if err != nil {
				return err
			}

			autolaunchScriptPath := path.Join(autolaunchPath, fmt.Sprintf("%s.py", saveAs))

			f, err = os.Create(autolaunchScriptPath)
			if err != nil {
				return err
			}

			br = io.TeeReader(bw, f)
		}

		_, _ = fmt.Fprintf(bw, "from os import execv\nexecv(%s, [%s])\n", quotedPath, strings.Join(quotedArgs, ", "))

		_, err = io.Copy(os.Stdout, br)
		if err != nil {
			return err
		}

		if f != nil {
			if err := f.Close(); err != nil {
				return err
			}
			_, _ = fmt.Fprintf(os.Stderr, "saved to %s\n", f.Name())
		}

		return nil
	},
}
View Source
var CreateTabCommand = &cli.NestedCommand{
	Command: &cobra.Command{
		Use:  "create-tab WINDOW_ID",
		Args: cobra.MaximumNArgs(1),
		RunE: cli.WithApp("itermctl", func(app *itermctl.App, cmd *cobra.Command, args []string) error {
			var windowId string
			if len(args) > 0 {
				windowId = args[0]
			} else {
				shellSession, err := env.Session()
				if err != nil {
					return fmt.Errorf("can't find current window: %w", err)
				}

				sessions, err := app.ListSessions()
				if err != nil {
					return fmt.Errorf("can't find current window: %w", err)
				}

				if shellSession.WindowIndex > len(sessions.GetWindows())-1 {
					return fmt.Errorf("can't find current window: there are only %d windows but index from shell is %d",
						len(sessions.GetWindows()), shellSession.WindowIndex)
				}

				window := sessions.GetWindows()[shellSession.WindowIndex]
				windowId = window.GetWindowId()
			}

			tabIndex, err := cmd.Flags().GetUint32("tab-index")
			if err != nil {
				return err
			}

			profileName, err := cmd.Flags().GetString("profile-name")
			if err != nil {
				return err
			}

			resp, err := app.CreateTab(windowId, tabIndex, profileName)
			if err != nil {
				return err
			}

			fmt.Printf("%s\n", resp.GetSessionId())
			return nil
		}),
	},
}
View Source
var ListSessionsCommand = &cli.NestedCommand{
	Command: &cobra.Command{
		Use:  "list-sessions",
		Long: "list sessions with status ([i]nactive, [a]ctive, [b]buried), session ID, window ID and optionally the session status",
		Args: cobra.ExactArgs(0),
		RunE: cli.WithApp("itermctl", func(app *itermctl.App, cmd *cobra.Command, args []string) error {
			withTitles, err := cmd.Flags().GetBool("titles")
			if err != nil {
				return err
			}

			sessionsResponse, err := app.ListSessions()
			if err != nil {
				return err
			}

			activeSessionId, err := app.ActiveSessionId()
			if err != nil {
				return err
			}

			tw := tabwriter.NewWriter(os.Stdout, 0, 8, 1, ' ', 0)
			for _, w := range sessionsResponse.GetWindows() {
				for _, t := range w.GetTabs() {
					for _, l := range t.Root.Links {
						var flag string
						var title string

						if withTitles {
							title = l.GetSession().GetTitle()
						}

						if l.GetSession().GetUniqueIdentifier() == activeSessionId {
							flag = "a"
						} else {
							flag = "i"
						}

						_, _ = fmt.Fprintf(tw,
							"%s\t%s\t%s\t%s\n",
							flag,
							l.GetSession().GetUniqueIdentifier(),
							w.GetWindowId(),
							title,
						)
					}
				}
			}

			for _, bs := range sessionsResponse.GetBuriedSessions() {
				var title string
				if withTitles {
					title = bs.GetTitle()
				}
				_, _ = fmt.Fprintf(
					tw,
					"b\t%s\t\t%s\n",
					bs.GetUniqueIdentifier(),
					title,
				)
			}

			return tw.Flush()
		}),
	},
}
View Source
var SendTextCommand = &cli.NestedCommand{
	Command: &cobra.Command{
		Use:  "send-text SESSION_ID",
		Args: cobra.ExactArgs(1),
		RunE: cli.WithApp("itermctl", func(app *itermctl.App, cmd *cobra.Command, args []string) error {
			suppressBroadcast, err := cmd.Flags().GetBool("suppress-broadcast")
			if err != nil {
				return err
			}

			data, err := ioutil.ReadAll(os.Stdin)
			if err != nil {
				return err
			}

			session, err := app.Session(args[0])
			if err != nil {
				return err
			}

			if err := session.SendText(string(data), suppressBroadcast); err != nil {
				return err
			}

			return nil
		}),
	},
}
View Source
var SplitPanesCommand = &cli.NestedCommand{
	Command: &cobra.Command{
		Use:  "split-pane SESSION_ID",
		Args: cobra.MaximumNArgs(1),
		RunE: cli.WithApp("itermctl", func(app *itermctl.App, cmd *cobra.Command, args []string) error {
			before, err := cmd.Flags().GetBool("before")
			if err != nil {
				return err
			}

			vertical, err := cmd.Flags().GetBool("vertical")
			if err != nil {
				return err
			}

			var sessionId string

			if len(args) > 0 {
				sessionId = args[0]
			} else {
				shellSession, err := env.Session()
				if err != nil {
					return fmt.Errorf("can't find current session: %w", err)
				}
				sessionId = shellSession.SessionId
			}

			session, err := app.Session(sessionId)
			if err != nil {
				return err
			}

			sessionIds, err := session.SplitPane(vertical, before)
			if err != nil {
				return err
			}

			for _, sessionId := range sessionIds {
				fmt.Printf("%s\n", sessionId)
			}

			return nil
		}),
	},
}

Functions

This section is empty.

Types

This section is empty.

Jump to

Keyboard shortcuts

? : This menu
/ : Search site
f or F : Jump to
y or Y : Canonical URL