Skip to content

Commit

Permalink
Better workflow, helps address #54 and #62
Browse files Browse the repository at this point in the history
  • Loading branch information
dmadisetti committed Feb 1, 2024
1 parent a573187 commit 19cdcb2
Show file tree
Hide file tree
Showing 4 changed files with 67 additions and 18 deletions.
23 changes: 18 additions & 5 deletions src/client.rs
Original file line number Diff line number Diff line change
Expand Up @@ -65,6 +65,9 @@ fn execute(
loop {
match queue.pop_back() {
None => break,
// For flag reference see:
// https://developer.valvesoftware.com/wiki/Command_line_options#Steam_.28Windows.29
// and https://gist.github.com/davispuh/6600880
Some(Command::StartClient) => {
if !scan_port(STEAM_PORT) {
let (sender, termination) = channel();
Expand Down Expand Up @@ -228,11 +231,17 @@ fn execute(
.map(|x| x.to_string())
.collect::<Vec<String>>();
command.append(&mut args);
log!("Finding entry");
let entry = match steam_run_wrapper() {
Ok(wrapper) => wrapper.into_os_string().into_string().unwrap(),
Err(STError::Problem(_)) => command.remove(0),
Err(err) => return Err(err), // unwrap and rewrap to explicitly note this is an err.
Err(err) => {
let mut reference = status.lock().unwrap();
*reference = Some(GameStatus::msg(&*reference, "Could not find entry program."));
return Err(err);
}, // unwrap and rewrap to explicitly note this is an err.
};
log!("Exits loop");
let status = status.clone();
thread::spawn(move || {
{
Expand Down Expand Up @@ -264,6 +273,8 @@ fn execute(
});
launched = true;
break;
} else {
log!("Tried", launchable.executable);
}
}
if !launched {
Expand All @@ -283,7 +294,8 @@ fn execute(
let response = String::from_utf8_lossy(&buf);
match *INPUT_LEX.tokenize(&line).as_slice() {
["login", _] => {
if response.to_string().contains("Login Failure") {
let response = response.to_string();
if response.contains("Login Failure") || response.contains("FAILED") {
let mut state = state.lock()?;
*state = State::Failed;
} else {
Expand All @@ -305,7 +317,7 @@ fn execute(
if response == "[0m" {
continue;
}

games = Vec::new();
let licenses = response.to_string();
let keys = keys_from_licenses(licenses);
Expand Down Expand Up @@ -362,7 +374,8 @@ fn execute(
cmd.write("")?;
while !response.starts_with("[0mAppID") {
if let Ok(buf) = cmd.maybe_next() {
response = String::from_utf8_lossy(&buf).into_owned().into();
response =
String::from_utf8_lossy(&buf).into_owned().into();
}
}
}
Expand Down Expand Up @@ -602,7 +615,7 @@ fn keys_from_licenses(licenses: String) -> Vec<i32> {

#[cfg(test)]
mod tests {
use crate::client::{Client, Command, State, keys_from_licenses};
use crate::client::{keys_from_licenses, Client, Command, State};
use crate::util::error::STError;
use std::sync::mpsc::channel;
use std::sync::Arc;
Expand Down
41 changes: 30 additions & 11 deletions src/main.rs
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,7 @@ use crossterm::terminal::{
use tui::style::{Color, Style};
use tui::{backend::CrosstermBackend, layout::Rect, Terminal};

use terminal_light;
use terminal_light::background_color;

use tui_image_rgba_updated::{ColorMode, Image};

Expand Down Expand Up @@ -38,7 +38,7 @@ fn entry() -> Result<(), Box<dyn std::error::Error>> {
let backend = CrosstermBackend::new(stdout);
let mut terminal = Terminal::new(backend)?;

let terminal_bg = terminal_light::background_color()
let terminal_bg = background_color()
.map(|c| c.rgb())
.map(|c| Color::Rgb(c.r, c.g, c.b))
.unwrap_or(Color::Gray);
Expand All @@ -59,20 +59,23 @@ fn entry() -> Result<(), Box<dyn std::error::Error>> {
let events = Events::new();
let client = Client::new();

if !app.user.is_empty() {
client.login(&app.user)?;
}

// Attempt to load from cache. If not, continue as usual.
let mut game_list: StatefulList<Game> = StatefulList::new();
let mut cached: bool = false;
match client.games() {
Ok(games) => {
game_list = StatefulList::with_items(games);
app.mode = Mode::Normal;
app.mode = Mode::Loading;
cached = true;
}
_ => game_list.restart(),
}

// Login after cache load, since a failed login needs to show the failure screen.
if !app.user.is_empty() {
client.login(&app.user)?;
}

loop {
terminal.draw(|frame| {
let layout = App::build_layout();
Expand Down Expand Up @@ -160,6 +163,9 @@ fn entry() -> Result<(), Box<dyn std::error::Error>> {
break;
}
KeyCode::Char('r') => {
// Marked cache as false for the potential race condition
// (you flush cache prior to login)
cached = false;
app.mode = Mode::Loading;
client.restart()?;
}
Expand All @@ -176,7 +182,7 @@ fn entry() -> Result<(), Box<dyn std::error::Error>> {
terminal.show_cursor()?;
game_list.unselect();
}
KeyCode::Char('\n') => {
KeyCode::Char('\n') | KeyCode::Enter => {
if let Some(game) = game_list.selected() {
client.run(game)?;
}
Expand Down Expand Up @@ -232,7 +238,7 @@ fn entry() -> Result<(), Box<dyn std::error::Error>> {
break;
}
}
KeyCode::Char('\n') => {
KeyCode::Char('\n') | KeyCode::Enter => {
let mut user = app.user.clone();
user.retain(|c| !c.is_whitespace());
terminal.hide_cursor()?;
Expand All @@ -257,7 +263,7 @@ fn entry() -> Result<(), Box<dyn std::error::Error>> {
game_list.query = "".to_string();
img = update_img(&game_list.selected());
}
KeyCode::Char('\n') => {
KeyCode::Char('\n') | KeyCode::Enter => {
terminal.hide_cursor()?;
app.mode = Mode::Searched;
}
Expand All @@ -280,12 +286,25 @@ fn entry() -> Result<(), Box<dyn std::error::Error>> {
},
_ => {}
}
// Need a hook to cancel if in loading mode.
if app.mode == Mode::Loading {
if let KeyCode::Char('q') = input {
break;
}
}
events.release();
}
if app.mode == Mode::Loading {
match client.get_state()? {
State::Loaded(_, -2) => {
client.load_games()?;
// If loaded from cache then just used the cache
if cached {
// Importantly, mark cached as false to allow reloads
cached = false;
app.mode = Mode::Normal;
} else {
client.load_games()?;
}
}
State::LoggedIn => {
config.save()?;
Expand Down
17 changes: 15 additions & 2 deletions src/util/event.rs
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,7 @@ use std::time::Duration;

use crate::util::log::log;

use crossterm::event::{read, Event as CrossEvent, KeyCode, KeyEvent};
use crossterm::event::{read, Event as CrossEvent, KeyCode, KeyEvent, KeyModifiers};

pub enum Event<I> {
Input(I),
Expand Down Expand Up @@ -53,8 +53,21 @@ impl Events {
thread::spawn(move || {
//matching the key
loop {
if let CrossEvent::Key(KeyEvent { code: kc, .. }) = read().unwrap() {
if let CrossEvent::Key(KeyEvent {
code: kc,
modifiers,
..
}) = read().unwrap()
{
if debounce.load(Ordering::Relaxed) {
// Let CTRL-c just be q
let kc = if kc == KeyCode::Char('c')
&& modifiers.contains(KeyModifiers::CONTROL)
{
KeyCode::Char('q')
} else {
kc
};
if let Err(err) = tx.send(Event::Input(kc)) {
log!(err);
return;
Expand Down
4 changes: 4 additions & 0 deletions src/util/stateful.rs
Original file line number Diff line number Diff line change
Expand Up @@ -31,6 +31,10 @@ impl<T: Named> StatefulList<T> {
}
}

pub fn is_empty(&self) -> bool {
self.items.is_empty()
}

pub fn selected(&self) -> Option<&T> {
self.state.selected().map(|i| {
*self
Expand Down

0 comments on commit 19cdcb2

Please sign in to comment.