refactor concat_headers

concat_headers method moves to App
pull/143/head
kyoto7250 2 years ago
parent 75bcf73cd6
commit 35a2ae7d18

@ -183,12 +183,11 @@ impl App {
Some(self.record_table.filter.input_str())
},
orders,
header_icons,
)
.await?;
self.record_table.update(
records,
headers,
self.concat_headers(headers, header_icons),
database.clone(),
table.clone(),
hold_cursor_position,
@ -242,7 +241,7 @@ impl App {
.pool
.as_ref()
.unwrap()
.get_records(&database, &table, 0, None, None, None)
.get_records(&database, &table, 0, None, None)
.await?;
self.record_table.update(
records,
@ -315,7 +314,6 @@ impl App {
Some(self.record_table.filter.input_str())
},
None,
None,
)
.await?;
if !records.is_empty() {
@ -406,6 +404,24 @@ impl App {
}
Ok(EventState::NotConsumed)
}
fn concat_headers(
&self,
headers: Vec<String>,
header_icons: Option<Vec<String>>,
) -> Vec<String> {
if let Some(header_icons) = &header_icons {
let mut new_headers = vec![String::new(); headers.len()];
for (index, header) in headers.iter().enumerate() {
new_headers[index] = format!("{} {}", header, header_icons[index])
.trim()
.to_string();
}
return new_headers;
}
headers
}
}
#[cfg(test)]
@ -441,4 +457,25 @@ mod test {
);
assert_eq!(app.left_main_chunk_percentage, 15);
}
#[test]
fn test_concat_headers() {
let app = App::new(Config::default());
let headers = vec![
"ID".to_string(),
"NAME".to_string(),
"TIMESTAMP".to_string(),
];
let header_icons = vec!["".to_string(), "↑1".to_string(), "↓2".to_string()];
let concat_headers: Vec<String> = app.concat_headers(headers, Some(header_icons));
assert_eq!(
concat_headers,
vec![
"ID".to_string(),
"NAME ↑1".to_string(),
"TIMESTAMP ↓2".to_string()
]
)
}
}

@ -23,7 +23,6 @@ pub trait Pool: Send + Sync {
page: u16,
filter: Option<String>,
orders: Option<String>,
header_icons: Option<Vec<String>>,
) -> anyhow::Result<(Vec<String>, Vec<Vec<String>>)>;
async fn get_columns(
&self,
@ -48,20 +47,6 @@ pub trait Pool: Send + Sync {
async fn close(&self);
}
fn concat_headers(headers: Vec<String>, header_icons: Option<Vec<String>>) -> Vec<String> {
if let Some(header_icons) = &header_icons {
let mut new_headers = vec![String::new(); headers.len()];
for (index, header) in headers.iter().enumerate() {
new_headers[index] = format!("{} {}", header, header_icons[index])
.trim()
.to_string();
}
return new_headers;
}
headers
}
pub enum ExecuteResult {
Read {
headers: Vec<String>,
@ -85,27 +70,3 @@ macro_rules! get_or_null {
$value.map_or("NULL".to_string(), |v| v.to_string())
};
}
#[cfg(test)]
mod test {
use super::concat_headers;
#[test]
fn test_concat_headers() {
let headers = vec![
"ID".to_string(),
"NAME".to_string(),
"TIMESTAMP".to_string(),
];
let header_icons = vec!["".to_string(), "↑1".to_string(), "↓2".to_string()];
let concat_headers: Vec<String> = concat_headers(headers, Some(header_icons));
assert_eq!(
concat_headers,
vec![
"ID".to_string(),
"NAME ↑1".to_string(),
"TIMESTAMP ↓2".to_string()
]
)
}
}

@ -1,6 +1,6 @@
use crate::get_or_null;
use super::{concat_headers, ExecuteResult, Pool, TableRow, RECORDS_LIMIT_PER_PAGE};
use super::{ExecuteResult, Pool, TableRow, RECORDS_LIMIT_PER_PAGE};
use async_trait::async_trait;
use chrono::{NaiveDate, NaiveDateTime, NaiveTime};
use database_tree::{Child, Database, Table};
@ -229,7 +229,6 @@ impl Pool for MySqlPool {
page: u16,
filter: Option<String>,
orders: Option<String>,
header_icons: Option<Vec<String>>,
) -> anyhow::Result<(Vec<String>, Vec<Vec<String>>)> {
let query = if let (Some(filter), Some(orders)) = (&filter, &orders) {
format!(
@ -283,7 +282,7 @@ impl Pool for MySqlPool {
}
records.push(new_row)
}
Ok((concat_headers(headers, header_icons), records))
Ok((headers, records))
}
async fn get_columns(

@ -1,6 +1,6 @@
use crate::get_or_null;
use super::{concat_headers, ExecuteResult, Pool, TableRow, RECORDS_LIMIT_PER_PAGE};
use super::{ExecuteResult, Pool, TableRow, RECORDS_LIMIT_PER_PAGE};
use async_trait::async_trait;
use chrono::{NaiveDate, NaiveDateTime, NaiveTime};
use database_tree::{Child, Database, Schema, Table};
@ -246,7 +246,6 @@ impl Pool for PostgresPool {
page: u16,
filter: Option<String>,
orders: Option<String>,
header_icons: Option<Vec<String>>,
) -> anyhow::Result<(Vec<String>, Vec<Vec<String>>)> {
let query = if let (Some(filter), Some(orders)) = (&filter, &orders) {
format!(
@ -344,7 +343,7 @@ impl Pool for PostgresPool {
}
records.push(new_row)
}
Ok((concat_headers(headers, header_icons), records))
Ok((headers, records))
}
async fn get_columns(

@ -1,6 +1,6 @@
use crate::get_or_null;
use super::{concat_headers, ExecuteResult, Pool, TableRow, RECORDS_LIMIT_PER_PAGE};
use super::{ExecuteResult, Pool, TableRow, RECORDS_LIMIT_PER_PAGE};
use async_trait::async_trait;
use chrono::NaiveDateTime;
use database_tree::{Child, Database, Table};
@ -231,7 +231,6 @@ impl Pool for SqlitePool {
page: u16,
filter: Option<String>,
orders: Option<String>,
header_icons: Option<Vec<String>>,
) -> anyhow::Result<(Vec<String>, Vec<Vec<String>>)> {
let query = if let (Some(filter), Some(orders)) = (&filter, &orders) {
format!(
@ -281,7 +280,7 @@ impl Pool for SqlitePool {
}
records.push(new_row)
}
Ok((concat_headers(headers, header_icons), records))
Ok((headers, records))
}
async fn get_columns(

Loading…
Cancel
Save