Compare commits

..

No commits in common. "063ce06aaadb9a62cf38defb0f80798414e97c50" and "e0644cd2ae11cdef5f5e5c5b7bb113df42dab0a2" have entirely different histories.

4 changed files with 29 additions and 81 deletions

View File

@ -8,7 +8,6 @@ use tui::backend::TermionBackend;
use tui::layout::{Constraint, Direction, Layout};
use tui::Terminal;
use crate::widget::info;
use crate::widget::repo_entry;
use crate::widget::service_switcher;
use crate::widget::tag_list;
@ -18,7 +17,6 @@ pub struct Ui {
repo: crate::widget::repo_entry::RepoEntry,
tags: crate::widget::tag_list::TagList,
services: crate::widget::service_switcher::ServiceSwitcher,
info: crate::widget::info::Info,
}
#[derive(PartialEq, Clone)]
@ -28,16 +26,13 @@ pub enum State {
SelectService,
}
impl std::iter::Iterator for State {
type Item = Self;
fn next(&mut self) -> Option<Self::Item> {
impl State {
fn next(&self) -> State {
match self {
State::EditRepo => *self = State::SelectTag,
State::SelectTag => *self = State::SelectService,
State::SelectService => *self = State::EditRepo,
State::EditRepo => State::SelectTag,
State::SelectTag => State::SelectService,
State::SelectService => State::EditRepo,
}
Some(self.clone())
}
}
@ -46,9 +41,8 @@ impl Ui {
let mut ui = Ui {
state: State::SelectService,
repo: repo_entry::RepoEntry::new(repo_id),
tags: tag_list::TagList::with_status("Tags are empty"),
tags: tag_list::TagList::with_status("Select image or edit Repository"),
services: service_switcher::ServiceSwitcher::new(),
info: info::Info::new("Select image of edit Repository"),
};
//setup tui
@ -68,10 +62,9 @@ impl Ui {
.direction(Direction::Vertical)
.constraints(
[
Constraint::Min(9),
Constraint::Min(3),
Constraint::Length(3),
Constraint::Min(7),
Constraint::Length(2),
Constraint::Max(7),
]
.as_ref(),
)
@ -82,36 +75,26 @@ impl Ui {
rect.render_widget(ui.repo.render(&ui.state), chunks[1]);
let (list, state) = ui.tags.render(&ui.state);
rect.render_stateful_widget(list, chunks[2], state);
rect.render_widget(ui.info.render(), chunks[3]);
})
.unwrap();
//handle input
match receiver.try_recv() {
Ok(Key::Ctrl('q')) => break 'core, //quit program without saving
Ok(Key::Char('\t')) => {
ui.state.next();
()
}
Ok(Key::Char('\t')) => ui.state = ui.state.next(),
Ok(Key::Ctrl('s')) => match ui.services.save() {
Err(e) => {
ui.info.set_info(&format!("{}", e));
ui.show_info(&format!("{}", e));
continue;
}
Ok(_) => ui.info.set_info("Saved compose file"),
Ok(_) => (),
},
Ok(Key::Ctrl('r')) => {
ui.repo.confirm();
ui.tags = tag_list::TagList::with_repo(ui.repo.get());
}
Ok(Key::Ctrl('n')) => match ui.tags.next_page() {
Err(e) => ui.info.set_info(&format!("{}", e)),
Ok(_) => (),
},
Ok(Key::Ctrl('p')) => match ui.tags.prev_page() {
Err(e) => ui.info.set_info(&format!("{}", e)),
Ok(_) => (),
},
Ok(Key::Ctrl('n')) => ui.tags.next_page(),
Ok(Key::Ctrl('p')) => ui.tags.prev_page(),
Ok(Key::Char('\n')) => match ui.state {
State::EditRepo => {
ui.repo.confirm();
@ -121,7 +104,7 @@ impl Ui {
let mut repo = ui.repo.get();
let tag = match ui.tags.get_selected() {
Err(e) => {
ui.info.set_info(&format!("{}", e));
ui.show_info(&format!("{}", e));
continue;
}
Ok(tag) => tag,
@ -134,14 +117,14 @@ impl Ui {
},
Ok(Key::Char(key)) => {
if ui.state == State::EditRepo {
ui.info.set_info("Editing Repository");
ui.show_info("Editing Repository");
}
ui.repo.handle_input(&ui.state, Key::Char(key));
ui.tags.handle_input(&ui.state, Key::Char(key));
}
Ok(Key::Backspace) => {
if ui.state == State::EditRepo {
ui.info.set_info("Editing Repository");
ui.show_info("Editing Repository");
}
ui.repo.handle_input(&ui.state, Key::Backspace);
ui.tags.handle_input(&ui.state, Key::Backspace);
@ -149,11 +132,11 @@ impl Ui {
Ok(Key::Up) => {
if ui.state == State::SelectService && ui.services.find_previous_match() {
match ui.services.extract_repo() {
Err(e) => ui.info.set_info(&format!("{}", e)),
Err(e) => ui.show_info(&format!("{}", e)),
Ok(s) => {
let repo = match crate::tags::Tags::check_repo(s) {
Err(e) => {
ui.info.set_info(&format!("{}", e));
ui.show_info(&format!("{}", e));
continue;
}
Ok(s) => s,
@ -169,11 +152,11 @@ impl Ui {
Ok(Key::Down) => match ui.state {
State::SelectService if ui.services.find_next_match() => {
match ui.services.extract_repo() {
Err(e) => ui.info.set_info(&format!("{}", e)),
Err(e) => ui.show_info(&format!("{}", e)),
Ok(s) => {
let repo = match crate::tags::Tags::check_repo(s) {
Err(e) => {
ui.info.set_info(&format!("{}", e));
ui.show_info(&format!("{}", e));
continue;
}
Ok(s) => s,
@ -202,6 +185,11 @@ impl Ui {
terminal.clear().unwrap();
}
/// helper function to show information in TagList
fn show_info(&mut self, error: &str) {
self.tags = tag_list::TagList::with_status(error);
}
/// create a thread for catching input and send them to core loop
pub fn spawn_stdin_channel(&self) -> mpsc::Receiver<termion::event::Key> {
let (tx, rx) = mpsc::channel::<termion::event::Key>();

View File

@ -1,33 +0,0 @@
use tui::style::{Color, Style};
use tui::widgets::{Block, List, ListItem};
pub struct Info {
info: String,
keys: String,
}
impl Info {
pub fn new(info: &str) -> Self {
Self {
info: String::from(info),
keys: String::from(
"Tab Cycle widgets C-s Save C-r Reload C-q Quit C-n Next page C-p Previous page ↑ ↓ Select tags or image line",
),
}
}
pub fn render(&self) -> List {
let items = vec![
ListItem::new(self.info.clone()),
ListItem::new(self.keys.clone()),
];
List::new(items)
.block(Block::default())
.style(Style::default().fg(Color::White).bg(Color::Black))
.highlight_style(Style::default().bg(Color::Black))
}
pub fn set_info(&mut self, info: &str) {
self.info = String::from(info);
}
}

View File

@ -1,4 +1,3 @@
pub mod info;
pub mod repo_entry;
pub mod service_switcher;
pub mod tag_list;

View File

@ -11,8 +11,6 @@ use crate::ui::State;
pub enum Error {
NoneSelected,
NoTags,
NoNextPage,
NoPrevPage,
}
impl fmt::Display for Error {
@ -20,8 +18,6 @@ impl fmt::Display for Error {
match self {
Error::NoTags => write!(f, "There are no tags"),
Error::NoneSelected => write!(f, "No tag selected"),
Error::NoNextPage => write!(f, "No next page available"),
Error::NoPrevPage => write!(f, "No previous page available"),
}
}
}
@ -59,27 +55,25 @@ impl TagList {
}
/// display next page if possible
pub fn next_page(&mut self) -> Result<(), Error> {
pub fn next_page(&mut self) {
match &self.typ {
Type::Status(_) => (),
Type::Repo(tags) => match tags.next_page() {
Err(e) => return Err(Error::NoNextPage),
Err(e) => self.typ = Type::Status(format!("{}", e)),
Ok(tags) => self.typ = Type::Repo(tags),
},
}
Ok(())
}
/// display previous page if possible
pub fn prev_page(&mut self) -> Result<(), Error> {
pub fn prev_page(&mut self) {
match &self.typ {
Type::Status(_) => (),
Type::Repo(tags) => match tags.prev_page() {
Err(e) => return Err(Error::NoPrevPage),
Err(e) => self.typ = Type::Status(format!("{}", e)),
Ok(tags) => self.typ = Type::Repo(tags),
},
}
Ok(())
}
/// get a list of tag names with info