pub fn print_state(message: Message, json: bool) {
    let state = match message {
        Message::StatusResponse(state) => state,
        _ => return,
    };
    if json {
        println!("{}", serde_json::to_string(&state).unwrap());
        return;
    }

    if state.tasks.len() == 0 {
        println!("Task list is empty. Add tasks with `pueue add -- [cmd]`");

        return;
    }
    let mut daemon_status = if state.running {
        style("Daemon status: running")
    } else {
        style("Daemon status: ")
    };

    if state.running {
        daemon_status = daemon_status.with(Color::Green);
    } else {
        daemon_status = daemon_status.with(Color::Yellow);
    }
    println!("{}", daemon_status);

    let mut table = Table::new();
    table
        .set_content_arrangement(ContentArrangement::Dynamic)
        .load_preset(UTF8_HORIZONTAL_BORDERS_ONLY)
        .set_header(vec![
            Cell::new("Index"),
            Cell::new("Status"),
            Cell::new("Exitcode"),
            Cell::new("Command"),
            Cell::new("Path"),
            Cell::new("Start"),
            Cell::new("End"),
        ]);

    for (id, task) in state.tasks {
        let mut row = Row::new();
        // Add a row per time
        row.add_cell(Cell::new(&id.to_string()));

        // Add status cell and color depending on state
        let status_cell = Cell::new(&task.status.to_string());
        let status_cell = match task.status {
            TaskStatus::Running | TaskStatus::Done => status_cell.fg(Color::Green),
            TaskStatus::Failed | TaskStatus::Killed => status_cell.fg(Color::Red),
            TaskStatus::Paused => status_cell.fg(Color::White),
            _ => status_cell.fg(Color::Yellow),
        };
        row.add_cell(status_cell);

        // Match the color of the exit code
        // If the exit_code is none, it has been killed by the task handler.
        match task.exit_code {
            Some(code) => {
                // Everything that's not 0, is failed task
                if code == 0 {
                    row.add_cell(Cell::new(&code.to_string()).fg(Color::Green));
                } else {
                    row.add_cell(Cell::new(&code.to_string()).fg(Color::Red));
                }
            }
            None => {
                row.add_cell(Cell::new(""));
            }
        }

        // Add command and path
        row.add_cell(Cell::new(&task.command));
        row.add_cell(Cell::new(&task.path));

        // Add start time, if already set
        if let Some(start) = task.start {
            let formatted = start.format("%H:%M").to_string();
            row.add_cell(Cell::new(&formatted));
        } else {
            row.add_cell(Cell::new(""));
        }

        // Add finish time, if already set
        if let Some(end) = task.end {
            let formatted = end.format("%H:%M").to_string();
            row.add_cell(Cell::new(&formatted));
        } else {
            row.add_cell(Cell::new(""));
        }

        table.add_row(row);
    }

    // Print the table
    println!("{}", table);
}
/// Print the current state of the daemon in a nicely formatted table
Print the current state of the daemon in a nicely formatted table
Print the current state of the daemon in a nicely formatted table
pub fn print_state(message: Message, json: bool) {
    let state = match message {
        Message::StatusResponse(state) => state,
        _ => return,
    };
    if json {
        println!("{}", serde_json::to_string(&state).unwrap());
        return;
    }
    if state.tasks.len() == 0 {
        println!("Task list is empty. Add tasks with `pueue add -- [cmd]`");
        return;
    }
    let mut daemon_status = if state.running {
        style("Daemon status: running")
    } else {
        style("Daemon status: ")
    };
    if state.running {
        daemon_status = daemon_status.with(Color::Green);
    } else {
        daemon_status = daemon_status.with(Color::Yellow);
    }
    println!("{}", daemon_status);
    let mut table = Table::new();
    table
        .set_content_arrangement(ContentArrangement::Dynamic)
        .load_preset(UTF8_HORIZONTAL_BORDERS_ONLY)
        .set_header(vec![
            Cell::new("Index"),
            Cell::new("Status"),
            Cell::new("Exitcode"),
            Cell::new("Command"),
            Cell::new("Path"),
            Cell::new("Start"),
            Cell::new("End"),
        ]);
    for (id, task) in state.tasks {
        let mut row = Row::new();
        // Add a row per time
        row.add_cell(Cell::new(&id.to_string()));
        // Add status cell and color depending on state
        let status_cell = Cell::new(&task.status.to_string());
        let status_cell = match task.status {
            TaskStatus::Running | TaskStatus::Done => status_cell.fg(Color::Green),
            TaskStatus::Failed | TaskStatus::Killed => status_cell.fg(Color::Red),
            TaskStatus::Paused => status_cell.fg(Color::White),
            _ => status_cell.fg(Color::Yellow),
        };
        row.add_cell(status_cell);
        // Match the color of the exit code
        // If the exit_code is none, it has been killed by the task handler.
        match task.exit_code {
            Some(code) => {
                // Everything that's not 0, is failed task
                if code == 0 {
                    row.add_cell(Cell::new(&code.to_string()).fg(Color::Green));
                } else {
                    row.add_cell(Cell::new(&code.to_string()).fg(Color::Red));
                }
            }
            None => {
                row.add_cell(Cell::new(""));
            }
        }
        // Add command and path
        row.add_cell(Cell::new(&task.command));
        row.add_cell(Cell::new(&task.path));
        // Add start time, if already set
        if let Some(start) = task.start {
            let formatted = start.format("%H:%M").to_string();
            row.add_cell(Cell::new(&formatted));
        } else {
            row.add_cell(Cell::new(""));
        }
        // Add finish time, if already set
        if let Some(end) = task.end {
            let formatted = end.format("%H:%M").to_string();
            row.add_cell(Cell::new(&formatted));
        } else {
            row.add_cell(Cell::new(""));
        }
        table.add_row(row);
    }
    // Print the table
    println!("{}", table);
}
pub fn print_state ( message : Message , json : bool ) { let state = match message { Message :: StatusResponse ( state ) => state , _ => return , } ; if json { println ! ( " " serde_json to_string ( state ) unwrap ( ) ) ; return ; } if state . tasks . len ( ) == 0 { println ! ( " " ) ; return ; } let mut daemon_status = if state . running { style ( " " ) } else { style ( " " ) } ; if state . running { daemon_status = daemon_status . with ( Color :: Green ) ; } else { daemon_status = daemon_status . with ( Color :: Yellow ) ; } println ! ( " " daemon_status ) ; let mut table = Table :: new ( ) ; table . set_content_arrangement ( ContentArrangement :: Dynamic ) . load_preset ( UTF8_HORIZONTAL_BORDERS_ONLY ) . set_header ( vec ! [ Cell new ( " " ) Cell new ( " " ) Cell new ( " " ) Cell new ( " " ) Cell new ( " " ) Cell new ( " " ) Cell new ( " " ) ] ) ; for ( id , task ) in state . tasks { let mut row = Row :: new ( ) ; // Add a row per time row . add_cell ( Cell :: new ( & id . to_string ( ) ) ) ; // Add status cell and color depending on state let status_cell = Cell :: new ( & task . status . to_string ( ) ) ; let status_cell = match task . status { TaskStatus :: Running | TaskStatus :: Done => status_cell . fg ( Color :: Green ) , TaskStatus :: Failed | TaskStatus :: Killed => status_cell . fg ( Color :: Red ) , TaskStatus :: Paused => status_cell . fg ( Color :: White ) , _ => status_cell . fg ( Color :: Yellow ) , } ; row . add_cell ( status_cell ) ; // Match the color of the exit code // If the exit_code is none, it has been killed by the task handler. match task . exit_code { Some ( code ) => { // Everything that's not 0, is failed task if code == 0 { row . add_cell ( Cell :: new ( & code . to_string ( ) ) . fg ( Color :: Green ) ) ; } else { row . add_cell ( Cell :: new ( & code . to_string ( ) ) . fg ( Color :: Red ) ) ; } } None => { row . add_cell ( Cell :: new ( " " ) ) ; } } // Add command and path row . add_cell ( Cell :: new ( & task . command ) ) ; row . add_cell ( Cell :: new ( & task . path ) ) ; // Add start time, if already set if let Some ( start ) = task . start { let formatted = start . format ( " " ) . to_string ( ) ; row . add_cell ( Cell :: new ( & formatted ) ) ; } else { row . add_cell ( Cell :: new ( " " ) ) ; } // Add finish time, if already set if let Some ( end ) = task . end { let formatted = end . format ( " " ) . to_string ( ) ; row . add_cell ( Cell :: new ( & formatted ) ) ; } else { row . add_cell ( Cell :: new ( " " ) ) ; } table . add_row ( row ) ; } // Print the table println ! ( " " table ) ; }
Print the current state of the daemon in a nicely formatted table
Print the current state of the daemon in a nicely formatted table
message
Message
json
bool
message
Message
json
bool