Skip to content

Commit

Permalink
add some custom theme function
Browse files Browse the repository at this point in the history
  • Loading branch information
betta-cyber committed Mar 16, 2022
1 parent 8068570 commit 825f274
Show file tree
Hide file tree
Showing 5 changed files with 79 additions and 20 deletions.
4 changes: 2 additions & 2 deletions Cargo.toml
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
[package]
name = "netease_music_tui"
version = "0.1.3"
version = "0.1.5"
authors = ["betta <betta0801@gmail.com>"]
license = "MIT"
keywords = ["netease", "player", "music", "tui"]
Expand All @@ -20,7 +20,7 @@ serde = "1.0.101"
serde_derive = "1.0.104"
serde_json = "1.0"
unicode-width = "0.1.5"
config = "0.9.3"
config = "0.12.0"
openssl = "0.10.25"
base64 = "0.11.0"
hex = "0.4.0"
Expand Down
8 changes: 8 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -88,6 +88,14 @@ This table shows some key binds
| Jump to next page | \<Ctrl+f> | Search result \| top list |
| Jump to previous page | \<Ctrl+b> | Search result \| top list |

## custom style

```
hover = "#565656"
active = "#abe047"
other = "#eb4129"
```

## Dev plan
- [x] Djradio and djprogram
- [ ] User page
Expand Down
18 changes: 7 additions & 11 deletions src/main.rs
Original file line number Diff line number Diff line change
Expand Up @@ -36,9 +36,10 @@ use app::{ActiveBlock, App};

use dbus_mpris::{dbus_mpris_handler, DbusMpris};

const FILE_NAME: &str = "Settings.toml";
const CONFIG_DIR: &str = ".config";
const APP_CONFIG_DIR: &str = "netease-music-tui";
pub const FILE_NAME: &str = "Settings.toml";
pub const CONFIG_DIR: &str = ".config";
pub const APP_CONFIG_DIR: &str = "netease-music-tui";


fn main() -> Result<(), failure::Error> {
let config_file_path = match dirs::home_dir() {
Expand All @@ -61,17 +62,12 @@ fn main() -> Result<(), failure::Error> {
};

// init application
let mut settings = config::Config::default();
let config_string = match fs::read_to_string(&config_file_path) {
let settings = match config::Config::builder()
.add_source(config::File::with_name(&config_file_path.to_string_lossy()))
.build() {
Ok(data) => data,
Err(_) => return Err(err_msg("Please set your account in config file")),
};
settings
.merge(config::File::from_str(
&config_string,
config::FileFormat::Toml,
))
.unwrap();

match settings.get_bool("debug") {
Ok(debug) => {
Expand Down
8 changes: 4 additions & 4 deletions src/ui/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,7 @@ use tui::widgets::{
use tui::Frame;
use util::{
create_artist_string, create_datetime_string, create_tag_string, display_track_progress,
get_color, get_percentage_width,
get_color, get_percentage_width, get_text_color
};

// table item for render
Expand Down Expand Up @@ -87,12 +87,12 @@ where
let block = Block::default()
.title("Help")
.borders(Borders::ALL)
.border_style(Style::default().fg(Color::Gray))
.title_style(Style::default().fg(Color::Gray));
.border_style(get_color(highlight_state))
.title_style(get_color(highlight_state));

Paragraph::new([Text::raw("Type ?")].iter())
.block(block)
.style(Style::default().fg(Color::Gray))
.style(get_text_color())
.render(f, chunks[1]);
}

Expand Down
61 changes: 58 additions & 3 deletions src/ui/util.rs
Original file line number Diff line number Diff line change
Expand Up @@ -4,12 +4,67 @@ use chrono::prelude::DateTime;
use chrono::Utc;
use std::time::{Duration, UNIX_EPOCH};
use tui::style::{Color, Style};
use regex::Regex;
use config::Config;

const FILE_NAME: &str = "Settings.toml";
const CONFIG_DIR: &str = ".config";
const APP_CONFIG_DIR: &str = "netease-music-tui";

lazy_static! {
/**
* Regex matches:
* - group 1: Red
* - group 2: Green
* - group 3: Blue
*/
static ref COLOR_HEX_REGEX: Regex = Regex::new(r"#(:?[0-9a-fA-F]{2})(:?[0-9a-fA-F]{2})(:?[0-9a-fA-F]{2})").unwrap();

static ref SETTINGS: Config = Config::builder()
.add_source(config::File::with_name(
&format!("{}/{}/{}/{}", &dirs::home_dir().unwrap().to_string_lossy(), CONFIG_DIR, APP_CONFIG_DIR, FILE_NAME)))
.build()
.unwrap();
}

pub fn parse_hex_color(color: &str) -> Option<Color> {
COLOR_HEX_REGEX.captures(color).map(|groups| {
Color::Rgb(
u8::from_str_radix(groups.get(1).unwrap().as_str(), 16)
.ok()
.unwrap(),
u8::from_str_radix(groups.get(2).unwrap().as_str(), 16)
.ok()
.unwrap(),
u8::from_str_radix(groups.get(3).unwrap().as_str(), 16)
.ok()
.unwrap(),
)
})
}


pub fn get_color((is_active, is_hovered): (bool, bool)) -> Style {
match (is_active, is_hovered) {
(true, _) => Style::default().fg(Color::LightCyan),
(false, true) => Style::default().fg(Color::Magenta),
_ => Style::default().fg(Color::Gray),
(true, _) => match SETTINGS.get_string("active") {
Ok(s) => Style::default().fg(parse_hex_color(&s).unwrap()),
Err(_) => Style::default().fg(Color::LightCyan),
}
(false, true) => match SETTINGS.get_string("hover") {
Ok(s) => Style::default().fg(parse_hex_color(&s).unwrap()),
Err(_) => Style::default().fg(Color::Magenta),
}
_ => match SETTINGS.get_string("other") {
Ok(s) => Style::default().fg(parse_hex_color(&s).unwrap()),
Err(_) => Style::default().fg(Color::Gray),
}
}
}

pub fn get_text_color() -> Style {
match SETTINGS.get_string("text") {
Ok(s) => Style::default().fg(parse_hex_color(&s).unwrap()),
Err(_) => Style::default().fg(Color::Gray),
}
}

Expand Down

0 comments on commit 825f274

Please sign in to comment.