add 'period' option for periodic refresh

Related #73
This commit is contained in:
Gokcehan 2018-06-09 22:02:09 +03:00
parent e025c68130
commit 723357fe3d
6 changed files with 30 additions and 0 deletions

7
app.go
View File

@ -9,6 +9,7 @@ import (
"os/exec" "os/exec"
"strconv" "strconv"
"strings" "strings"
"time"
) )
type cmdItem struct { type cmdItem struct {
@ -19,6 +20,7 @@ type cmdItem struct {
type app struct { type app struct {
ui *ui ui *ui
nav *nav nav *nav
ticker *time.Ticker
quitChan chan bool quitChan chan bool
cmd *exec.Cmd cmd *exec.Cmd
cmdIn io.WriteCloser cmdIn io.WriteCloser
@ -34,6 +36,7 @@ func newApp() *app {
return &app{ return &app{
ui: ui, ui: ui,
nav: nav, nav: nav,
ticker: new(time.Ticker),
quitChan: make(chan bool, 1), quitChan: make(chan bool, 1),
} }
} }
@ -132,6 +135,10 @@ func (app *app) loop() {
case e := <-serverChan: case e := <-serverChan:
e.eval(app, nil) e.eval(app, nil)
app.ui.draw(app.nav) app.ui.draw(app.nav)
case <-app.ticker.C:
app.nav.renew()
app.ui.loadFile(app.nav)
app.ui.draw(app.nav)
} }
} }
} }

View File

@ -82,6 +82,7 @@ var (
"wrapscan", "wrapscan",
"nowrapscan", "nowrapscan",
"wrapscan!", "wrapscan!",
"period",
"scrolloff", "scrolloff",
"tabstop", "tabstop",
"filesep", "filesep",

1
doc.go
View File

@ -91,6 +91,7 @@ The following options can be used to customize the behavior of lf:
reverse boolean (default off) reverse boolean (default off)
smartcase boolean (default on) smartcase boolean (default on)
wrapscan boolean (default on) wrapscan boolean (default on)
period integer (default 0)
scrolloff integer (default 0) scrolloff integer (default 0)
tabstop integer (default 8) tabstop integer (default 8)
filesep string (default "\n") filesep string (default "\n")

View File

@ -95,6 +95,7 @@ The following options can be used to customize the behavior of lf:
reverse boolean (default off) reverse boolean (default off)
smartcase boolean (default on) smartcase boolean (default on)
wrapscan boolean (default on) wrapscan boolean (default on)
period integer (default 0)
scrolloff integer (default 0) scrolloff integer (default 0)
tabstop integer (default 8) tabstop integer (default 8)
filesep string (default "\n") filesep string (default "\n")

18
eval.go
View File

@ -6,6 +6,7 @@ import (
"os" "os"
"strconv" "strconv"
"strings" "strings"
"time"
"unicode" "unicode"
"unicode/utf8" "unicode/utf8"
) )
@ -96,6 +97,23 @@ func (e *setExpr) eval(app *app, args []string) {
gOpts.wrapscan = false gOpts.wrapscan = false
case "wrapscan!": case "wrapscan!":
gOpts.wrapscan = !gOpts.wrapscan gOpts.wrapscan = !gOpts.wrapscan
case "period":
n, err := strconv.Atoi(e.val)
if err != nil {
app.ui.printf("period: %s", err)
return
}
if n < 0 {
app.ui.print("period: value should be a non-negative number")
return
}
gOpts.period = n
if n == 0 {
app.ticker.Stop()
} else {
app.ticker.Stop()
app.ticker = time.NewTicker(time.Duration(gOpts.period) * time.Second)
}
case "scrolloff": case "scrolloff":
n, err := strconv.Atoi(e.val) n, err := strconv.Atoi(e.val)
if err != nil { if err != nil {

View File

@ -32,6 +32,7 @@ var gOpts struct {
preview bool preview bool
smartcase bool smartcase bool
wrapscan bool wrapscan bool
period int
scrolloff int scrolloff int
tabstop int tabstop int
filesep string filesep string
@ -56,6 +57,7 @@ func init() {
gOpts.preview = true gOpts.preview = true
gOpts.smartcase = true gOpts.smartcase = true
gOpts.wrapscan = true gOpts.wrapscan = true
gOpts.period = 0
gOpts.scrolloff = 0 gOpts.scrolloff = 0
gOpts.tabstop = 8 gOpts.tabstop = 8
gOpts.filesep = "\n" gOpts.filesep = "\n"