@@ -15,15 +15,10 @@ import (
1515 "golang.org/x/term"
1616)
1717
18- var (
19- // Period between polls for terminal size changes.
20- // 10ms is the default, human reaction times are an order of magnitude slower than this interval,
21- // and auto generated escape sequence bytes are an order of magnitude faster than this interval.
22- SIZE_POLLING_INTERVAL = 10 * time .Millisecond
23-
24- // Used by the package maintainer:
25- DEBUG = "" // a non-empty string specifies the destination file for debugging info
26- )
18+ // Period between polls for terminal size changes.
19+ // 10ms is the default, human reaction times are an order of magnitude slower than this interval,
20+ // and auto generated escape sequence bytes are an order of magnitude faster than this interval.
21+ var SIZE_POLLING_INTERVAL = 10 * time .Millisecond
2722
2823type (
2924 StatusWidgetFn func (* Repl ) string
@@ -38,9 +33,10 @@ type Repl struct {
3833
3934 StatusWidgets * StatusWidgetFns
4035
41- history [][]byte // simply keep everything, it doesn't matter
42- historyIdx int // -1 for last
43- historyFile * os.File // open history file, so we can keep appending
36+ history [][]byte // simply keep everything, it doesn't matter
37+ historyIdx int // -1 for last
38+ historyMaxLines int
39+ historyFile * os.File // open history file, so we can keep appending
4440
4541 phraseRe * regexp.Regexp
4642
@@ -75,28 +71,30 @@ type Options struct {
7571// Create a new Repl using your custom Handler.
7672func NewRepl (handler Handler , opts * Options ) * Repl {
7773 r := & Repl {
78- handler : handler ,
79- history : make ([][]byte , 0 ),
80- historyIdx : - 1 ,
81- historyFile : nil ,
82- phraseRe : regexp .MustCompile (`([0-9a-zA-Z_\-\.]+)` ),
83- reader : newStdinReader (),
84- buffer : nil ,
85- backup : nil ,
86- prevDel : nil ,
87- filter : nil ,
88- bufferPos : 0 ,
89- viewStart : 0 ,
90- viewEnd : - 1 ,
91- promptRow : - 1 ,
92- width : 0 ,
93- height : 0 ,
94- onEnd : nil ,
95- debug : nil ,
96- }
97-
98- if DEBUG != "" {
99- debug , err := os .Create (DEBUG )
74+ handler : handler ,
75+ history : make ([][]byte , 0 ),
76+ historyIdx : - 1 ,
77+ historyFile : nil ,
78+ historyMaxLines : 1000 ,
79+ phraseRe : regexp .MustCompile (`([0-9a-zA-Z_\-\.]+)` ),
80+ reader : newStdinReader (),
81+ buffer : nil ,
82+ backup : nil ,
83+ prevDel : nil ,
84+ filter : nil ,
85+ bufferPos : 0 ,
86+ viewStart : 0 ,
87+ viewEnd : - 1 ,
88+ promptRow : - 1 ,
89+ width : 0 ,
90+ height : 0 ,
91+ onEnd : nil ,
92+ debug : nil ,
93+ }
94+
95+ debug := os .Getenv ("REPL_DEBUG_LOG" )
96+ if debug != "" {
97+ debug , err := os .Create (debug )
10098 if err != nil {
10199 panic (fmt .Errorf ("error start repl (debug): %w" , err ))
102100 }
@@ -124,6 +122,7 @@ func (r *Repl) loadOptions(opts *Options) error {
124122 }
125123
126124 r .historyFile = historyFile
125+ r .historyMaxLines = opts .HistoryMaxLines
127126
128127 // Load history file into the buffer
129128 if err := r .loadHistory (); err != nil {
@@ -165,6 +164,15 @@ func (r *Repl) saveHistory() error {
165164 return fmt .Errorf ("could not truncate history file: %w" , err )
166165 }
167166
167+ if _ , err := r .historyFile .Seek (0 , io .SeekStart ); err != nil {
168+ return fmt .Errorf ("could not seek to beginning after truncate: %w" , err )
169+ }
170+
171+ // truncate history buffer before save.
172+ if len (r .history ) > r .historyMaxLines {
173+ r .history = r .history [0 :r .historyMaxLines ]
174+ }
175+
168176 historyWriter := bufio .NewWriter (r .historyFile )
169177 for _ , line := range r .history {
170178 line := bytes .TrimSpace (line )
0 commit comments