Skip to content

Instantly share code, notes, and snippets.

@alex-pat
Last active June 8, 2018 14:00
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save alex-pat/116470872825f444384dc9cda98df667 to your computer and use it in GitHub Desktop.
Save alex-pat/116470872825f444384dc9cda98df667 to your computer and use it in GitHub Desktop.
extern crate pancurses;
use pancurses::{endwin, getmouse, initscr, mousemask, noecho, Input, ALL_MOUSE_EVENTS};
fn draw_border(win: &pancurses::Window) {
let (y, x) = win.get_max_yx();
let (oy, ox) = win.get_cur_yx();
win.clear();
win.mvprintw(0, 0, "┏");
win.mvprintw(y - 1, 0, "┗");
win.mvprintw(0, x - 1, "┓");
win.mvprintw(y - 1, x - 1, "┛");
for i in 1..x - 1 {
win.mvprintw(0, i, "━");
win.mvprintw(y - 1, i, "━");
}
for i in 1..y - 1 {
win.mvprintw(i, 0, "┃");
win.mvprintw(i, x - 1, "┃");
}
win.mv(oy, ox);
}
fn main() {
let window = initscr();
window.keypad(true);
noecho();
mousemask(ALL_MOUSE_EVENTS, std::ptr::null_mut());
window.getch();
draw_border(&window);
window.refresh();
loop {
match window.getch() {
Some(Input::KeyMouse) => {
if let Ok(e) = getmouse() {
window.mvaddch(e.y, e.x, '*');
}
}
Some(Input::KeyResize) => {
pancurses::resize_term(0, 0);
draw_border(&window);
}
Some(Input::Character(c)) if c == 'q' => break,
_ => (),
};
}
endwin();
}
extern crate pancurses;
use pancurses::*;
use std::cmp::{min, max};
fn draw_border(win: &Window, fpoint: (i32, i32), spoint: (i32, i32)) {
let (_y1, _x1) = fpoint;
let (_y2, _x2) = spoint;
let (oy, ox) = win.get_cur_yx();
let y1 = min(_y1, _y2);
let y2 = max(_y1, _y2);
let x1 = min(_x1, _x2);
let x2 = max(_x1, _x2);
for i in x1..x2 {
win.mvprintw(y1, i, "━");
win.mvprintw(y2, i, "━");
}
for i in y1..y2 {
win.mvprintw(i, x1, "┃");
win.mvprintw(i, x2, "┃");
}
win.mvprintw(y1, x1, "┏");
win.mvprintw(y2, x1, "┗");
win.mvprintw(y1, x2, "┓");
win.mvprintw(y2, x2, "┛");
win.mv(oy, ox);
}
fn big_border(win: &Window) {
let (y, x) = win.get_max_yx();
draw_border(&win, (0,0), (y-1, x-1));
}
fn main() {
let window = initscr();
let mut last_yx = (0, 0);
window.keypad(true);
noecho();
mousemask(ALL_MOUSE_EVENTS, std::ptr::null_mut());
window.getch();
big_border(&window);
window.refresh();
loop {
match window.getch() {
Some(Input::KeyMouse) => {
if let Ok(e) = getmouse() {
match e.bstate {
BUTTON1_PRESSED => last_yx = (e.y, e.x),
BUTTON1_RELEASED => draw_border(&window, last_yx, (e.y, e.x)),
_ => ()
};
}
}
Some(Input::KeyResize) => {
resize_term(0, 0);
window.clear();
big_border(&window);
}
Some(Input::Character(c)) if c == 'q' => break,
Some(Input::Character(c)) if c == 'l' => {
window.clear();
big_border(&window);
},
_ => (),
};
}
endwin();
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment