Skip to content
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

Replicate xclip behaviour #23

Merged
merged 1 commit into from
Oct 13, 2024
Merged
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
10 changes: 2 additions & 8 deletions cmd/bore/app/handler.go
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,6 @@ package app
import (
"bufio"
"bytes"
"errors"
"fmt"
"io"
"os"
Expand Down Expand Up @@ -64,7 +63,7 @@ func (a *App) Copy(ctx *cli.Context) error {
return fmt.Errorf("unsupported format: %s", format)
}

var fn func(*cli.Context) (io.Reader, error) = a.CopyFromPrompt
fn := a.CopyFromPrompt

// If the program was piped to, read directly from STDIN
fi, err := os.Stdin.Stat()
Expand All @@ -75,11 +74,6 @@ func (a *App) Copy(ctx *cli.Context) error {
fn = a.CopyFromStdin
}

// Sanity check
if fn == nil {
return errors.New("unsupported input method")
}

reader, err := fn(ctx)
if err != nil {
return err
Expand All @@ -91,7 +85,7 @@ func (a *App) Copy(ctx *cli.Context) error {
}

if a.config.ShowIdOnCopy {
fmt.Fprintln(ctx.App.Writer, fmt.Sprintf("Copied content with ID: %s", id))
fmt.Fprintf(ctx.App.Writer, "Copied content with ID: %s", id)
}

return nil
Expand Down
18 changes: 15 additions & 3 deletions cmd/bore/root.go
Original file line number Diff line number Diff line change
Expand Up @@ -6,16 +6,17 @@ import (
"github.com/urfave/cli/v2"
boreapp "go.trulyao.dev/bore/cmd/bore/app"
"go.trulyao.dev/bore/pkg/config"
"go.trulyao.dev/bore/pkg/handler"
)

var (
app *boreapp.App
err error

version = "latest"
)

func Execute() error {
var err error
app, err = boreapp.New(config.DefaultConfigFilePath())
if err != nil {
return err
Expand All @@ -31,8 +32,13 @@ func CreateRootCommand() *cli.App {
Usage: "A minimal clipboard manager for terminal/headless environments",
Version: version,
Action: func(c *cli.Context) error {
cli.ShowAppHelp(c)
return nil
// If the program was piped to, read directly from STDIN
fi, _ := os.Stdin.Stat()
if (fi.Mode() & os.ModeCharDevice) == 0 {
return app.Copy(c)
}

return app.Paste(c)
},
Flags: []cli.Flag{
&cli.StringFlag{
Expand All @@ -45,6 +51,12 @@ func CreateRootCommand() *cli.App {
Name: "json",
Usage: "Output the result in JSON format",
},
&cli.StringFlag{
Name: "format",
Aliases: []string{"f"},
Usage: "The format of the content to copy. Available formats: base64, plain-text",
Value: handler.FormatPlainText.String(),
},
},

// Global flags have to be passed in BEFORE subcommands e.g `bore -c config.toml config dump`
Expand Down