2015-09-30 00:11:46 +02:00
|
|
|
/*
|
|
|
|
* Quick - Quick key value store for config files and persistent state files
|
|
|
|
*
|
2019-04-09 20:39:42 +02:00
|
|
|
* Quick (C) 2015 MinIO, Inc.
|
2015-09-30 00:11:46 +02:00
|
|
|
*
|
|
|
|
* Licensed under the Apache License, Version 2.0 (the "License");
|
|
|
|
* you may not use this file except in compliance with the License.
|
|
|
|
* You may obtain a copy of the License at
|
|
|
|
*
|
|
|
|
* http://www.apache.org/licenses/LICENSE-2.0
|
|
|
|
*
|
|
|
|
* Unless required by applicable law or agreed to in writing, software
|
|
|
|
* distributed under the License is distributed on an "AS IS" BASIS,
|
|
|
|
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
|
|
|
|
* See the License for the specific language governing permissions and
|
|
|
|
* limitations under the License.
|
|
|
|
*/
|
|
|
|
|
|
|
|
package quick
|
|
|
|
|
|
|
|
import (
|
|
|
|
"bufio"
|
|
|
|
"bytes"
|
|
|
|
"fmt"
|
|
|
|
"io"
|
|
|
|
|
2016-06-13 04:35:28 +02:00
|
|
|
"github.com/cheggaaa/pb"
|
2015-09-30 00:11:46 +02:00
|
|
|
)
|
|
|
|
|
2017-05-06 19:16:59 +02:00
|
|
|
const errorFmt = "%5d: %s <<<<"
|
2015-09-30 00:11:46 +02:00
|
|
|
|
|
|
|
// FormatJSONSyntaxError generates a pretty printed json syntax error since
|
|
|
|
// golang doesn't provide an easy way to report the location of the error
|
2017-05-06 19:16:59 +02:00
|
|
|
func FormatJSONSyntaxError(data io.Reader, offset int64) (highlight string) {
|
2015-09-30 00:11:46 +02:00
|
|
|
var readLine bytes.Buffer
|
2017-05-06 19:16:59 +02:00
|
|
|
var errLine = 1
|
|
|
|
var readBytes int64
|
2015-09-30 00:11:46 +02:00
|
|
|
|
|
|
|
bio := bufio.NewReader(data)
|
|
|
|
|
|
|
|
// termWidth is set to a default one to use when we are
|
|
|
|
// not able to calculate terminal width via OS syscalls
|
|
|
|
termWidth := 25
|
|
|
|
|
|
|
|
// errorShift is the length of the minimum needed place for
|
2016-08-15 11:44:48 +02:00
|
|
|
// error msg accessories, like <--, etc.. We calculate it
|
2015-09-30 00:11:46 +02:00
|
|
|
// dynamically to avoid an eventual bug after modifying errorFmt
|
|
|
|
errorShift := len(fmt.Sprintf(errorFmt, 1, ""))
|
|
|
|
|
2016-06-13 04:35:28 +02:00
|
|
|
if width, err := pb.GetTerminalWidth(); err == nil {
|
|
|
|
termWidth = width
|
2015-09-30 00:11:46 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
for {
|
|
|
|
b, err := bio.ReadByte()
|
|
|
|
if err != nil {
|
|
|
|
break
|
|
|
|
}
|
|
|
|
readBytes++
|
2017-05-06 19:16:59 +02:00
|
|
|
if readBytes > offset {
|
2015-09-30 00:11:46 +02:00
|
|
|
break
|
|
|
|
}
|
2019-02-13 13:59:36 +01:00
|
|
|
if b == '\n' {
|
2015-09-30 00:11:46 +02:00
|
|
|
readLine.Reset()
|
|
|
|
errLine++
|
2019-02-13 13:59:36 +01:00
|
|
|
continue
|
|
|
|
} else if b == '\t' {
|
2015-09-30 00:11:46 +02:00
|
|
|
readLine.WriteByte(' ')
|
2019-02-13 13:59:36 +01:00
|
|
|
} else if b == '\r' {
|
2015-09-30 00:11:46 +02:00
|
|
|
break
|
|
|
|
}
|
2019-02-13 13:59:36 +01:00
|
|
|
readLine.WriteByte(b)
|
2015-09-30 00:11:46 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
lineLen := readLine.Len()
|
|
|
|
idx := lineLen - termWidth + errorShift
|
|
|
|
if idx < 0 || idx > lineLen-1 {
|
|
|
|
idx = 0
|
|
|
|
}
|
|
|
|
|
2017-05-06 19:16:59 +02:00
|
|
|
return fmt.Sprintf(errorFmt, errLine, readLine.String()[idx:])
|
2015-09-30 00:11:46 +02:00
|
|
|
}
|