From 2728ec2e404a2f701881ff7324035022e337c71f Mon Sep 17 00:00:00 2001 From: Takumasa Sakao Date: Sat, 18 Sep 2021 15:10:35 +0900 Subject: [PATCH 1/5] Do not wrap --- viddy.go | 1 + 1 file changed, 1 insertion(+) diff --git a/viddy.go b/viddy.go index 57dc46c..88294f1 100644 --- a/viddy.go +++ b/viddy.go @@ -382,6 +382,7 @@ func (v *Viddy) Run() error { b.SetDynamicColors(true) b.SetTitle("body") b.SetRegions(true) + b.SetWrap(false) v.bodyView = b t := tview.NewTextView() From c71941509b80daa6049c39a4868f740dae36a95c Mon Sep 17 00:00:00 2001 From: Takumasa Sakao Date: Sat, 18 Sep 2021 22:39:21 +0900 Subject: [PATCH 2/5] Add option to run on pty --- config.go | 7 ++++++ go.mod | 28 +++++++++++------------ go.sum | 14 ++++++++++++ main.go | 1 + run.go | 61 ++++++++++++++++++++++++++++++++++++++++++++++++++ run_windows.go | 45 +++++++++++++++++++++++++++++++++++++ snapshot.go | 36 ++--------------------------- viddy.go | 53 ++++++++++++++++++++++++++++++++++--------- 8 files changed, 187 insertions(+), 58 deletions(-) create mode 100644 run.go create mode 100644 run_windows.go diff --git a/config.go b/config.go index 37540e3..009405d 100644 --- a/config.go +++ b/config.go @@ -42,6 +42,7 @@ type general struct { debug bool differences bool noTitle bool + pty bool } type theme struct { @@ -81,6 +82,7 @@ func newConfig(v *viper.Viper, args []string) (*config, error) { flagSet.Bool("debug", false, "") flagSet.String("shell", "", "shell (default \"sh\")") flagSet.String("shell-options", "", "additional shell options") + flagSet.Bool("pty", false, "run on pty (experimental)") flagSet.SetInterspersed(false) @@ -133,11 +135,16 @@ func newConfig(v *viper.Viper, args []string) (*config, error) { return nil, err } + if err := v.BindPFlag("general.pty", flagSet.Lookup("pty")); err != nil { + return nil, err + } + conf.general.debug = v.GetBool("general.debug") conf.general.shell = v.GetString("general.shell") conf.general.shellOptions = v.GetString("general.shell_options") conf.general.differences, _ = flagSet.GetBool("differences") conf.general.noTitle, _ = flagSet.GetBool("no-title") + conf.general.pty = v.GetBool("general.pty") conf.theme.Theme = tview.Theme{ PrimitiveBackgroundColor: tcell.GetColor(v.GetString("color.background")), diff --git a/go.mod b/go.mod index 37e5663..1d78074 100644 --- a/go.mod +++ b/go.mod @@ -3,27 +3,17 @@ module github.com/sachaos/viddy go 1.17 require ( + github.com/Azure/go-ansiterm v0.0.0-20210617225240-d185dfc1b5a1 // indirect github.com/adrg/xdg v0.3.3 + github.com/creack/pty v1.1.15 + github.com/davecgh/go-spew v1.1.1 // indirect github.com/fatih/color v1.12.0 github.com/fsnotify/fsnotify v1.5.1 // indirect + github.com/gdamore/encoding v1.0.0 // indirect github.com/gdamore/tcell/v2 v2.4.1-0.20210904044819-ae5116d72813 github.com/google/go-github v17.0.0+incompatible // indirect github.com/google/go-querystring v1.1.0 // indirect github.com/hashicorp/go-version v1.3.0 // indirect - github.com/rivo/tview v0.0.0-20210624165335-29d673af0ce2 - github.com/sergi/go-diff v1.2.0 - github.com/spf13/cast v1.4.1 - github.com/spf13/pflag v1.0.5 - github.com/spf13/viper v1.8.1 - github.com/stretchr/testify v1.7.0 - github.com/tcnksm/go-latest v0.0.0-20170313132115-e3007ae9052e - golang.org/x/sys v0.0.0-20210903071746-97244b99971b // indirect - golang.org/x/text v0.3.7 // indirect -) - -require ( - github.com/davecgh/go-spew v1.1.1 // indirect - github.com/gdamore/encoding v1.0.0 // indirect github.com/hashicorp/hcl v1.0.0 // indirect github.com/lucasb-eyer/go-colorful v1.2.0 // indirect github.com/magiconair/properties v1.8.5 // indirect @@ -31,14 +21,24 @@ require ( github.com/mattn/go-isatty v0.0.12 // indirect github.com/mattn/go-runewidth v0.0.13 // indirect github.com/mitchellh/mapstructure v1.4.1 // indirect + github.com/moby/term v0.0.0-20210619224110-3f7ff695adc6 github.com/pelletier/go-toml v1.9.3 // indirect github.com/pmezard/go-difflib v1.0.0 // indirect + github.com/rivo/tview v0.0.0-20210624165335-29d673af0ce2 github.com/rivo/uniseg v0.2.0 // indirect + github.com/sergi/go-diff v1.2.0 github.com/spf13/afero v1.6.0 // indirect + github.com/spf13/cast v1.4.1 github.com/spf13/jwalterweatherman v1.1.0 // indirect + github.com/spf13/pflag v1.0.5 + github.com/spf13/viper v1.8.1 + github.com/stretchr/testify v1.7.0 github.com/subosito/gotenv v1.2.0 // indirect + github.com/tcnksm/go-latest v0.0.0-20170313132115-e3007ae9052e golang.org/x/net v0.0.0-20210405180319-a5a99cb37ef4 // indirect + golang.org/x/sys v0.0.0-20210903071746-97244b99971b // indirect golang.org/x/term v0.0.0-20210220032956-6a3ed077a48d // indirect + golang.org/x/text v0.3.7 // indirect gopkg.in/ini.v1 v1.62.0 // indirect gopkg.in/yaml.v2 v2.4.0 // indirect gopkg.in/yaml.v3 v3.0.0-20210107192922-496545a6307b // indirect diff --git a/go.sum b/go.sum index 5e2c8b6..040bde0 100644 --- a/go.sum +++ b/go.sum @@ -37,6 +37,8 @@ cloud.google.com/go/storage v1.6.0/go.mod h1:N7U0C8pVQ/+NIKOBQyamJIeKQKkZ+mxpohl cloud.google.com/go/storage v1.8.0/go.mod h1:Wv1Oy7z6Yz3DshWRJFhqM/UCfaWIRTdp0RXyy7KQOVs= cloud.google.com/go/storage v1.10.0/go.mod h1:FLPqc6j+Ki4BU591ie1oL6qBQGu2Bl/tZ9ullr3+Kg0= dmitri.shuralyov.com/gpu/mtl v0.0.0-20190408044501-666a987793e9/go.mod h1:H6x//7gZCb22OMCxBHrMx7a5I7Hp++hsVxbQ4BYO7hU= +github.com/Azure/go-ansiterm v0.0.0-20210617225240-d185dfc1b5a1 h1:UQHMgLO+TxOElx5B5HZ4hJQsoJ/PvUvKRhJHDQXO8P8= +github.com/Azure/go-ansiterm v0.0.0-20210617225240-d185dfc1b5a1/go.mod h1:xomTg63KZ2rFqZQzSB4Vz2SUXa1BpHTVz9L5PTmPC4E= github.com/BurntSushi/toml v0.3.1/go.mod h1:xHWCNGjB5oqiDr8zfno3MHue2Ht5sIBksp03qcyfWMU= github.com/BurntSushi/xgb v0.0.0-20160522181843-27f122750802/go.mod h1:IVnqGOEym/WlBOVXweHU+Q+/VP0lqqI8lqeDx9IjBqo= github.com/adrg/xdg v0.3.3 h1:s/tV7MdqQnzB1nKY8aqHvAMD+uCiuEDzVB5HLRY849U= @@ -57,6 +59,9 @@ github.com/cncf/udpa/go v0.0.0-20200629203442-efcf912fb354/go.mod h1:WmhPx2Nbnht github.com/cncf/udpa/go v0.0.0-20201120205902-5459f2c99403/go.mod h1:WmhPx2Nbnhtbo57+VJT5O0JRkEi1Wbu0z5j0R8u5Hbk= github.com/coreos/go-semver v0.3.0/go.mod h1:nnelYz7RCh+5ahJtPPxZlU+153eP4D4r3EedlOD2RNk= github.com/coreos/go-systemd/v22 v22.3.2/go.mod h1:Y58oyj3AT4RCenI/lSvhwexgC+NSVTIJ3seZv2GcEnc= +github.com/creack/pty v1.1.11/go.mod h1:oKZEueFk5CKHvIhNR5MUki03XCEU+Q6VDXinZuGJ33E= +github.com/creack/pty v1.1.15 h1:cKRCLMj3Ddm54bKSpemfQ8AtYFBhAI2MPmdys22fBdc= +github.com/creack/pty v1.1.15/go.mod h1:MOBLtS5ELjhRRrroQr9kyvTxUAFNvYEK993ew/Vr4O4= 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= github.com/davecgh/go-spew v1.1.1/go.mod h1:J7Y8YcW2NihsgmVo/mv3lAwl/skON4iLHjSsI+c5H38= @@ -212,6 +217,8 @@ github.com/mitchellh/mapstructure v0.0.0-20160808181253-ca63d7c062ee/go.mod h1:F github.com/mitchellh/mapstructure v1.1.2/go.mod h1:FVVH3fgwuzCH5S8UJGiWEs2h04kUh9fWfEaFds41c1Y= github.com/mitchellh/mapstructure v1.4.1 h1:CpVNEelQCZBooIPDn+AR3NpivK/TIKU8bDxdASFVQag= github.com/mitchellh/mapstructure v1.4.1/go.mod h1:bFUtVrKA4DC2yAKiSyO/QUcy7e+RRV2QTWOzhPopBRo= +github.com/moby/term v0.0.0-20210619224110-3f7ff695adc6 h1:dcztxKSvZ4Id8iPpHERQBbIJfabdt4wUm5qy3wOL2Zc= +github.com/moby/term v0.0.0-20210619224110-3f7ff695adc6/go.mod h1:E2VnQOmVuvZB6UYnnDB0qG5Nq/1tD9acaOpo6xmt0Kw= github.com/modern-go/concurrent v0.0.0-20180228061459-e0a39a4cb421/go.mod h1:6dJC0mAP4ikYIbvyc7fijjWJddQyLn8Ig3JB5CqoB9Q= github.com/modern-go/reflect2 v0.0.0-20180701023420-4b7aa43c6742/go.mod h1:bx2lNnkwVCuqBIxFjflWJWanXIb3RllmbCylyMrvgv0= github.com/modern-go/reflect2 v1.0.1/go.mod h1:bx2lNnkwVCuqBIxFjflWJWanXIb3RllmbCylyMrvgv0= @@ -219,6 +226,8 @@ github.com/pascaldekloe/goe v0.0.0-20180627143212-57f6aae5913c/go.mod h1:lzWF7FI github.com/pelletier/go-toml v1.9.3 h1:zeC5b1GviRUyKYd6OJPvBU/mcVDVoL1OhT17FCt5dSQ= github.com/pelletier/go-toml v1.9.3/go.mod h1:u1nR/EPcESfeI/szUZKdtJ0xRNbUoANCkoOuaOx1Y+c= github.com/pkg/errors v0.8.1/go.mod h1:bwawxfHBFNV+L2hUp1rHADufV3IMtnDRdf1r5NINEl0= +github.com/pkg/errors v0.9.1 h1:FEBLx1zS214owpjy7qsBeixbURkuhQAwrK5UwLGTwt4= +github.com/pkg/errors v0.9.1/go.mod h1:bwawxfHBFNV+L2hUp1rHADufV3IMtnDRdf1r5NINEl0= github.com/pkg/sftp v1.10.1/go.mod h1:lYOWFsE0bwd1+KfKJaKeuokY15vzFx25BLbzYYoAxZI= github.com/pmezard/go-difflib v1.0.0 h1:4DBwDE0NGyQoBHbLQYPwSUPoCMWR5BEzIk/f1lZbAQM= github.com/pmezard/go-difflib v1.0.0/go.mod h1:iKH77koFhYxTK1pcRnkKkqfTogsbg7gZNVY4sRDYZ/4= @@ -245,6 +254,7 @@ github.com/spf13/cast v1.4.1 h1:s0hze+J0196ZfEMTs80N7UlFt0BDuQ7Q+JDnHiMWKdA= github.com/spf13/cast v1.4.1/go.mod h1:Qx5cxh0v+4UWYiBimWS+eyWzqEqokIECu5etghLkUJE= github.com/spf13/jwalterweatherman v1.1.0 h1:ue6voC5bR5F8YxI5S67j9i582FU4Qvo2bmqnqMYADFk= github.com/spf13/jwalterweatherman v1.1.0/go.mod h1:aNWZUN0dPAAO/Ljvb5BEdw96iTZ0EXowPYD95IqWIGo= +github.com/spf13/pflag v1.0.3/go.mod h1:DYY7MBk1bdzusC3SYhjObp+wFpr4gzcvqqNjLnInEg4= github.com/spf13/pflag v1.0.5 h1:iy+VFUOCP1a+8yFto/drg2CJ5u0yRoB7fZw3DKv/JXA= github.com/spf13/pflag v1.0.5/go.mod h1:McXfInJRrz4CZXVZOBLb0bTZqETkiAhM9Iw0y3An2Bg= github.com/spf13/viper v1.8.1 h1:Kq1fyeebqsBfbjZj4EL7gj2IO0mMaiyjYUWcUsl2O44= @@ -424,6 +434,7 @@ golang.org/x/sys v0.0.0-20210320140829-1e4c9ba3b0c4/go.mod h1:h1NjWce9XRLGQEsW7w golang.org/x/sys v0.0.0-20210330210617-4fbd30eecc44/go.mod h1:h1NjWce9XRLGQEsW7wpKNCjG9DtNlClVuFLEZdDNbEs= golang.org/x/sys v0.0.0-20210403161142-5e06dd20ab57/go.mod h1:h1NjWce9XRLGQEsW7wpKNCjG9DtNlClVuFLEZdDNbEs= golang.org/x/sys v0.0.0-20210510120138-977fb7262007/go.mod h1:oPkhp1MJrh7nUepCBck5+mAzfO9JrbApNNgaTdGDITg= +golang.org/x/sys v0.0.0-20210616094352-59db8d763f22/go.mod h1:oPkhp1MJrh7nUepCBck5+mAzfO9JrbApNNgaTdGDITg= golang.org/x/sys v0.0.0-20210630005230-0f9fa26af87c/go.mod h1:oPkhp1MJrh7nUepCBck5+mAzfO9JrbApNNgaTdGDITg= golang.org/x/sys v0.0.0-20210903071746-97244b99971b h1:3Dq0eVHn0uaQJmPO+/aYPI/fRMqdrVDbu7MQcku54gg= golang.org/x/sys v0.0.0-20210903071746-97244b99971b/go.mod h1:oPkhp1MJrh7nUepCBck5+mAzfO9JrbApNNgaTdGDITg= @@ -456,6 +467,7 @@ golang.org/x/tools v0.0.0-20190506145303-2d16b83fe98c/go.mod h1:RgjU9mgBXZiqYHBn golang.org/x/tools v0.0.0-20190524140312-2c0ae7006135/go.mod h1:RgjU9mgBXZiqYHBnxXauZ1Gv1EHHAz9KjViQ78xBX0Q= golang.org/x/tools v0.0.0-20190606124116-d0a3d012864b/go.mod h1:/rFqwRUd4F7ZHNgwSSTFct+R/Kf4OFW1sUzUTQQTgfc= golang.org/x/tools v0.0.0-20190621195816-6e04913cbbac/go.mod h1:/rFqwRUd4F7ZHNgwSSTFct+R/Kf4OFW1sUzUTQQTgfc= +golang.org/x/tools v0.0.0-20190624222133-a101b041ded4/go.mod h1:/rFqwRUd4F7ZHNgwSSTFct+R/Kf4OFW1sUzUTQQTgfc= golang.org/x/tools v0.0.0-20190628153133-6cdbf07be9d0/go.mod h1:/rFqwRUd4F7ZHNgwSSTFct+R/Kf4OFW1sUzUTQQTgfc= golang.org/x/tools v0.0.0-20190816200558-6889da9d5479/go.mod h1:b+2E5dAYhXwXZwtnZ6UAqBI28+e2cm9otk0dWdXHAEo= golang.org/x/tools v0.0.0-20190911174233-4f2ddba30aff/go.mod h1:b+2E5dAYhXwXZwtnZ6UAqBI28+e2cm9otk0dWdXHAEo= @@ -617,6 +629,8 @@ gopkg.in/yaml.v2 v2.4.0/go.mod h1:RDklbk79AGWmwhnvt/jBztapEOGDOx6ZbXqjP6csGnQ= gopkg.in/yaml.v3 v3.0.0-20200313102051-9f266ea9e77c/go.mod h1:K4uyk7z7BCEPqu6E+C64Yfv1cQ7kz7rIZviUmN+EgEM= gopkg.in/yaml.v3 v3.0.0-20210107192922-496545a6307b h1:h8qDotaEPuJATrMmW04NCwg7v22aHH28wwpauUhK9Oo= gopkg.in/yaml.v3 v3.0.0-20210107192922-496545a6307b/go.mod h1:K4uyk7z7BCEPqu6E+C64Yfv1cQ7kz7rIZviUmN+EgEM= +gotest.tools/v3 v3.0.2 h1:kG1BFyqVHuQoVQiR1bWGnfz/fmHvvuiSPIV7rvl360E= +gotest.tools/v3 v3.0.2/go.mod h1:3SzNCllyD9/Y+b5r9JIKQ474KzkZyqLqEfYqMsX94Bk= honnef.co/go/tools v0.0.0-20190102054323-c2f93a96b099/go.mod h1:rf3lG4BRIbNafJWhAfAdb/ePZxsR/4RtNHQocxwk9r4= honnef.co/go/tools v0.0.0-20190106161140-3f1c8253044a/go.mod h1:rf3lG4BRIbNafJWhAfAdb/ePZxsR/4RtNHQocxwk9r4= honnef.co/go/tools v0.0.0-20190418001031-e561f6794a2a/go.mod h1:rf3lG4BRIbNafJWhAfAdb/ePZxsR/4RtNHQocxwk9r4= diff --git a/main.go b/main.go index e93fdc3..e57ecf2 100644 --- a/main.go +++ b/main.go @@ -79,6 +79,7 @@ Options: -t, --no-title turn off header --shell shell (default "sh") --shell-options additional shell options + --pty run on pty (experimental) -h, --help display this help and exit -v, --version output version information and exit`) diff --git a/run.go b/run.go new file mode 100644 index 0000000..f964055 --- /dev/null +++ b/run.go @@ -0,0 +1,61 @@ +// +build !windows + +package main + +import ( + "bytes" + "io" + "time" + + "github.com/creack/pty" +) + +//nolint:unparam +func (s *Snapshot) run(finishedQueue chan<- int64, width int, isPty bool) error { + s.start = time.Now() + defer func() { + s.end = time.Now() + }() + + var b, eb bytes.Buffer + + commands := []string{s.command} + commands = append(commands, s.args...) + + command := s.prepareCommand(commands) + command.Stderr = &eb + + if isPty { + pty, err := pty.StartWithSize(command, &pty.Winsize{ + Cols: uint16(width), + }) + + if err != nil { + return nil + } + + go func() { + _, _ = io.Copy(&b, pty) + }() + } else { + command.Stdout = &b + if err := command.Start(); err != nil { + return err + } + } + + go func() { + if err := command.Wait(); err != nil { + s.err = err + } + + s.result = b.Bytes() + s.errorResult = eb.Bytes() + s.exitCode = command.ProcessState.ExitCode() + s.completed = true + finishedQueue <- s.id + close(s.finish) + }() + + return nil +} diff --git a/run_windows.go b/run_windows.go new file mode 100644 index 0000000..71274d3 --- /dev/null +++ b/run_windows.go @@ -0,0 +1,45 @@ +// +build windows + +package main + +import ( + "bytes" + "time" +) + +//nolint:unparam +func (s *Snapshot) run(finishedQueue chan<- int64, width int, isPty bool) error { + s.start = time.Now() + defer func() { + s.end = time.Now() + }() + + var b, eb bytes.Buffer + + commands := []string{s.command} + commands = append(commands, s.args...) + + command := s.prepareCommand(commands) + command.Stderr = &eb + command.Stdout = &b + + err := command.Start() + if err != nil { + return err + } + + go func() { + if err := command.Wait(); err != nil { + s.err = err + } + + s.result = b.Bytes() + s.errorResult = eb.Bytes() + s.exitCode = command.ProcessState.ExitCode() + s.completed = true + finishedQueue <- s.id + close(s.finish) + }() + + return nil +} diff --git a/snapshot.go b/snapshot.go index 43fb3ec..31714e3 100644 --- a/snapshot.go +++ b/snapshot.go @@ -95,18 +95,7 @@ func (s *Snapshot) compareFromBefore() error { return nil } -//nolint:unparam -func (s *Snapshot) run(finishedQueue chan<- int64) error { - s.start = time.Now() - defer func() { - s.end = time.Now() - }() - - var b, eb bytes.Buffer - - commands := []string{s.command} - commands = append(commands, s.args...) - +func (s *Snapshot) prepareCommand(commands []string) *exec.Cmd { var command *exec.Cmd if runtime.GOOS == "windows" { @@ -120,28 +109,7 @@ func (s *Snapshot) run(finishedQueue chan<- int64) error { args = append(args, strings.Join(commands, " ")) command = exec.Command(s.shell, args...) //nolint:gosec } - - command.Stdout = &b - command.Stderr = &eb - - if err := command.Start(); err != nil { - return nil //nolint:nilerr - } - - go func() { - if err := command.Wait(); err != nil { - s.err = err - } - - s.result = b.Bytes() - s.errorResult = eb.Bytes() - s.exitCode = command.ProcessState.ExitCode() - s.completed = true - finishedQueue <- s.id - close(s.finish) - }() - - return nil + return command } func isWhiteString(str string) bool { diff --git a/viddy.go b/viddy.go index 88294f1..8058629 100644 --- a/viddy.go +++ b/viddy.go @@ -6,12 +6,17 @@ import ( "fmt" "html/template" "io" + "os" "sort" "strconv" "strings" "sync" "time" + "github.com/moby/term" + + "sync/atomic" + "github.com/gdamore/tcell/v2" "github.com/rivo/tview" ) @@ -42,8 +47,12 @@ type Viddy struct { historyRows map[int64]*HistoryRow sync.RWMutex + // bWidth store current pty width. + bWidth atomic.Value + idList []int64 + middle *tview.Flex bodyView *tview.TextView app *tview.Application logView *tview.TextView @@ -63,6 +72,7 @@ type Viddy struct { isNoTitle bool isShowDiff bool isEditQuery bool + pty bool query string @@ -118,6 +128,7 @@ func NewViddy(conf *config) *Viddy { isShowDiff: conf.general.differences, isNoTitle: conf.general.noTitle, isDebug: conf.general.debug, + pty: conf.general.pty, currentID: -1, latestFinishedID: -1, @@ -162,7 +173,7 @@ func (v *Viddy) startRunner() { v.addSnapshot(s) v.queue <- s.id - _ = s.run(v.finishedQueue) + _ = s.run(v.finishedQueue, v.getBodyWidth(), v.pty) } } @@ -358,15 +369,15 @@ func (v *Viddy) arrange() { body.AddItem(v.queryEditor, 1, 1, false) } - middle := tview.NewFlex().SetDirection(tview.FlexColumn). - AddItem(body, 0, 1, false) + v.middle.Clear() + v.middle.AddItem(body, 0, 1, false) if v.isTimeMachine { - middle.AddItem(v.historyView, 21, 1, true) + v.middle.AddItem(v.historyView, 21, 1, true) } flex.AddItem( - middle, + v.middle, 0, 1, false) if v.showLogView { @@ -382,6 +393,7 @@ func (v *Viddy) Run() error { b.SetDynamicColors(true) b.SetTitle("body") b.SetRegions(true) + b.GetInnerRect() b.SetWrap(false) v.bodyView = b @@ -432,6 +444,9 @@ func (v *Viddy) Run() error { _, _ = io.WriteString(hv, v.helpPage()) v.helpView = hv + middle := tview.NewFlex().SetDirection(tview.FlexColumn) + v.middle = middle + q := tview.NewInputField().SetLabel("/") q.SetChangedFunc(func(text string) { v.query = text @@ -549,18 +564,23 @@ func (v *Viddy) Run() error { return event }) - v.app = app - - go v.diffQueueHandler() - go v.queueHandler() - go v.startRunner() + app.SetAfterDrawFunc(func(screen tcell.Screen) { + v.setBodyWidth() + }) v.UpdateStatusView() app.EnableMouse(true) + v.app = app v.arrange() + v.setBodyWidth() + + go v.diffQueueHandler() + go v.queueHandler() + go v.startRunner() + return app.Run() } @@ -698,6 +718,19 @@ func formatKeyStroke(stroke KeyStroke) string { return b.String() } +func (v *Viddy) setBodyWidth() { + width := 80 + if winsize, err := term.GetWinsize(os.Stdout.Fd()); err == nil { + width = int(winsize.Width) + } + + v.bWidth.Store(width) +} + +func (v *Viddy) getBodyWidth() int { + return v.bWidth.Load().(int) +} + func (v *Viddy) helpPage() string { value := struct { GoToPast string From 5342ef3304e0ad42995aabd8409d682e1dc8f83d Mon Sep 17 00:00:00 2001 From: Takumasa Sakao Date: Sat, 18 Sep 2021 22:40:26 +0900 Subject: [PATCH 3/5] Update help message --- main.go | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/main.go b/main.go index e57ecf2..f66aced 100644 --- a/main.go +++ b/main.go @@ -79,7 +79,7 @@ Options: -t, --no-title turn off header --shell shell (default "sh") --shell-options additional shell options - --pty run on pty (experimental) + --pty run on pty (experimental, not for Windows) -h, --help display this help and exit -v, --version output version information and exit`) From 94d5f7f078141a1c1fa66e130caa052dd65ae5c2 Mon Sep 17 00:00:00 2001 From: Takumasa Sakao Date: Sat, 18 Sep 2021 22:51:01 +0900 Subject: [PATCH 4/5] Use upstream tview --- go.mod | 6 ++---- go.sum | 4 ++++ run.go | 1 - viddy.go | 8 ++++---- 4 files changed, 10 insertions(+), 9 deletions(-) diff --git a/go.mod b/go.mod index 1d78074..8339aa5 100644 --- a/go.mod +++ b/go.mod @@ -10,7 +10,7 @@ require ( github.com/fatih/color v1.12.0 github.com/fsnotify/fsnotify v1.5.1 // indirect github.com/gdamore/encoding v1.0.0 // indirect - github.com/gdamore/tcell/v2 v2.4.1-0.20210904044819-ae5116d72813 + github.com/gdamore/tcell/v2 v2.4.1-0.20210905002822-f057f0a857a1 github.com/google/go-github v17.0.0+incompatible // indirect github.com/google/go-querystring v1.1.0 // indirect github.com/hashicorp/go-version v1.3.0 // indirect @@ -24,7 +24,7 @@ require ( github.com/moby/term v0.0.0-20210619224110-3f7ff695adc6 github.com/pelletier/go-toml v1.9.3 // indirect github.com/pmezard/go-difflib v1.0.0 // indirect - github.com/rivo/tview v0.0.0-20210624165335-29d673af0ce2 + github.com/rivo/tview v0.0.0-20210909154944-f7430b878d17 github.com/rivo/uniseg v0.2.0 // indirect github.com/sergi/go-diff v1.2.0 github.com/spf13/afero v1.6.0 // indirect @@ -43,5 +43,3 @@ require ( gopkg.in/yaml.v2 v2.4.0 // indirect gopkg.in/yaml.v3 v3.0.0-20210107192922-496545a6307b // indirect ) - -replace github.com/rivo/tview v0.0.0-20210624165335-29d673af0ce2 => github.com/sachaos/tview v0.0.0-20210909084047-7f6f0b84f61c diff --git a/go.sum b/go.sum index 040bde0..be990ca 100644 --- a/go.sum +++ b/go.sum @@ -82,6 +82,8 @@ github.com/gdamore/encoding v1.0.0 h1:+7OoQ1Bc6eTm5niUzBa0Ctsh6JbMW6Ra+YNuAtDBdk github.com/gdamore/encoding v1.0.0/go.mod h1:alR0ol34c49FCSBLjhosxzcPHQbf2trDkoo5dl+VrEg= github.com/gdamore/tcell/v2 v2.4.1-0.20210904044819-ae5116d72813 h1:uqlt4EHPdtAAXKBq6OKc0auHsQP5zfhdHo/BYe6hz2Q= github.com/gdamore/tcell/v2 v2.4.1-0.20210904044819-ae5116d72813/go.mod h1:Az6Jt+M5idSED2YPGtwnfJV0kXohgdCBPmHGSYc1r04= +github.com/gdamore/tcell/v2 v2.4.1-0.20210905002822-f057f0a857a1 h1:QqwPZCwh/k1uYqq6uXSb9TRDhTkfQbO80v8zhnIe5zM= +github.com/gdamore/tcell/v2 v2.4.1-0.20210905002822-f057f0a857a1/go.mod h1:Az6Jt+M5idSED2YPGtwnfJV0kXohgdCBPmHGSYc1r04= github.com/ghodss/yaml v1.0.0/go.mod h1:4dBDuWmgqj2HViK6kFavaiC9ZROes6MMH2rRYeMEF04= github.com/go-gl/glfw v0.0.0-20190409004039-e6da0acd62b1/go.mod h1:vR7hzQXu2zJy9AVAgeJqvqgH9Q5CA+iKCZ2gyEVpxRU= github.com/go-gl/glfw/v3.3/glfw v0.0.0-20191125211704-12ad95a8df72/go.mod h1:tQ2UAYgL5IevRw8kRxooKSPJfGvJ9fJQFa0TUsXzTg8= @@ -233,6 +235,8 @@ github.com/pmezard/go-difflib v1.0.0 h1:4DBwDE0NGyQoBHbLQYPwSUPoCMWR5BEzIk/f1lZb github.com/pmezard/go-difflib v1.0.0/go.mod h1:iKH77koFhYxTK1pcRnkKkqfTogsbg7gZNVY4sRDYZ/4= github.com/posener/complete v1.1.1/go.mod h1:em0nMJCgc9GFtwrmVmEMR/ZL6WyhyjMBndrE9hABlRI= github.com/prometheus/client_model v0.0.0-20190812154241-14fe0d1b01d4/go.mod h1:xMI15A0UPsDsEKsMN9yxemIoYk6Tm2C1GtYGdfGttqA= +github.com/rivo/tview v0.0.0-20210909154944-f7430b878d17 h1:Hvt6NxqL7PhMn3SpabVqp8bFkf8xR3tzXYbMFF0z0DI= +github.com/rivo/tview v0.0.0-20210909154944-f7430b878d17/go.mod h1:WIfMkQNY+oq/mWwtsjOYHIZBuwthioY2srOmljJkTnk= github.com/rivo/uniseg v0.2.0 h1:S1pD9weZBuJdFmowNwbpi7BJ8TNftyUImj/0WQi72jY= github.com/rivo/uniseg v0.2.0/go.mod h1:J6wj4VEh+S6ZtnVlnTBMWIodfgj8LQOQFoIToxlJtxc= github.com/rogpeppe/fastuuid v1.2.0/go.mod h1:jVj6XXZzXRy/MSR5jhDC/2q6DgLz+nrA6LYCDYWNEvQ= diff --git a/run.go b/run.go index f964055..3f97512 100644 --- a/run.go +++ b/run.go @@ -29,7 +29,6 @@ func (s *Snapshot) run(finishedQueue chan<- int64, width int, isPty bool) error pty, err := pty.StartWithSize(command, &pty.Winsize{ Cols: uint16(width), }) - if err != nil { return nil } diff --git a/viddy.go b/viddy.go index 8058629..298b76f 100644 --- a/viddy.go +++ b/viddy.go @@ -11,13 +11,11 @@ import ( "strconv" "strings" "sync" - "time" - - "github.com/moby/term" - "sync/atomic" + "time" "github.com/gdamore/tcell/v2" + "github.com/moby/term" "github.com/rivo/tview" ) @@ -321,7 +319,9 @@ func (v *Viddy) renderSnapshot(id int64) error { return errCannotCreateSnapshot } + v.bodyView.Lock() v.bodyView.Clear() + v.bodyView.Unlock() if !s.completed { return errNotCompletedYet From 2db70848512bf1943fee972d53131dac62ebaef3 Mon Sep 17 00:00:00 2001 From: Takumasa Sakao Date: Sat, 18 Sep 2021 22:52:19 +0900 Subject: [PATCH 5/5] Fix lint errors --- run.go | 3 +-- snapshot.go | 1 + 2 files changed, 2 insertions(+), 2 deletions(-) diff --git a/run.go b/run.go index 3f97512..d2d0ad5 100644 --- a/run.go +++ b/run.go @@ -10,7 +10,6 @@ import ( "github.com/creack/pty" ) -//nolint:unparam func (s *Snapshot) run(finishedQueue chan<- int64, width int, isPty bool) error { s.start = time.Now() defer func() { @@ -30,7 +29,7 @@ func (s *Snapshot) run(finishedQueue chan<- int64, width int, isPty bool) error Cols: uint16(width), }) if err != nil { - return nil + return err } go func() { diff --git a/snapshot.go b/snapshot.go index 31714e3..7d4de30 100644 --- a/snapshot.go +++ b/snapshot.go @@ -109,6 +109,7 @@ func (s *Snapshot) prepareCommand(commands []string) *exec.Cmd { args = append(args, strings.Join(commands, " ")) command = exec.Command(s.shell, args...) //nolint:gosec } + return command }