Skip to content

Commit

Permalink
fix: -p and -P
Browse files Browse the repository at this point in the history
Seems like the options were there but where never really implemented.

This should fix it.

closes #181

Signed-off-by: Carlos Alexandro Becker <[email protected]>
  • Loading branch information
caarlos0 committed Jan 8, 2024
1 parent 8265897 commit 5698e34
Showing 1 changed file with 41 additions and 23 deletions.
64 changes: 41 additions & 23 deletions mods.go
Original file line number Diff line number Diff line change
Expand Up @@ -131,6 +131,18 @@ func (m *Mods) Update(msg tea.Msg) (tea.Model, tea.Cmd) {
if msg.content == "" && m.Config.Prefix == "" && m.Config.Show == "" && !m.Config.ShowLast {
return m, m.quit
}

if m.Config.IncludePromptArgs {
m.appendToOutput(m.Config.Prefix + "\n\n")
}

if m.Config.IncludePrompt > 0 {
parts := strings.Split(m.Input, "\n")
if len(parts) > m.Config.IncludePrompt {
parts = parts[0:m.Config.IncludePrompt]
}
m.appendToOutput(strings.Join(parts, "\n") + "\n")
}
m.state = requestState
cmds = append(cmds, m.startCompletionCmd(msg.content))
case completionOutput:
Expand All @@ -139,29 +151,7 @@ func (m *Mods) Update(msg tea.Msg) (tea.Model, tea.Cmd) {
return m, m.quit
}
if msg.content != "" {
m.Output += msg.content
if !isOutputTTY() || m.Config.Raw {
m.contentMutex.Lock()
m.content = append(m.content, msg.content)
m.contentMutex.Unlock()
} else {
const tabWidth = 4
wasAtBottom := m.glamViewport.ScrollPercent() == 1.0
oldHeight := m.glamHeight
m.glamOutput, _ = m.glam.Render(m.Output)
m.glamOutput = strings.TrimRightFunc(m.glamOutput, unicode.IsSpace)
m.glamOutput = strings.ReplaceAll(m.glamOutput, "\t", strings.Repeat(" ", tabWidth))
m.glamHeight = lipgloss.Height(m.glamOutput)
m.glamOutput += "\n"
truncatedGlamOutput := m.renderer.NewStyle().MaxWidth(m.width).Render(m.glamOutput)
m.glamViewport.SetContent(truncatedGlamOutput)
if oldHeight < m.glamHeight && wasAtBottom {
// If the viewport's at the bottom and we've received a new
// line of content, follow the output by auto scrolling to
// the bottom.
m.glamViewport.GotoBottom()
}
}
m.appendToOutput(msg.content)
m.state = responseState
}
cmds = append(cmds, m.receiveCompletionStreamCmd(msg))
Expand Down Expand Up @@ -600,3 +590,31 @@ func (m *Mods) readFromCache() tea.Cmd {
})()
}
}

const tabWidth = 4

func (m *Mods) appendToOutput(s string) {
m.Output += s
if !isOutputTTY() || m.Config.Raw {
m.contentMutex.Lock()
m.content = append(m.content, s)
m.contentMutex.Unlock()
return
}

wasAtBottom := m.glamViewport.ScrollPercent() == 1.0
oldHeight := m.glamHeight
m.glamOutput, _ = m.glam.Render(m.Output)
m.glamOutput = strings.TrimRightFunc(m.glamOutput, unicode.IsSpace)
m.glamOutput = strings.ReplaceAll(m.glamOutput, "\t", strings.Repeat(" ", tabWidth))
m.glamHeight = lipgloss.Height(m.glamOutput)
m.glamOutput += "\n"
truncatedGlamOutput := m.renderer.NewStyle().MaxWidth(m.width).Render(m.glamOutput)
m.glamViewport.SetContent(truncatedGlamOutput)
if oldHeight < m.glamHeight && wasAtBottom {
// If the viewport's at the bottom and we've received a new
// line of content, follow the output by auto scrolling to
// the bottom.
m.glamViewport.GotoBottom()
}
}

0 comments on commit 5698e34

Please sign in to comment.