func drawSide(g *gocui.Gui, side *gocui.View, filelist []string, fOffset int) { side.Clear() _, wheight := g.Size() wheight = wheight - 2*Padding if fOffset < window_offset { window_offset = fOffset } else if fOffset >= window_offset+wheight { window_offset = fOffset - wheight + 1 } end := wheight + window_offset if len(filelist) < end { end = len(filelist) } for i, name := range filelist { if i < window_offset || i >= end { continue } bname := filepath.Base(name) if i == fOffset { fmt.Fprintln(side, PrefixArrow+bname) } else { fmt.Fprintln(side, PrefixEmpty+bname) } } }
func layout(g *gocui.Gui) error { maxX, maxY := g.Size() if _, err := g.SetView("center", 0, 0, maxX*4/5, maxY-Padding); err != nil { if err != gocui.ErrorUnkView { return err } } if v, err := g.SetView("side", maxX*4/5+1, 0, maxX-Padding, maxY-Padding); err != nil { if err != gocui.ErrorUnkView { return err } _, height := g.Size() if height-2*Padding > len(files) { height = len(files) } for i := 0; i < height; i++ { name := filepath.Base(files[i]) if i == 0 { fmt.Fprintln(v, PrefixArrow+name) } else { fmt.Fprintln(v, PrefixEmpty+name) } } } return nil }
func prevOne(g *gocui.Gui, v *gocui.View) error { if lv := g.View("side"); lv != nil { _, height := g.Size() height = height - 2*Padding length := len(files) file_offset = (file_offset - 1 + length) % length drawSide(g, lv, files, file_offset) } return nil }
func drawCenter(g *gocui.Gui) { if v := g.View("center"); v != nil { _, height := g.Size() height = height - 2 if text, err := TailFile(files[file_offset], height); err == nil { if i := strings.Index(text, "\n"); i > 0 { re := regexp.MustCompile("\\033[[0-9]+m") line := re.ReplaceAllString(text[0:i], "") text = line + text[i:] } else { re := regexp.MustCompile("\\033[[0-9]+m") text = re.ReplaceAllString(text, "") } v.Clear() fmt.Fprintf(v, "%s", text) g.Flush() } else { v.Clear() fmt.Fprintf(v, "------ failed to read from %s ------\n", filepath.Base(files[file_offset])) g.Flush() } } }