Skip to content

Commit

Permalink
Merge pull request #25 from SoptikHa2/update-dependencies
Browse files Browse the repository at this point in the history
Update dependencies
  • Loading branch information
SoptikHa2 authored Oct 14, 2022
2 parents 7ae641b + c960d3a commit 26c910f
Show file tree
Hide file tree
Showing 5 changed files with 72 additions and 52 deletions.
3 changes: 3 additions & 0 deletions .gitignore
Original file line number Diff line number Diff line change
Expand Up @@ -12,3 +12,6 @@ Cargo.lock
# AUR package files - stored in AUR repository
aur/desed
aur/desed-git

# Ignore IDEs
.idea
16 changes: 8 additions & 8 deletions Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -7,24 +7,24 @@ readme = "README.md"
license = "GPL-3.0-or-later"
keywords = ["awk", "debugger", "debugging", "tui"]
categories = ["command-line-utilities", "development-tools", "development-tools::debugging"]
version = "1.2.0"
version = "1.2.1"
authors = ["Petr Šťastný <[email protected]>"]
edition = "2018"


[dependencies]
# Parsing CLI arguments
clap = "2.33.0"
clap = { version = "3.0.14", features = ["cargo"] }
# TUI
crossterm = "0.17.3"
tui = { version = "0.9.1", default-features = false, features = ["crossterm"] }
crossterm = "0.25.0"
tui = { version = "0.19.0", default-features = false, features = ["crossterm"] }
# Flexible error handling
anyhow = "1.0.31"
anyhow = "1.0.65"
# For easier handling of conditional compilation
cfg-if = "0.1.10"
cfg-if = "1.0.0"

[target.'cfg(target_os = "linux")'.dependencies]
inotify = "0.8.3" # Watch files and auto-reload on changes
inotify = "0.10.0" # Watch files and auto-reload on changes

[target.'cfg(any(target_os="darwin", target_os="dragonfly", target_os="freebsd", target_os="netbsd", target_os="openbsd"))'.dependencies]
kqueue = "1.0.1" # Watch files and auto-reload on changes
kqueue = "1.0.6" # Watch files and auto-reload on changes
10 changes: 5 additions & 5 deletions src/cli.rs
Original file line number Diff line number Diff line change
Expand Up @@ -9,14 +9,14 @@ pub fn parse_arguments<'a, 'b>() -> Result<Options> {
.author("Petr Šťastný <[email protected]>")
.about("Sed script debugger. Debug and demystify your sed scripts with TUI debugger.")
.arg(Arg::with_name("sed_n")
.short("n")
.short('n')
.long("quiet")
.long("silent")
.help("sed: suppress automatic printing of pattern space")
.takes_value(false)
.required(false))
.arg(Arg::with_name("sed_E")
.short("E")
.short('E')
.long("regexp-extended")
.help("sed: use extended regular expressions in the script")
.takes_value(false)
Expand All @@ -28,19 +28,19 @@ pub fn parse_arguments<'a, 'b>() -> Result<Options> {
.required(false))
.arg(Arg::with_name("sed_z")
.long("null-data")
.short("z")
.short('z')
.help("sed: separate lines by NUL characters")
.takes_value(false)
.required(false))
.arg(Arg::with_name("verbose")
.long("verbose")
.short("v")
.short('v')
.help("This will enable various debug printing to stderr.")
.takes_value(false)
.required(false))
.arg(Arg::with_name("sed-path")
.long("sed-path")
.help("Specify path to sed that should be used.")
.help("Specify path to sed that should be used. If omitted, gsed/sed from your $PATH will run.")
.takes_value(true)
.required(false))
.arg(Arg::with_name("sed-script")
Expand Down
7 changes: 6 additions & 1 deletion src/main.rs
Original file line number Diff line number Diff line change
Expand Up @@ -10,10 +10,15 @@ use ui::generic::{ApplicationExitReason, UiAgent};
use ui::tui::Tui;

fn main() {
// If an error occurs, we do not want to clear terminal, it's useful for the error to remain visible.
// But we want to clear terminal when user just exited GUI normally.
let mut clear_terminal: bool = true;

if let Err(error) = run(0) {
eprintln!("An error occured: {}", error);
clear_terminal = false;
}
if let Err(error) = Tui::restore_terminal_state() {
if let Err(error) = Tui::restore_terminal_state(clear_terminal) {
eprintln!("An error occured while attempting to reset terminal to previous state. Consider using 'reset' command. Error: {}", error);
}
}
Expand Down
88 changes: 50 additions & 38 deletions src/ui/tui.rs
Original file line number Diff line number Diff line change
Expand Up @@ -2,19 +2,20 @@ use crate::file_watcher::FileWatcher;
use crate::sed::debugger::{Debugger, DebuggingState};
use crate::ui::generic::{ApplicationExitReason, UiAgent};
use anyhow::{Context, Result};
use crossterm::event::{self, Event, KeyCode, KeyEvent, MouseEvent};
use crossterm::event::{self, Event, KeyCode, KeyEvent, MouseEvent, MouseEventKind};
use crossterm::execute;
use std::cmp::{max, min};
use std::collections::HashSet;
use std::io::{self, Write};
use std::io;
use std::sync::mpsc;
use std::thread;
use std::time::{Duration, Instant};
use tui::backend::{Backend, CrosstermBackend};
use tui::layout::{Constraint, Direction, Layout, Rect};
use tui::style::{Color, Modifier, Style};
use tui::terminal::Frame;
use tui::widgets::{Block, Borders, Paragraph, Text};
use tui::text::{Text, Span, Spans};
use tui::widgets::{Block, Borders, Paragraph, Wrap};
use tui::Terminal;

pub struct Tui<'a> {
Expand Down Expand Up @@ -155,7 +156,7 @@ impl<'a> Tui<'a> {
}
}

/// Draw source code into main window.
/// Draw source code into main window.github.com/s2e/s2e
///
/// Handles scrolling and breakpoint display as well.
///
Expand All @@ -173,7 +174,7 @@ impl<'a> Tui<'a> {
let block_source_code = Block::default()
.title(" Source code ")
.borders(Borders::ALL);
let mut text_output: Vec<Text> = Vec::new();
let mut text_output: Vec<Spans> = Vec::new();

// Scroll:
// Focused line is line that should always be at the center of the screen.
Expand Down Expand Up @@ -238,23 +239,25 @@ impl<'a> Tui<'a> {
format!("{: <4}", (line_number + 1))
};
// Send the line we defined earlier to be displayed
text_output.push(Text::styled(
linenr_format,
Style::default().fg(linenr_color).bg(linenr_bg_color),
));
if let Some(source) = source_code.get(line_number) {
text_output.push(Text::raw(source));
}
text_output.push(Text::raw("\n"));
text_output.push(Spans::from(vec![
Span::styled(
linenr_format,
Style::default().fg(linenr_color).bg(linenr_bg_color),
),
if let Some(source) = source_code.get(line_number) {
Span::raw(source)
} else { Span::raw("") }
]));

};
for number in display_start..source_code.len() {
add_new_line(number);
}
// Add one more "phantom" line so we see line where current segment execution ends
add_new_line(source_code.len());
let paragraph = Paragraph::new(text_output.iter())
let paragraph = Paragraph::new(text_output)
.block(block_source_code)
.wrap(true);
.wrap(Wrap { trim: false });
f.render_widget(paragraph, area);
}

Expand All @@ -264,26 +267,27 @@ impl<'a> Tui<'a> {
let block_regex_space = Block::default()
.title(" Regex matches ")
.borders(Borders::ALL);
let mut text: Vec<Text> = Vec::new();
let mut text: Vec<Spans> = Vec::new();
if regex_space.len() == 0 {
text.push(Text::styled(
text.push(Spans::from(vec![Span::styled(
"\nNo matches",
Style::default()
.modifier(Modifier::ITALIC)
.add_modifier(Modifier::ITALIC)
.fg(Color::DarkGray),
));
)]));
} else {
for (i, m) in regex_space.iter().enumerate() {
text.push(Text::styled(
text.push(Spans::from(vec![
Span::styled(
format!("\n\\{} ", i),
Style::default().fg(Color::DarkGray),
));
text.push(Text::raw(m));
Style::default().fg(Color::DarkGray)),
Span::raw(m)
]));
}
}
let paragraph = Paragraph::new(text.iter())
let paragraph = Paragraph::new(text)
.block(block_regex_space)
.wrap(true);
.wrap(Wrap { trim: false });
f.render_widget(paragraph, area);
}

Expand All @@ -294,13 +298,13 @@ impl<'a> Tui<'a> {
text_to_write: Option<&String>,
area: Rect,
) {
let block = Block::default().title(&heading).borders(Borders::ALL);
let block = Block::default().title(heading).borders(Borders::ALL);
let default_string = String::new();
let text = [Text::styled(
let text = [Span::styled(
format!("\n{}", text_to_write.unwrap_or(&default_string)),
Style::default().fg(Color::LightBlue),
)];
let paragraph = Paragraph::new(text.iter()).block(block).wrap(true);
let paragraph = Paragraph::new(Spans::from(text.to_vec())).block(block).wrap(Wrap{ trim: false });
f.render_widget(paragraph, area);
}

Expand All @@ -310,17 +314,25 @@ impl<'a> Tui<'a> {
#[allow(unused_must_use)]
// NOTE: We don't care if we fail to do something here. Terminal might not support everything,
// but we try to restore as much as we can.
pub fn restore_terminal_state() -> Result<()> {
pub fn restore_terminal_state(clear_terminal: bool) -> Result<()> {
let mut stdout = io::stdout();

// Disable mouse control
execute!(stdout, event::DisableMouseCapture);
// Disable raw mode that messes up with user's terminal and show cursor again

// Disable raw mode that messes up with user's terminal
crossterm::terminal::disable_raw_mode();
let backend = CrosstermBackend::new(stdout);
let mut terminal = Terminal::new(backend)?;

// Show cursor again
terminal.show_cursor();
// And clear as much as we can before handing the control of terminal back to user.
terminal.clear();

// Clear terminal if wanted
if clear_terminal {
// And clear as much as we can before handing the control of terminal back to user.
terminal.clear();
}
Ok(())
}
}
Expand Down Expand Up @@ -505,23 +517,23 @@ impl<'a> UiAgent for Tui<'a> {
self.pressed_keys_buffer.clear();
}
},
Interrupt::MouseEvent(event) => match event {
Interrupt::MouseEvent(event) => match event.kind {
// Button pressed, mark current line as breakpoint
MouseEvent::Up(_button, _col, row, _key_modifiers) => {
let target_breakpoint = (row - 1) as usize + draw_memory.current_startline;
MouseEventKind::Up(_button) => {
let target_breakpoint = (event.row - 1) as usize + draw_memory.current_startline;
if self.breakpoints.contains(&target_breakpoint) {
self.breakpoints.remove(&target_breakpoint);
} else {
self.breakpoints.insert(target_breakpoint);
}
}
MouseEvent::ScrollUp(_col, _row, _key_modifiers) => {
MouseEventKind::ScrollUp => {
if self.cursor > 0 {
self.cursor -= 1;
}
use_execution_pointer_as_focus_line = false;
}
MouseEvent::ScrollDown(_col, _row, _key_modifiers) => {
MouseEventKind::ScrollDown => {
if self.cursor < debugger.source_code.len() {
self.cursor += 1;
}
Expand Down Expand Up @@ -552,7 +564,7 @@ impl<'a> UiAgent for Tui<'a> {
},
&mut draw_memory,
)
})?
})?;
}
}
}
Expand Down

0 comments on commit 26c910f

Please sign in to comment.