diff --git a/cfg/config.go b/cfg/config.go index 3d4840a..e2c34ec 100644 --- a/cfg/config.go +++ b/cfg/config.go @@ -59,6 +59,8 @@ type Config struct { NoHeaders bool Columns string UseColumns []int + YankColumns string + UseYankColumns []int Separator string OutputMode int InvertMatch bool diff --git a/cmd/root.go b/cmd/root.go index afbf0df..c4ff4fe 100644 --- a/cmd/root.go +++ b/cmd/root.go @@ -147,6 +147,8 @@ func Execute() { "Custom field separator") rootCmd.PersistentFlags().StringVarP(&conf.Columns, "columns", "c", "", "Only show the speficied columns (separated by ,)") + rootCmd.PersistentFlags().StringVarP(&conf.YankColumns, "yank-columns", "y", "", + "Yank the speficied columns (separated by ,) to the clipboard") rootCmd.PersistentFlags().StringVarP(&conf.TransposeColumns, "transpose-columns", "T", "", "Transpose the speficied columns (separated by ,)") diff --git a/go.mod b/go.mod index 7c203d7..7ae1eeb 100644 --- a/go.mod +++ b/go.mod @@ -18,6 +18,7 @@ require ( github.com/agext/levenshtein v1.2.3 // indirect github.com/apparentlymart/go-textseg/v13 v13.0.0 // indirect github.com/apparentlymart/go-textseg/v15 v15.0.0 // indirect + github.com/atotto/clipboard v0.1.4 // indirect github.com/google/go-cmp v0.6.0 // indirect github.com/inconshreveable/mousetrap v1.1.0 // indirect github.com/mattn/go-runewidth v0.0.16 // indirect diff --git a/go.sum b/go.sum index 5491a3d..3a96455 100644 --- a/go.sum +++ b/go.sum @@ -8,6 +8,8 @@ github.com/apparentlymart/go-textseg/v15 v15.0.0 h1:uYvfpb3DyLSCGWnctWKGj857c6ew github.com/apparentlymart/go-textseg/v15 v15.0.0/go.mod h1:K8XmNZdhEBkdlyDdvbmmsvpAG721bKi0joRfFdHIWJ4= github.com/araddon/dateparse v0.0.0-20210429162001-6b43995a97de h1:FxWPpzIjnTlhPwqqXc4/vE0f7GvRjuAsbW+HOIe8KnA= github.com/araddon/dateparse v0.0.0-20210429162001-6b43995a97de/go.mod h1:DCaWoUhZrYW9p1lxo/cm8EmUOOzAPSEZNGF2DK1dJgw= +github.com/atotto/clipboard v0.1.4 h1:EH0zSVneZPSuFR11BlR9YppQTVDbh5+16AmcJi4g1z4= +github.com/atotto/clipboard v0.1.4/go.mod h1:ZY9tmq7sm5xIbd9bOK4onWV4S6X0u6GY7Vn0Yu86PYI= github.com/cpuguy83/go-md2man/v2 v2.0.4/go.mod h1:tgQtvFlXSQOSOSIRvRPT7W67SCa46tRHOmNcaadrF8o= github.com/davecgh/go-spew v1.1.0/go.mod h1:J7Y8YcW2NihsgmVo/mv3lAwl/skON4iLHjSsI+c5H38= github.com/davecgh/go-spew v1.1.1 h1:vj9j/u1bqnvCEfJOwUhtlOARqs3+rkHYY13jYWTU97c= diff --git a/lib/helpers.go b/lib/helpers.go index 32b6705..28dc321 100644 --- a/lib/helpers.go +++ b/lib/helpers.go @@ -77,6 +77,14 @@ func PrepareColumns(conf *cfg.Config, data *Tabdata) error { conf.UseColumns = usecolumns + // -y columns + useyankcolumns, err := PrepareColumnVars(conf.YankColumns, data) + if err != nil { + return err + } + + conf.UseYankColumns = useyankcolumns + return nil } diff --git a/lib/printer.go b/lib/printer.go index 3189060..c863b7f 100644 --- a/lib/printer.go +++ b/lib/printer.go @@ -26,6 +26,7 @@ import ( "strconv" "strings" + "github.com/atotto/clipboard" "github.com/gookit/color" "github.com/olekukonko/tablewriter" "github.com/tlinden/tablizer/cfg" @@ -38,6 +39,9 @@ func printData(writer io.Writer, conf cfg.Config, data *Tabdata) { // by, independently if it's being used for display or not. sortTable(conf, data) + // put one or more columns into clipboard + yankColumns(conf, data) + // add numbers to headers and remove those we're not interested in numberizeAndReduceHeaders(conf, data) @@ -262,3 +266,26 @@ func printCSVData(writer io.Writer, data *Tabdata) { log.Fatal(err) } } + +func yankColumns(conf cfg.Config, data *Tabdata) { + var yank string + + if len(data.entries) == 0 || len(conf.UseYankColumns) == 0 { + return + } + + for _, row := range data.entries { + for i, field := range row { + for _, idx := range conf.UseYankColumns { + if i == idx-1 { + yank += field + " " + } + } + } + } + + if yank != "" { + // FIXME: does not use the primary clipboard, grnz... + clipboard.WriteAll(yank) + } +}