Add mouse wheel support to pager example

This commit is contained in:
Christian Rocha 2020-06-23 12:01:23 -04:00
parent d6cf9e69b6
commit de147c5061
No known key found for this signature in database
GPG Key ID: D6CC7A16E5878018
4 changed files with 35 additions and 16 deletions

View File

@ -5,9 +5,11 @@ go 1.13
replace github.com/charmbracelet/bubbletea => ../ replace github.com/charmbracelet/bubbletea => ../
require ( require (
github.com/charmbracelet/bubbles v0.2.1-0.20200621033419-e9dd6b06e039 github.com/charmbracelet/bubbles v0.3.1-0.20200623160017-c9196e5407ad
github.com/charmbracelet/bubbletea v0.8.3-0.20200622174044-8bab4bce9a84 github.com/charmbracelet/bubbletea v0.9.1-0.20200623143446-d6cf9e69b6f2
github.com/fogleman/ease v0.0.0-20170301025033-8da417bf1776 github.com/fogleman/ease v0.0.0-20170301025033-8da417bf1776
github.com/mattn/go-runewidth v0.0.9 github.com/mattn/go-runewidth v0.0.9
github.com/muesli/termenv v0.5.3-0.20200617154346-da5de72b61c0 github.com/muesli/termenv v0.5.3-0.20200617154346-da5de72b61c0
golang.org/x/crypto v0.0.0-20200622213623-75b288015ac9 // indirect
golang.org/x/sys v0.0.0-20200622214017-ed371f2e16b4 // indirect
) )

View File

@ -1,5 +1,6 @@
github.com/charmbracelet/bubbles v0.2.1-0.20200621033419-e9dd6b06e039 h1:fdkwef6LvD1P6kpsjeG3D1CUj8nCBdW1jphBBkh5cIU=
github.com/charmbracelet/bubbles v0.2.1-0.20200621033419-e9dd6b06e039/go.mod h1:ufe3wvuHegIN7HB0nz4+WDQ2AcHaU8OoRSnfq7usvq4= github.com/charmbracelet/bubbles v0.2.1-0.20200621033419-e9dd6b06e039/go.mod h1:ufe3wvuHegIN7HB0nz4+WDQ2AcHaU8OoRSnfq7usvq4=
github.com/charmbracelet/bubbles v0.3.1-0.20200623160017-c9196e5407ad h1:oVOOqMWwb1ST9nu9vWGLlFC/dJWQlkUnsJbElOo1BPM=
github.com/charmbracelet/bubbles v0.3.1-0.20200623160017-c9196e5407ad/go.mod h1:3v6yfT4kd1oOivS8uOXyWvDo1DaXO3R88mlijgPDo7w=
github.com/fogleman/ease v0.0.0-20170301025033-8da417bf1776 h1:VRIbnDWRmAh5yBdz+J6yFMF5vso1It6vn+WmM/5l7MA= github.com/fogleman/ease v0.0.0-20170301025033-8da417bf1776 h1:VRIbnDWRmAh5yBdz+J6yFMF5vso1It6vn+WmM/5l7MA=
github.com/fogleman/ease v0.0.0-20170301025033-8da417bf1776/go.mod h1:9wvnDu3YOfxzWM9Cst40msBF1C2UdQgDv962oTxSuMs= github.com/fogleman/ease v0.0.0-20170301025033-8da417bf1776/go.mod h1:9wvnDu3YOfxzWM9Cst40msBF1C2UdQgDv962oTxSuMs=
github.com/google/goterm v0.0.0-20190703233501-fc88cf888a3f h1:5CjVwnuUcp5adK4gmY6i72gpVFVnZDP2h5TmPScB6u4= github.com/google/goterm v0.0.0-20190703233501-fc88cf888a3f h1:5CjVwnuUcp5adK4gmY6i72gpVFVnZDP2h5TmPScB6u4=
@ -10,7 +11,6 @@ github.com/mattn/go-isatty v0.0.12 h1:wuysRhFDzyxgEmMf5xjvJ2M9dZoWAXNNr5LSBS7uHX
github.com/mattn/go-isatty v0.0.12/go.mod h1:cbi8OIDigv2wuxKPP5vlRcQ1OAZbq2CE4Kysco4FUpU= github.com/mattn/go-isatty v0.0.12/go.mod h1:cbi8OIDigv2wuxKPP5vlRcQ1OAZbq2CE4Kysco4FUpU=
github.com/mattn/go-runewidth v0.0.9 h1:Lm995f3rfxdpd6TSmuVCHVb/QhupuXlYr8sCI/QdE+0= github.com/mattn/go-runewidth v0.0.9 h1:Lm995f3rfxdpd6TSmuVCHVb/QhupuXlYr8sCI/QdE+0=
github.com/mattn/go-runewidth v0.0.9/go.mod h1:H031xJmbD/WCDINGzjvQ9THkh0rPKHF+m2gUSrubnMI= github.com/mattn/go-runewidth v0.0.9/go.mod h1:H031xJmbD/WCDINGzjvQ9THkh0rPKHF+m2gUSrubnMI=
github.com/muesli/termenv v0.5.3-0.20200617122104-66d435cfe924 h1:oQHK4eixHzrl+DmUKH0x7SJgbkQBiggektj3ChSuE9I=
github.com/muesli/termenv v0.5.3-0.20200617122104-66d435cfe924/go.mod h1:O1/I6sw+6KcrgAmcs6uiUVr7Lui+DNVbHTzt9Lm/PlI= github.com/muesli/termenv v0.5.3-0.20200617122104-66d435cfe924/go.mod h1:O1/I6sw+6KcrgAmcs6uiUVr7Lui+DNVbHTzt9Lm/PlI=
github.com/muesli/termenv v0.5.3-0.20200617154346-da5de72b61c0 h1:IASsj5pQ478TEN9TLiuhP+ugUQEotZ1QdzQvcJUQ1dM= github.com/muesli/termenv v0.5.3-0.20200617154346-da5de72b61c0 h1:IASsj5pQ478TEN9TLiuhP+ugUQEotZ1QdzQvcJUQ1dM=
github.com/muesli/termenv v0.5.3-0.20200617154346-da5de72b61c0/go.mod h1:O1/I6sw+6KcrgAmcs6uiUVr7Lui+DNVbHTzt9Lm/PlI= github.com/muesli/termenv v0.5.3-0.20200617154346-da5de72b61c0/go.mod h1:O1/I6sw+6KcrgAmcs6uiUVr7Lui+DNVbHTzt9Lm/PlI=
@ -19,6 +19,8 @@ github.com/pkg/term v0.0.0-20200520122047-c3ffed290a03/go.mod h1:Z9+Ul5bCbBKnbCv
golang.org/x/crypto v0.0.0-20190308221718-c2843e01d9a2/go.mod h1:djNgcEr1/C05ACkg1iLfiJU5Ep61QUkGW8qpdssI0+w= golang.org/x/crypto v0.0.0-20190308221718-c2843e01d9a2/go.mod h1:djNgcEr1/C05ACkg1iLfiJU5Ep61QUkGW8qpdssI0+w=
golang.org/x/crypto v0.0.0-20200604202706-70a84ac30bf9 h1:vEg9joUBmeBcK9iSJftGNf3coIG4HqZElCPehJsfAYM= golang.org/x/crypto v0.0.0-20200604202706-70a84ac30bf9 h1:vEg9joUBmeBcK9iSJftGNf3coIG4HqZElCPehJsfAYM=
golang.org/x/crypto v0.0.0-20200604202706-70a84ac30bf9/go.mod h1:LzIPMQfyMNhhGPhUkYOs5KpL4U8rLKemX1yGLhDgUto= golang.org/x/crypto v0.0.0-20200604202706-70a84ac30bf9/go.mod h1:LzIPMQfyMNhhGPhUkYOs5KpL4U8rLKemX1yGLhDgUto=
golang.org/x/crypto v0.0.0-20200622213623-75b288015ac9 h1:psW17arqaxU48Z5kZ0CQnkZWQJsqcURM6tKiBApRjXI=
golang.org/x/crypto v0.0.0-20200622213623-75b288015ac9/go.mod h1:LzIPMQfyMNhhGPhUkYOs5KpL4U8rLKemX1yGLhDgUto=
golang.org/x/net v0.0.0-20190404232315-eb5bcb51f2a3/go.mod h1:t9HGtf8HONx5eT2rtn7q6eTqICYqUVnKs3thJo3Qplg= golang.org/x/net v0.0.0-20190404232315-eb5bcb51f2a3/go.mod h1:t9HGtf8HONx5eT2rtn7q6eTqICYqUVnKs3thJo3Qplg=
golang.org/x/sys v0.0.0-20190215142949-d0b11bdaac8a/go.mod h1:STP8DvDyc/dI5b8T5hshtkjS+E42TnysNCUPdjciGhY= golang.org/x/sys v0.0.0-20190215142949-d0b11bdaac8a/go.mod h1:STP8DvDyc/dI5b8T5hshtkjS+E42TnysNCUPdjciGhY=
golang.org/x/sys v0.0.0-20190412213103-97732733099d/go.mod h1:h1NjWce9XRLGQEsW7wpKNCjG9DtNlClVuFLEZdDNbEs= golang.org/x/sys v0.0.0-20190412213103-97732733099d/go.mod h1:h1NjWce9XRLGQEsW7wpKNCjG9DtNlClVuFLEZdDNbEs=
@ -26,4 +28,6 @@ golang.org/x/sys v0.0.0-20200116001909-b77594299b42 h1:vEOn+mP2zCOVzKckCZy6YsCtD
golang.org/x/sys v0.0.0-20200116001909-b77594299b42/go.mod h1:h1NjWce9XRLGQEsW7wpKNCjG9DtNlClVuFLEZdDNbEs= golang.org/x/sys v0.0.0-20200116001909-b77594299b42/go.mod h1:h1NjWce9XRLGQEsW7wpKNCjG9DtNlClVuFLEZdDNbEs=
golang.org/x/sys v0.0.0-20200620081246-981b61492c35 h1:wb/9mP8eUAmHfkM8RmpeLq6nUA7c2i5+bQOtcDftjaE= golang.org/x/sys v0.0.0-20200620081246-981b61492c35 h1:wb/9mP8eUAmHfkM8RmpeLq6nUA7c2i5+bQOtcDftjaE=
golang.org/x/sys v0.0.0-20200620081246-981b61492c35/go.mod h1:h1NjWce9XRLGQEsW7wpKNCjG9DtNlClVuFLEZdDNbEs= golang.org/x/sys v0.0.0-20200620081246-981b61492c35/go.mod h1:h1NjWce9XRLGQEsW7wpKNCjG9DtNlClVuFLEZdDNbEs=
golang.org/x/sys v0.0.0-20200622214017-ed371f2e16b4 h1:5/PjkGUjvEU5Gl6BxmvKRPpqo2uNMv4rcHBMwzk/st8=
golang.org/x/sys v0.0.0-20200622214017-ed371f2e16b4/go.mod h1:h1NjWce9XRLGQEsW7wpKNCjG9DtNlClVuFLEZdDNbEs=
golang.org/x/text v0.3.0/go.mod h1:NqM8EUOU14njkJ3fqMW+pc6Ldnwhi/IjpwHt7yyuwOQ= golang.org/x/text v0.3.0/go.mod h1:NqM8EUOU14njkJ3fqMW+pc6Ldnwhi/IjpwHt7yyuwOQ=

View File

@ -40,25 +40,27 @@ func main() {
// //
// This becomes handy when debugging stuff since you can't debug to stdout // This becomes handy when debugging stuff since you can't debug to stdout
// because the UI is occupying it! // because the UI is occupying it!
p := os.Getenv("PAGER_LOG") path := os.Getenv("PAGER_LOG")
if p != "" { if path != "" {
f, err := tea.LogToFile(p, "pager") f, err := tea.LogToFile(path, "pager")
if err != nil { if err != nil {
fmt.Printf("Could not open file %s: %v", p, err) fmt.Printf("Could not open file %s: %v", path, err)
os.Exit(1) os.Exit(1)
} }
defer f.Close() defer f.Close()
} }
// Use the full size of the terminal in its "alternate screen buffer" p := tea.NewProgram(initialize(string(content)), update, view)
tea.AltScreen()
defer tea.ExitAltScreen()
if err := tea.NewProgram( // Use the full size of the terminal in its "alternate screen buffer"
initialize(string(content)), p.EnterAltScreen()
update, defer p.ExitAltScreen()
view,
).Start(); err != nil { // We also turn on mouse support so we can track the mouse wheel
p.EnableMouseCellMotion()
defer p.DisableMouseCellMotion()
if err := p.Start(); err != nil {
fmt.Println("could not run program:", err) fmt.Println("could not run program:", err)
os.Exit(1) os.Exit(1)
} }

11
tea.go
View File

@ -203,24 +203,35 @@ func (p *Program) ExitAltScreen() {
fmt.Fprintf(p.output, te.CSI+te.ExitAltScreenSeq) fmt.Fprintf(p.output, te.CSI+te.ExitAltScreenSeq)
} }
// EnableMouseCellMotion enables mouse click, release, wheel and motion events if a
// button is pressed.
func (p *Program) EnableMouseCellMotion() { func (p *Program) EnableMouseCellMotion() {
p.mtx.Lock() p.mtx.Lock()
defer p.mtx.Unlock() defer p.mtx.Unlock()
fmt.Fprintf(p.output, te.CSI+"?1002h") fmt.Fprintf(p.output, te.CSI+"?1002h")
} }
// DisableMouseCellMotino disables Mouse Cell Motion tracking. If you've
// enabled Cell Motion mouse trakcing be sure to call this as your program is
// exiting or your users will be very upset!
func (p *Program) DisableMouseCellMotion() { func (p *Program) DisableMouseCellMotion() {
p.mtx.Lock() p.mtx.Lock()
defer p.mtx.Unlock() defer p.mtx.Unlock()
fmt.Fprintf(p.output, te.CSI+"?1002l") fmt.Fprintf(p.output, te.CSI+"?1002l")
} }
// EnableMouseAllMotion enables mouse click, release, wheel and motion events,
// regardless of whether a button is pressed. Many modern terminals support
// this, but not all.
func (p *Program) EnableMouseAllMotion() { func (p *Program) EnableMouseAllMotion() {
p.mtx.Lock() p.mtx.Lock()
defer p.mtx.Unlock() defer p.mtx.Unlock()
fmt.Fprintf(p.output, te.CSI+"?1003h") fmt.Fprintf(p.output, te.CSI+"?1003h")
} }
// DisableMouseAllMotion disables All Motion mouse tracking. If you've enabled
// All Motion mouse tracking be sure you call this as your program is exiting
// or your users will be very upset!
func (p *Program) DisableMouseAllMotion() { func (p *Program) DisableMouseAllMotion() {
p.mtx.Lock() p.mtx.Lock()
defer p.mtx.Unlock() defer p.mtx.Unlock()