Skip to content

Commit

Permalink
COLUMNS
Browse files Browse the repository at this point in the history
  • Loading branch information
ilyagr committed Jul 5, 2024
1 parent 4cb8e89 commit 3f8bc17
Show file tree
Hide file tree
Showing 6 changed files with 52 additions and 8 deletions.
10 changes: 10 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -193,6 +193,16 @@ the following to your `.bashrc`:
export HWATCH="--no-title --color --no-help-banner --border --with-scrollbar"
```

## Execution environment

When running a command, `hwatch` sets the `COLUMNS`. Many commands recognize it
automatically (try `git diff --stat`), some others accept column width as a
command-line argument.

TODO: Link to standard.

Some versions of `bash` might reset the value of `COLUMNS`; for this reason the `HWATCH_COLUMNS` environment variable is set to the same value.

## Example

### interval 10 second
Expand Down
20 changes: 18 additions & 2 deletions src/app.rs
Original file line number Diff line number Diff line change
Expand Up @@ -42,9 +42,9 @@ use crate::{
};

// local const
use crate::Interval;
use crate::DEFAULT_TAB_SIZE;
use crate::HISTORY_WIDTH;
use crate::{Interval, OutputWidthShared};

///
#[derive(Clone, Copy, PartialEq, Eq)]
Expand Down Expand Up @@ -162,6 +162,9 @@ pub struct App<'a> {
///
interval: Interval,

///
output_width: OutputWidthShared,

///
tab_size: u16,

Expand Down Expand Up @@ -203,7 +206,12 @@ pub struct App<'a> {
/// Trail at watch view window.
impl<'a> App<'a> {
///
pub fn new(tx: Sender<AppEvent>, rx: Receiver<AppEvent>, interval: Interval) -> Self {
pub fn new(
tx: Sender<AppEvent>,
rx: Receiver<AppEvent>,
interval: Interval,
output_width: OutputWidthShared,
) -> Self {
// method at create new view trail.
Self {
keymap: default_keymap(),
Expand Down Expand Up @@ -239,6 +247,7 @@ impl<'a> App<'a> {
results_stderr: HashMap::new(),

interval: interval.clone(),
output_width,
tab_size: DEFAULT_TAB_SIZE,

header_area: HeaderArea::new(*interval.read().unwrap()),
Expand Down Expand Up @@ -328,8 +337,15 @@ impl<'a> App<'a> {

///
pub fn draw(&mut self, f: &mut Frame) {
// !!
self.define_subareas(f.size());

{
let mut cur_output_width = self.output_width.write().unwrap();
// TODO: Account for the border
*cur_output_width = Some(self.watch_area.get_pane_width() as usize);
}

if self.show_header {
// Draw header area.
self.header_area.draw(f);
Expand Down
14 changes: 11 additions & 3 deletions src/exec.rs
Original file line number Diff line number Diff line change
Expand Up @@ -151,6 +151,7 @@ pub struct ExecuteCommand {
pub command: Vec<String>,
pub is_exec: bool,
pub is_compress: bool,
pub output_width: Option<usize>,
pub tx: Sender<AppEvent>,
}

Expand All @@ -162,6 +163,7 @@ impl ExecuteCommand {
command: vec![],
is_exec: false,
is_compress: false,
output_width: None,
tx,
}
}
Expand All @@ -181,11 +183,17 @@ impl ExecuteCommand {

// exec command...
let length = exec_commands.len();
let child_result = Command::new(&exec_commands[0])
let mut child_command = Command::new(&exec_commands[0]);
child_command
.args(&exec_commands[1..length])
.stdout(Stdio::piped())
.stderr(Stdio::piped())
.spawn();
.stderr(Stdio::piped());
if let Some(cols) = self.output_width {
child_command
.env("COLUMNS", cols.to_string())
.env("HWATCH_COLUMNS", cols.to_string());
}
let child_result = child_command.spawn();

// merge stdout and stderr
let mut vec_output = Vec::new();
Expand Down
9 changes: 8 additions & 1 deletion src/main.rs
Original file line number Diff line number Diff line change
Expand Up @@ -92,7 +92,9 @@ pub const DEFAULT_TAB_SIZE: u16 = 4;
pub const HISTORY_WIDTH: u16 = 25;
pub const SHELL_COMMAND_EXECCMD: &str = "{COMMAND}";
pub const HISTORY_LIMIT: &str = "5000";
// TODO: Change to mutex? Rename to ...Mutex?
type Interval = Arc<RwLock<f64>>;
type OutputWidthShared = Arc<RwLock<Option<usize>>>;

// const at Windows
#[cfg(windows)]
Expand Down Expand Up @@ -417,6 +419,7 @@ fn main() {
.get_one::<f64>("interval")
.unwrap_or(&DEFAULT_INTERVAL);
let interval = Interval::new(override_interval.into());
let output_width = OutputWidthShared::new(None.into());

// history limit
let default_limit: u32 = HISTORY_LIMIT.parse().unwrap();
Expand Down Expand Up @@ -477,6 +480,7 @@ fn main() {
.collect();
let is_exec = m.get_flag("exec");
let interval = interval.clone();
let output_width = output_width.clone();
let _ = thread::spawn(move || loop {
// Create cmd..
let mut exe = exec::ExecuteCommand::new(tx.clone());
Expand All @@ -493,9 +497,12 @@ fn main() {
// Set is exec flag.
exe.is_exec = is_exec;

exe.output_width = *output_width.read().unwrap();

// Exec command
exe.exec_command();

// !
let sleep_interval = *interval.read().unwrap();
std::thread::sleep(Duration::from_secs_f64(sleep_interval));
});
Expand All @@ -505,7 +512,7 @@ fn main() {
if !batch {
// is watch mode
// Create view
let mut view = view::View::new(interval.clone())
let mut view = view::View::new(interval.clone(), output_width)
// Set interval on view.header
.set_interval(interval)
.set_tab_size(tab_size)
Expand Down
6 changes: 4 additions & 2 deletions src/view.rs
Original file line number Diff line number Diff line change
Expand Up @@ -32,6 +32,7 @@ use crate::DEFAULT_TAB_SIZE;
pub struct View {
after_command: String,
interval: Interval,
output_width: Arc<RwLock<Option<usize>>>,
tab_size: u16,
limit: u32,
keymap: Keymap,
Expand All @@ -52,10 +53,11 @@ pub struct View {

///
impl View {
pub fn new(interval: Interval) -> Self {
pub fn new(interval: Interval, output_width: Arc<RwLock<Option<usize>>>) -> Self {
Self {
after_command: "".to_string(),
interval,
output_width,
tab_size: DEFAULT_TAB_SIZE,
limit: 0,
keymap: default_keymap(),
Expand Down Expand Up @@ -188,7 +190,7 @@ impl View {
}

// Create App
let mut app = App::new(tx, rx, self.interval.clone());
let mut app = App::new(tx, rx, self.interval.clone(), self.output_width.clone());

// set keymap
app.set_keymap(self.keymap.clone());
Expand Down
1 change: 1 addition & 0 deletions src/watch.rs
Original file line number Diff line number Diff line change
Expand Up @@ -63,6 +63,7 @@ impl<'a> WatchArea<'a> {
}

///
// TODO: Rename to get_area_height?
pub fn get_area_size(&mut self) -> i16 {
let height = self.area.height as i16;

Expand Down

0 comments on commit 3f8bc17

Please sign in to comment.