put tui tools in separate file

This commit is contained in:
Stephan Dilly 2020-03-18 15:05:07 +01:00
parent 783f2ddb1b
commit ca672b3072
4 changed files with 34 additions and 33 deletions

View file

@ -1,9 +1,9 @@
use crate::{clear::Clear, git_utils};
use crate::{clear::Clear, git_utils, tui_utils};
use crossterm::event::{Event, KeyCode};
use std::borrow::Cow;
use tui::backend::Backend;
use tui::layout::{Alignment, Rect};
use tui::{
backend::Backend,
layout::{Alignment, Rect},
style::{Color, Style},
widgets::{Block, Borders, Paragraph, Text, Widget},
Frame,
@ -55,7 +55,7 @@ impl UIElement for UICommit {
.block(Block::default().title("Commit").borders(Borders::ALL))
.alignment(Alignment::Left),
)
.render(f, git_utils::centered_rect(60, 20, f.size()));
.render(f, tui_utils::centered_rect(60, 20, f.size()));
}
}

View file

@ -1,6 +1,5 @@
use git2::{DiffFormat, DiffOptions, Repository, StatusOptions, StatusShow};
use std::path::Path;
use tui::layout::{Constraint, Direction, Layout, Rect};
///
#[derive(Copy, Clone, PartialEq)]
@ -105,34 +104,6 @@ pub fn commit(msg: &String) {
.unwrap();
}
/// use layouts to create a rects that
/// centers inside `r` and sizes `percent_x`/`percent_x` of `r`
pub fn centered_rect(percent_x: u16, percent_y: u16, r: Rect) -> Rect {
let popup_layout = Layout::default()
.direction(Direction::Vertical)
.constraints(
[
Constraint::Percentage((100 - percent_y) / 2),
Constraint::Percentage(percent_y),
Constraint::Percentage((100 - percent_y) / 2),
]
.as_ref(),
)
.split(r);
Layout::default()
.direction(Direction::Horizontal)
.constraints(
[
Constraint::Percentage((100 - percent_x) / 2),
Constraint::Percentage(percent_x),
Constraint::Percentage((100 - percent_x) / 2),
]
.as_ref(),
)
.split(popup_layout[1])[1]
}
///
pub fn index_empty() -> bool {
let repo = repo();

View file

@ -4,6 +4,7 @@ mod commit;
mod git_status;
mod git_utils;
mod poll;
mod tui_utils;
use app::App;
use crossterm::{

29
src/tui_utils.rs Normal file
View file

@ -0,0 +1,29 @@
use tui::layout::{Constraint, Direction, Layout, Rect};
/// use layouts to create a rects that
/// centers inside `r` and sizes `percent_x`/`percent_x` of `r`
pub fn centered_rect(percent_x: u16, percent_y: u16, r: Rect) -> Rect {
let popup_layout = Layout::default()
.direction(Direction::Vertical)
.constraints(
[
Constraint::Percentage((100 - percent_y) / 2),
Constraint::Percentage(percent_y),
Constraint::Percentage((100 - percent_y) / 2),
]
.as_ref(),
)
.split(r);
Layout::default()
.direction(Direction::Horizontal)
.constraints(
[
Constraint::Percentage((100 - percent_x) / 2),
Constraint::Percentage(percent_x),
Constraint::Percentage((100 - percent_x) / 2),
]
.as_ref(),
)
.split(popup_layout[1])[1]
}