Skip to content

Commit

Permalink
fix: fix list state
Browse files Browse the repository at this point in the history
  • Loading branch information
covercash2 committed Nov 20, 2024
1 parent 7dd0e85 commit 08ddb97
Show file tree
Hide file tree
Showing 2 changed files with 40 additions and 8 deletions.
7 changes: 5 additions & 2 deletions ollama/ollama-cli/src/error.rs
Original file line number Diff line number Diff line change
Expand Up @@ -11,8 +11,11 @@ pub enum Error {
#[error(transparent)]
Modelfile(#[from] ModelfileError),

#[error("unable to index instruction in Modelfile")]
ModelfileIndex,
#[error("unable to index instruction in Modelfile: {0}")]
ModelfileIndex(usize),

#[error("missing instruction in Modelfile: {0}")]
ModelfileMissing(String),

#[error(transparent)]
OllamaRs(#[from] OllamaError),
Expand Down
41 changes: 35 additions & 6 deletions ollama/ollama-cli/src/tui/models/modelfile.rs
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,7 @@ use super::ModelEvent;
pub struct ModelfileViewModel {
modelfile: Option<Modelfile>,
model_info: HashMap<String, toml::Value>,
selected: Option<usize>,
instructions: Vec<String>,
details: Option<toml::Value>,
list_state: ListState,
Expand Down Expand Up @@ -50,12 +51,12 @@ impl ModelfileViewModel {
match action {
Action::Quit => Ok(Some(ModelEvent::Deactivate)),
Action::Up => {
self.list_state.select_previous();
self.prev();
self.update_details()?;
Ok(None)
}
Action::Down => {
self.list_state.select_next();
self.next();
self.update_details()?;
Ok(None)
}
Expand All @@ -69,16 +70,44 @@ impl ModelfileViewModel {
}
}

fn next(&mut self) {
if let Some(selected) = self.selected {
if selected == self.instructions.len() - 1 {
self.selected = Some(0);
} else {
self.selected = Some(selected + 1);
}
} else {
self.selected = Some(0);
}
}

fn prev(&mut self) {
if let Some(selected) = self.selected {
if selected == 0 {
self.selected = Some(self.instructions.len() - 1);
} else {
self.selected = Some(selected - 1);
}
} else {
self.selected = Some(self.instructions.len() - 1);
}
}

fn update_details(&mut self) -> Result<()> {
tracing::info!(offset = self.list_state.selected(), "updating offset");
if let Some(selected) = self.list_state.selected() {
let details = self
if let Some(selected) = self.selected {
let instruction = self
.instructions
.get(selected)
.and_then(|instruction| self.model_info.get(instruction))
.ok_or(Error::ModelfileIndex(selected))?;
let details = self
.model_info
.get(instruction)
.cloned()
.ok_or(Error::ModelfileIndex)?;
.ok_or(Error::ModelfileMissing(instruction.to_string()))?;
self.details = Some(details);
self.list_state.select(Some(selected));
}
Ok(())
}
Expand Down

0 comments on commit 08ddb97

Please sign in to comment.