use rho_protocol::SettingKey;
use super::{
daemon_settings::SettingsNamespace,
line_editor::LineEditor,
line_editor_view::EditorPresentation,
picker::UiPicker,
view_composer::{editor_frame, ComposerFrame},
};
#[derive(Clone, Debug, PartialEq, Eq)]
pub(super) enum TextInputTarget {
Setting {
namespace: SettingsNamespace,
key: SettingKey,
label: String,
},
AgentField(AgentField),
CustomHost(super::custom_provider_login::CustomHostStep),
}
#[derive(Clone, Copy, Debug, PartialEq, Eq)]
pub(super) enum AgentField {
Description,
Model,
Provider,
Tools,
}
impl AgentField {
pub(super) fn label(self) -> &'static str {
match self {
Self::Description => "description",
Self::Model => "model",
Self::Provider => "provider",
Self::Tools => "tools",
}
}
pub(super) const fn value(self) -> &'static str {
match self {
Self::Description => "agent_field:description",
Self::Model => "agent_field:model",
Self::Provider => "agent_field:provider",
Self::Tools => "agent_field:tools",
}
}
}
#[derive(Clone, Debug)]
pub(super) struct TextInput {
pub(super) target: TextInputTarget,
pub(super) editor: LineEditor,
}
impl TextInput {
pub(super) fn setting(
namespace: SettingsNamespace,
key: SettingKey,
label: String,
value: String,
) -> Self {
Self {
target: TextInputTarget::Setting {
namespace,
key,
label,
},
editor: LineEditor::new(value),
}
}
pub(super) fn agent_field(field: AgentField, value: impl Into<String>) -> Self {
Self {
target: TextInputTarget::AgentField(field),
editor: LineEditor::new(value),
}
}
pub(super) fn custom_host(
step: super::custom_provider_login::CustomHostStep,
value: impl Into<String>,
) -> Self {
Self {
target: TextInputTarget::CustomHost(step),
editor: LineEditor::new(value),
}
}
pub(super) fn with_return_picker(mut self, picker: UiPicker) -> Self {
self.editor = self.editor.with_return_picker(picker);
self
}
pub(super) fn take_return_picker(&mut self) -> Option<UiPicker> {
self.editor.take_return_picker()
}
fn presentation(&self) -> EditorPresentation {
match self.target {
TextInputTarget::Setting { .. }
| TextInputTarget::AgentField(_)
| TextInputTarget::CustomHost(_) => EditorPresentation::Plain,
}
}
pub(super) fn label(&self) -> &str {
match &self.target {
TextInputTarget::Setting { label, .. } => label,
TextInputTarget::AgentField(field) => field.label(),
TextInputTarget::CustomHost(step) => step.label(),
}
}
fn confirm_verb(&self) -> &'static str {
match &self.target {
TextInputTarget::CustomHost(_) => "Enter continue",
TextInputTarget::Setting { .. } | TextInputTarget::AgentField(_) => "Enter save",
}
}
}
pub(super) fn text_input_frame(input: &TextInput, width: usize) -> ComposerFrame {
let prompt = format!(
"edit {} {}",
input.label(),
super::composer_chrome::join_footer_parts([input.confirm_verb(), "Esc cancel"])
);
let presentation = input.presentation();
editor_frame(&prompt, input.editor.viewport(presentation, width), width)
}