Skip to content
GitLab
Menu
Projects
Groups
Snippets
Loading...
Help
Help
Support
Community forum
Keyboard shortcuts
?
Submit feedback
Contribute to GitLab
Sign in / Register
Toggle navigation
Menu
Open sidebar
OpenDAS
ollama
Commits
99d5161e
Unverified
Commit
99d5161e
authored
Oct 02, 2023
by
Patrick Devine
Committed by
GitHub
Oct 02, 2023
Browse files
don't wordwrap when stdout is redirected or piped (#662)
parent
ea8380be
Changes
1
Hide whitespace changes
Inline
Side-by-side
Showing
1 changed file
with
37 additions
and
24 deletions
+37
-24
cmd/cmd.go
cmd/cmd.go
+37
-24
No files found.
cmd/cmd.go
View file @
99d5161e
...
...
@@ -380,7 +380,20 @@ func pull(model string, insecure bool) error {
func
RunGenerate
(
cmd
*
cobra
.
Command
,
args
[]
string
)
error
{
if
len
(
args
)
>
1
{
// join all args into a single prompt
return
generate
(
cmd
,
args
[
0
],
strings
.
Join
(
args
[
1
:
],
" "
))
wordWrap
:=
false
if
term
.
IsTerminal
(
int
(
os
.
Stdout
.
Fd
()))
{
wordWrap
=
true
}
nowrap
,
err
:=
cmd
.
Flags
()
.
GetBool
(
"nowordwrap"
)
if
err
!=
nil
{
return
err
}
if
nowrap
{
wordWrap
=
false
}
return
generate
(
cmd
,
args
[
0
],
strings
.
Join
(
args
[
1
:
],
" "
),
wordWrap
)
}
if
readline
.
IsTerminal
(
int
(
os
.
Stdin
.
Fd
()))
{
...
...
@@ -392,7 +405,7 @@ func RunGenerate(cmd *cobra.Command, args []string) error {
type
generateContextKey
string
func
generate
(
cmd
*
cobra
.
Command
,
model
,
prompt
string
)
error
{
func
generate
(
cmd
*
cobra
.
Command
,
model
,
prompt
string
,
wordWrap
bool
)
error
{
client
,
err
:=
api
.
FromEnv
()
if
err
!=
nil
{
return
err
...
...
@@ -408,24 +421,9 @@ func generate(cmd *cobra.Command, model, prompt string) error {
generateContext
=
[]
int
{}
}
var
wrapTerm
bool
termType
:=
os
.
Getenv
(
"TERM"
)
if
termType
==
"xterm-256color"
{
wrapTerm
=
true
}
termWidth
,
_
,
err
:=
term
.
GetSize
(
int
(
0
))
if
err
!=
nil
{
wrapTerm
=
false
}
// override wrapping if the user turned it off
nowrap
,
err
:=
cmd
.
Flags
()
.
GetBool
(
"nowordwrap"
)
if
err
!=
nil
{
return
err
}
if
nowrap
{
wrapTerm
=
false
wordWrap
=
false
}
cancelCtx
,
cancel
:=
context
.
WithCancel
(
context
.
Background
())
...
...
@@ -452,7 +450,7 @@ func generate(cmd *cobra.Command, model, prompt string) error {
latest
=
response
if
w
rapTerm
{
if
w
ordWrap
{
for
_
,
ch
:=
range
response
.
Response
{
if
currentLineLength
+
1
>
termWidth
-
5
{
// backtrack the length of the last word and clear to the end of the line
...
...
@@ -533,7 +531,7 @@ func generateInteractive(cmd *cobra.Command, model string) error {
}
// load the model
if
err
:=
generate
(
cmd
,
model
,
""
);
err
!=
nil
{
if
err
:=
generate
(
cmd
,
model
,
""
,
false
);
err
!=
nil
{
return
err
}
...
...
@@ -579,6 +577,21 @@ func generateInteractive(cmd *cobra.Command, model string) error {
}
defer
scanner
.
Close
()
var
wordWrap
bool
termType
:=
os
.
Getenv
(
"TERM"
)
if
termType
==
"xterm-256color"
{
wordWrap
=
true
}
// override wrapping if the user turned it off
nowrap
,
err
:=
cmd
.
Flags
()
.
GetBool
(
"nowordwrap"
)
if
err
!=
nil
{
return
err
}
if
nowrap
{
wordWrap
=
false
}
var
multiLineBuffer
string
var
isMultiLine
bool
...
...
@@ -632,10 +645,10 @@ func generateInteractive(cmd *cobra.Command, model string) error {
case
"nohistory"
:
scanner
.
HistoryDisable
()
case
"wordwrap"
:
cmd
.
Flags
()
.
Set
(
"no
word
w
rap
"
,
"false"
)
word
W
rap
=
true
fmt
.
Println
(
"Set 'wordwrap' mode."
)
case
"nowordwrap"
:
cmd
.
Flags
()
.
Set
(
"no
word
w
rap
"
,
"true"
)
word
W
rap
=
false
fmt
.
Println
(
"Set 'nowordwrap' mode."
)
case
"verbose"
:
cmd
.
Flags
()
.
Set
(
"verbose"
,
"true"
)
...
...
@@ -698,7 +711,7 @@ func generateInteractive(cmd *cobra.Command, model string) error {
}
if
len
(
line
)
>
0
&&
line
[
0
]
!=
'/'
{
if
err
:=
generate
(
cmd
,
model
,
line
);
err
!=
nil
{
if
err
:=
generate
(
cmd
,
model
,
line
,
wordWrap
);
err
!=
nil
{
return
err
}
}
...
...
@@ -710,7 +723,7 @@ func generateBatch(cmd *cobra.Command, model string) error {
for
scanner
.
Scan
()
{
prompt
:=
scanner
.
Text
()
fmt
.
Printf
(
">>> %s
\n
"
,
prompt
)
if
err
:=
generate
(
cmd
,
model
,
prompt
);
err
!=
nil
{
if
err
:=
generate
(
cmd
,
model
,
prompt
,
false
);
err
!=
nil
{
return
err
}
}
...
...
Write
Preview
Markdown
is supported
0%
Try again
or
attach a new file
.
Attach a file
Cancel
You are about to add
0
people
to the discussion. Proceed with caution.
Finish editing this message first!
Cancel
Please
register
or
sign in
to comment