Skip to content

Commit

Permalink
config items, fix additional settings input
Browse files Browse the repository at this point in the history
  • Loading branch information
aprxi committed Sep 22, 2024
1 parent 1bb4695 commit bef30c9
Show file tree
Hide file tree
Showing 4 changed files with 187 additions and 123 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -180,25 +180,7 @@ impl Creator<ConfigItem> for ProfileCreator {
}

fn render(&mut self, f: &mut Frame, area: Rect) {
match self.sub_part_creation_state {
SubPartCreationState::NotCreating => match self.creation_step {
ProfileCreationStep::EnterName => {
self.render_enter_name(f, area)
}
ProfileCreationStep::SelectProvider => {
self.render_select_provider(f, area)
}
ProfileCreationStep::ConfirmCreate => {
self.render_confirm_create(f, area)
}
ProfileCreationStep::CreatingProfile => {
self.render_creating_profile(f, area)
}
},
SubPartCreationState::CreatingProvider(ref mut creator) => {
creator.render(f, area);
}
}
self.render_creator(f, area);
}

async fn create_item(
Expand Down Expand Up @@ -234,35 +216,18 @@ impl Creator<ConfigItem> for ProviderCreator {
}

fn render(&mut self, f: &mut Frame, area: Rect) {
match self.current_step {
ProviderCreationStep::EnterName => self.render_enter_name(f, area),
ProviderCreationStep::SelectProviderType => {
self.render_select_provider_type(f, area)
}
ProviderCreationStep::SelectModel => {
self.render_select_model(f, area)
}
ProviderCreationStep::ConfigureSettings => {
self.render_configure_settings(f, area)
}
ProviderCreationStep::ConfirmCreate => {
self.render_confirm_create(f, area)
}
ProviderCreationStep::CreatingProvider => {
self.render_creating_provider(f, area)
}
}
self.render_creator(f, area);
}

async fn create_item(
&mut self,
) -> Result<CreatorAction<ConfigItem>, ApplicationError> {
self.current_step = ProviderCreationStep::CreatingProvider;
self.set_current_step(ProviderCreationStep::CreatingProvider);
match self.create_provider().await {
Ok(new_config) => Ok(CreatorAction::Finish(new_config)),
Err(e) => {
log::error!("Failed to create provider: {}", e);
self.current_step = ProviderCreationStep::ConfirmCreate;
self.set_current_step(ProviderCreationStep::ConfirmCreate);
Ok(CreatorAction::Continue)
}
}
Expand Down Expand Up @@ -518,7 +483,9 @@ impl ConfigItemManager {
current_tab: ConfigTab,
) -> Result<WindowMode, ApplicationError> {
if let Some(creator) = &mut self.creator {
eprintln!("Handling creation input");
let action = creator.handle_input(key_event).await?;
eprintln!("Action: {:?}", action);
match action {
CreatorAction::Continue => {
Ok(WindowMode::Modal(ModalEvent::UpdateUI))
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -22,14 +22,14 @@ pub enum SubPartCreationState {

pub struct ProfileCreator {
new_profile_name: String,
pub creation_step: ProfileCreationStep,
creation_step: ProfileCreationStep,
db_handler: UserProfileDbHandler,
pub background_task: Option<mpsc::Receiver<BackgroundTaskResult>>,
background_task: Option<mpsc::Receiver<BackgroundTaskResult>>,
task_start_time: Option<Instant>,
selected_provider: Option<ConfigItem>,
provider_configs: Vec<ConfigItem>,
selected_provider_index: usize,
pub sub_part_creation_state: SubPartCreationState,
sub_part_creation_state: SubPartCreationState,
text_area: Option<TextArea<ReadDocument>>,
}

Expand Down Expand Up @@ -57,7 +57,29 @@ impl ProfileCreator {
})
}

pub async fn handle_select_provider(
pub fn render_creator(&mut self, f: &mut Frame, area: Rect) {
match self.sub_part_creation_state {
SubPartCreationState::NotCreating => match self.creation_step {
ProfileCreationStep::EnterName => {
self.render_enter_name(f, area)
}
ProfileCreationStep::SelectProvider => {
self.render_select_provider(f, area)
}
ProfileCreationStep::ConfirmCreate => {
self.render_confirm_create(f, area)
}
ProfileCreationStep::CreatingProfile => {
self.render_creating_profile(f, area)
}
},
SubPartCreationState::CreatingProvider(ref mut creator) => {
creator.render(f, area);
}
}
}

async fn handle_select_provider(
&mut self,
input: KeyEvent,
) -> Result<CreatorAction<UserProfile>, ApplicationError> {
Expand Down Expand Up @@ -135,7 +157,7 @@ impl ProfileCreator {
Ok(CreatorAction::CreateItem)
}

pub fn render_select_provider(&self, f: &mut Frame, area: Rect) {
fn render_select_provider(&self, f: &mut Frame, area: Rect) {
let mut items: Vec<ListItem> = self
.provider_configs
.iter()
Expand Down Expand Up @@ -237,10 +259,12 @@ impl ProfileCreator {
format!(" • {}: ", key),
Some(Style::default().fg(Color::Yellow)),
);
setting_line.add_segment(
value.as_str().unwrap_or("").to_string(),
Some(Style::default().fg(Color::Cyan)),
);
if let Some(content) = value.get("__content") {
setting_line.add_segment(
content.as_str().unwrap_or("").to_string(),
Some(Style::default().fg(Color::Cyan)),
);
}
lines.push(setting_line);
}
}
Expand Down Expand Up @@ -364,7 +388,7 @@ impl ProfileCreator {
self.text_area = Some(TextArea::with_read_document(Some(text_lines)));
}

pub fn handle_confirm_create(
fn handle_confirm_create(
&mut self,
input: KeyEvent,
) -> Result<CreatorAction<UserProfile>, ApplicationError> {
Expand Down Expand Up @@ -419,7 +443,7 @@ impl ProfileCreator {
result
}

pub fn render_enter_name(&self, f: &mut Frame, area: Rect) {
fn render_enter_name(&self, f: &mut Frame, area: Rect) {
let input = Paragraph::new(self.new_profile_name.as_str())
.style(Style::default().fg(Color::Yellow))
.block(
Expand All @@ -430,7 +454,7 @@ impl ProfileCreator {
f.render_widget(input, area);
}

pub fn render_confirm_create(&mut self, f: &mut Frame, area: Rect) {
fn render_confirm_create(&mut self, f: &mut Frame, area: Rect) {
let chunks = Layout::default()
.direction(Direction::Vertical)
.constraints([Constraint::Min(1), Constraint::Length(3)])
Expand Down Expand Up @@ -483,7 +507,7 @@ impl ProfileCreator {
f.render_widget(create_button, button_chunks[1]);
}

pub fn go_to_previous_step(
fn go_to_previous_step(
&mut self,
) -> Result<CreatorAction<UserProfile>, ApplicationError> {
match self.creation_step {
Expand All @@ -503,7 +527,7 @@ impl ProfileCreator {
}
}

pub fn render_creating_profile(&self, f: &mut Frame, area: Rect) {
fn render_creating_profile(&self, f: &mut Frame, area: Rect) {
let elapsed = self
.task_start_time
.map(|start| start.elapsed().as_secs())
Expand Down
Loading

0 comments on commit bef30c9

Please sign in to comment.