termsize library added

Used termsize to find size of the terminal and display a box properly. Changes depending on size of terminal upon start.
This commit is contained in:
Daniel Jones 2023-03-18 12:48:14 -05:00
parent 776d71b16c
commit 395b1d1ce0
2 changed files with 14 additions and 14 deletions

View File

@ -6,4 +6,5 @@ edition = "2021"
# See more keys and their definitions at https://doc.rust-lang.org/cargo/reference/manifest.html # See more keys and their definitions at https://doc.rust-lang.org/cargo/reference/manifest.html
[dependencies] [dependencies]
ncurses = "5.101.0" ncurses = "5.101.0"
termsize = "0.1"

View File

@ -12,6 +12,7 @@ use std::fs;
use std::io::prelude::*; use std::io::prelude::*;
use std::env; use std::env;
extern crate ncurses; extern crate ncurses;
extern crate termsize;
//initial function. Reads the ebook passed by argument. //initial function. Reads the ebook passed by argument.
@ -23,21 +24,14 @@ fn main() {
} }
fn get_terminal_size()-> (i8, i32, i32){
let mut stdscr: i8 = 0;
let mut w: i32 = 0; //width
let mut h: i32 = 0; //height
ncurses::getmaxyx(&mut stdscr, &mut h, &mut w);
return (stdscr, h, w);
}
//Gets files in directory and returns string with contents. //Gets files in directory and returns string with contents.
fn get_directory() -> String{ fn get_directory() -> String{
let mut dir: String = "".to_string(); let mut dir: String = "".to_string();
let mut hold: String; let mut hold: String;
let paths = fs::read_dir("./").unwrap(); let paths = fs::read_dir("./").unwrap();
@ -50,13 +44,18 @@ fn get_directory() -> String{
return dir; return dir;
} }
//Generates menu screen
fn menu(){ fn menu(){
let termsize::Size {mut rows, mut cols} = termsize::get().unwrap();
let (mut stdscr, height, width): (i8, i32, i32) = get_terminal_size();
println!("height: {} \nwidth: {} \n", rows, cols);
rows -= 5;
cols -= 5;
ncurses::initscr(); ncurses::initscr();
ncurses::clear(); ncurses::clear();
let win = ncurses::newwin(100, 100, 5, 5); let win = ncurses::newwin(rows as i32, cols as i32, 0, 0);
ncurses::wprintw(win, &get_directory()); ncurses::wprintw(win, &get_directory());
ncurses::refresh(); ncurses::refresh();
ncurses::box_(win, 0, 0); ncurses::box_(win, 0, 0);