termsize-0.1.9/.cargo_vcs_info.json0000644000000001360000000000100127050ustar { "git": { "sha1": "555bc864343114e308ef67a5fa68785c806daef1" }, "path_in_vcs": "" }termsize-0.1.9/.github/workflows/ci.yml000064400000000000000000000032401046102023000162070ustar 00000000000000 name: CI on: push: paths-ignore: - '*.md' branches: - main - master tags: - '**' pull_request: paths-ignore: - '*.md' branches: - main - master env: CARGO_TERM_COLOR: always jobs: codestyle: runs-on: ubuntu-latest steps: - name: Setup Rust uses: hecrj/setup-rust-action@v2 with: components: rustfmt rust-version: nightly - name: Setup rust cache uses: Swatinem/rust-cache@v2 - uses: actions/checkout@v4 - run: cargo fmt --all -- --check lint: runs-on: ubuntu-latest steps: - name: Setup Rust uses: hecrj/setup-rust-action@v2 with: components: clippy - name: Setup rust cache uses: Swatinem/rust-cache@v2 - uses: actions/checkout@v4 - run: cargo clippy --all-targets -- -D clippy::all compile: runs-on: ubuntu-latest steps: - name: Setup Rust uses: hecrj/setup-rust-action@v2 - name: Setup rust cache uses: Swatinem/rust-cache@v2 - uses: actions/checkout@master - run: cargo check --all test: needs: [codestyle, lint, compile] strategy: matrix: os: [ubuntu-latest, macos-latest, windows-latest] runs-on: ${{ matrix.os }} steps: - name: Setup Rust uses: hecrj/setup-rust-action@v2 - name: Setup rust cache uses: Swatinem/rust-cache@v2 - name: Checkout uses: actions/checkout@v4 - name: Test # because ci doesn't run in a tty env, running test will fail # by we can at the very least make sure we compile on all supported os's run: cargo checktermsize-0.1.9/.gitignore000064400000000000000000000000221046102023000134570ustar 00000000000000target Cargo.lock termsize-0.1.9/.travis.yml000064400000000000000000000025041046102023000136070ustar 00000000000000language: rust sudo: false matrix: fast_finish: true include: - rust: nightly - rust: nightly os: osx - rust: beta - rust: beta os: osx - rust: stable - rust: stable os: osx allow_failures: - rust: nightly cache: cargo: true apt: true directories: - target/debug/deps - target/debug/build addons: apt: packages: - libcurl4-openssl-dev - libelf-dev - libdw-dev - binutils-dev - libiberty-dev before_cache: # Travis can't cache files that are not readable by "others" - chmod -R a+r $HOME/.cargo # https://github.com/travis-ci/travis-ci/issues/7669#issuecomment-299149209 before_install: - > if [ "$TRAVIS_OS_NAME" == "linux" ]; then stty cols 80 else # man resize echo "osx on travis doesnt support stty cols 80" fi script: - cargo build after_success: - '[ $TRAVIS_RUST_VERSION = stable ] && [ $TRAVIS_BRANCH = master ] && [ $TRAVIS_PULL_REQUEST = false ] && cargo doc --no-deps && echo "" > target/doc/index.html && pip install --user ghp-import && /home/travis/.local/bin/ghp-import -n target/doc && git push -fq https://${GH_TOKEN}@github.com/${TRAVIS_REPO_SLUG}.git gh-pages && echo "documented"'termsize-0.1.9/CHANGELOG.md000064400000000000000000000023031046102023000133040ustar 00000000000000# 0.1.9 * drop `atty` dependency in favor of `std.io.IsTerminal` # 0.1.8 * switch to gh actions for ci # 0.1.7 * add shim for unsupported platforms [#14](https://github.com/softprops/termsize/pull/14) via [@RReverser](https://github.com/RReverser) * Prevent the Unix implementation being optimised away in release modes [#18](https://github.com/softprops/termsize/pull/18) via [@dspeyrer](https://github.com/dspeyrer) # 0.1.6 * fix `TIOCGWINSZ` size issue on FreeBSD [#6](https://github.com/softprops/termsize/pull/9) # 0.1.5 * added support for [Redox OS](https://github.com/redox-os/redox) [@ids1024](https://github.com/softprops/termsize/pull/8) # 0.1.4 * added support for [Fuchsia](https://en.wikipedia.org/wiki/Google_Fuchsia) OS [@antiagainst](https://github.com/softprops/termsize/pull/5) # 0.1.3 * add support for new targets arm-unknown-linux-gnueabihf and armv7-unknown-linux-gnueabihf [@wimh](https://github.com/softprops/termsize/pull/3) # 0.1.2 * replaced hardcoded per-platform constants for TIOCGWINSZ with the one defined in libc [@antiagainst](https://github.com/softprops/termsize/pull/4) # 0.1.1 * fixed broken windows support (added automated testing) # 0.1.0 * initial release termsize-0.1.9/Cargo.lock0000644000000020640000000000100106620ustar # This file is automatically @generated by Cargo. # It is not intended for manual editing. version = 3 [[package]] name = "libc" version = "0.2.155" source = "registry+https://github.com/rust-lang/crates.io-index" checksum = "97b3888a4aecf77e811145cadf6eef5901f4782c53886191b2f693f24761847c" [[package]] name = "termsize" version = "0.1.9" dependencies = [ "libc", "winapi", ] [[package]] name = "winapi" version = "0.3.9" source = "registry+https://github.com/rust-lang/crates.io-index" checksum = "5c839a674fcd7a98952e593242ea400abe93992746761e38641405d28b00f419" dependencies = [ "winapi-i686-pc-windows-gnu", "winapi-x86_64-pc-windows-gnu", ] [[package]] name = "winapi-i686-pc-windows-gnu" version = "0.4.0" source = "registry+https://github.com/rust-lang/crates.io-index" checksum = "ac3b87c63620426dd9b991e5ce0329eff545bccbbb34f3be09ff6fb6ab51b7b6" [[package]] name = "winapi-x86_64-pc-windows-gnu" version = "0.4.0" source = "registry+https://github.com/rust-lang/crates.io-index" checksum = "712e227841d057c1ee1cd2fb22fa7e5a5461ae8e48fa2ca79ec42cfc1931183f" termsize-0.1.9/Cargo.toml0000644000000021070000000000100107030ustar # THIS FILE IS AUTOMATICALLY GENERATED BY CARGO # # When uploading crates to the registry Cargo will automatically # "normalize" Cargo.toml files for maximal compatibility # with all versions of Cargo and also rewrite `path` dependencies # to registry (e.g., crates.io) dependencies. # # If you are reading this file be aware that the original Cargo.toml # will likely look very different (and much more reasonable). # See Cargo.toml.orig for the original contents. [package] edition = "2021" rust-version = "1.70" name = "termsize" version = "0.1.9" authors = ["softprops "] description = "Retrieves terminal size" homepage = "https://github.com/softprops/termsize" documentation = "http://softprops.github.io/termsize" readme = "README.md" keywords = [ "tty", "terminal", "term", "size", "dimensions", ] license = "MIT" repository = "https://github.com/softprops/termsize" [target."cfg(unix)".dependencies.libc] version = "0.2" [target."cfg(windows)".dependencies.winapi] version = "0.3" features = [ "handleapi", "fileapi", "wincon", ] termsize-0.1.9/Cargo.toml.orig000064400000000000000000000012761046102023000143720ustar 00000000000000[package] name = "termsize" version = "0.1.9" authors = ["softprops "] description = "Retrieves terminal size" repository = "https://github.com/softprops/termsize" homepage = "https://github.com/softprops/termsize" documentation = "http://softprops.github.io/termsize" keywords = ["tty", "terminal", "term", "size", "dimensions"] license = "MIT" readme = "README.md" edition = "2021" rust-version = "1.70" #[badges] #travis-ci = { repository = "softprops/termsize" } #coveralls = { repository = "softprops/termsize" } [target.'cfg(unix)'.dependencies] libc = "0.2" [target.'cfg(windows)'.dependencies] winapi = { version = "0.3", features = ["handleapi", "fileapi", "wincon"] } termsize-0.1.9/LICENSE000064400000000000000000000020441046102023000125020ustar 00000000000000Copyright (c) 2015-2024 Doug Tangren Permission is hereby granted, free of charge, to any person obtaining a copy of this software and associated documentation files (the "Software"), to deal in the Software without restriction, including without limitation the rights to use, copy, modify, merge, publish, distribute, sublicense, and/or sell copies of the Software, and to permit persons to whom the Software is furnished to do so, subject to the following conditions: The above copyright notice and this permission notice shall be included in all copies or substantial portions of the Software. THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.termsize-0.1.9/README.md000064400000000000000000000021661046102023000127610ustar 00000000000000# termsize [![CI](https://github.com/softprops/termsize/actions/workflows/ci.yml/badge.svg)](https://github.com/softprops/termsize/actions/workflows/ci.yml) [![Crates.io](https://img.shields.io/crates/v/termsize.svg)](https://crates.io/crates/termsize) > because terminal size matters Termsize is a rust crate providing a multi-platform interface for resolving your terminal's current size in rows and columns. On most unix systems, this is similar invoking the [stty(1)](http://man7.org/linux/man-pages/man1/stty.1.html) program, requesting the terminal size. ## [Documentation](https://softprops.github.com/termsize) ## install run `cargo add termsize` in your terminal or add the following to your `Cargo.toml` file ```toml [dependencies] termsize = "0.1" ``` ## usage Termize provides one function, `get`, which returns a `termsize::Size` struct exposing two fields: `rows` and `cols` representing the number of rows and columns a a terminal's stdout supports. ```rust pub fn main() { termsize::get().map(|{ rows, cols }| { println!("rows {} cols {}", size.rows, size.cols) }); } ``` Doug Tangren (softprops) 2015-2024termsize-0.1.9/appveyor.yml000064400000000000000000000010351046102023000140640ustar 00000000000000environment: matrix: - TARGET: nightly-x86_64-pc-windows-msvc - TARGET: nightly-i686-pc-windows-msvc - TARGET: nightly-x86_64-pc-windows-gnu - TARGET: nightly-i686-pc-windows-gnu install: - ps: Start-FileDownload "https://static.rust-lang.org/dist/rust-${env:TARGET}.exe" -FileName "rust-install.exe" - ps: .\rust-install.exe /VERYSILENT /NORESTART /DIR="C:\rust" | Out-Null - ps: $env:PATH="$env:PATH;C:\rust\bin" - call "%VCVARS%" || ver>nul - rustc -vV - cargo -vV build: false test_script: - cargo test --verbose termsize-0.1.9/examples/ts.rs000064400000000000000000000002071046102023000143060ustar 00000000000000pub fn main() { match termsize::get() { Some(size) => println!("{:?}", size), _ => println!("not a term"), } } termsize-0.1.9/rustfmt.toml000064400000000000000000000005561046102023000141040ustar 00000000000000# https://github.com/rust-lang/rustfmt/blob/master/Configurations.md#fn_params_layout fn_params_layout = "Vertical" # https://github.com/rust-lang/rustfmt/blob/master/Configurations.md#imports_granularity imports_granularity = "Crate" # https://github.com/rust-lang/rustfmt/blob/master/Configurations.md#format_code_in_doc_comments format_code_in_doc_comments = truetermsize-0.1.9/src/lib.rs000064400000000000000000000014131046102023000133770ustar 00000000000000#![deny(missing_docs)] //! Termsize is a tiny crate that provides a simple //! interface for retrieving the current //! [terminal interface](http://www.manpagez.com/man/4/tty/) size //! //! ```rust //! extern crate termsize; //! //! termsize::get().map(|size| println!("rows {} cols {}", size.rows, size.cols)); //! ``` /// Container for number of rows and columns #[derive(Debug)] pub struct Size { /// number of rows pub rows: u16, /// number of columns pub cols: u16, } #[cfg(unix)] #[path = "nix.rs"] mod imp; #[cfg(windows)] #[path = "win.rs"] mod imp; #[cfg(not(any(unix, windows)))] #[path = "other.rs"] mod imp; pub use imp::get; #[cfg(test)] mod tests { use super::get; #[test] fn test_get() { assert!(get().is_some()) } } termsize-0.1.9/src/nix.rs000064400000000000000000000043011046102023000134260ustar 00000000000000extern crate libc; use std::io::IsTerminal; use self::{ super::Size, libc::{c_ushort, ioctl, STDOUT_FILENO, TIOCGWINSZ}, }; /// A representation of the size of the current terminal #[repr(C)] #[derive(Debug)] pub struct UnixSize { /// number of rows pub rows: c_ushort, /// number of columns pub cols: c_ushort, x: c_ushort, y: c_ushort, } /// Gets the current terminal size pub fn get() -> Option { // http://rosettacode.org/wiki/Terminal_control/Dimensions#Library:_BSD_libc if !std::io::stdout().is_terminal() { return None; } let mut us = UnixSize { rows: 0, cols: 0, x: 0, y: 0, }; let r = unsafe { ioctl(STDOUT_FILENO, TIOCGWINSZ, &mut us) }; if r == 0 { Some(Size { rows: us.rows, cols: us.cols, }) } else { None } } #[cfg(test)] mod tests { use super::{super::Size, get}; use std::process::{Command, Output, Stdio}; #[cfg(target_os = "macos")] fn stty_size() -> Output { Command::new("stty") .arg("-f") .arg("/dev/stderr") .arg("size") .stderr(Stdio::inherit()) .output() .expect("expected stty output") } #[cfg(not(target_os = "macos"))] fn stty_size() -> Output { Command::new("stty") .arg("-F") .arg("/dev/stderr") .arg("size") .stderr(Stdio::inherit()) .output() .expect("expected stty output") } #[test] fn test_shell() { let output = stty_size(); assert!(output.status.success()); let stdout = String::from_utf8(output.stdout).expect("expected utf8"); let mut data = stdout.split_whitespace(); let rs = data .next() .expect("expected row") .parse::() .expect("expected u16 col"); let cs = data .next() .expect("expected col") .parse::() .expect("expected u16 col"); if let Some(Size { rows, cols }) = get() { assert_eq!(rows, rs); assert_eq!(cols, cs); } } } termsize-0.1.9/src/other.rs000064400000000000000000000001241046102023000137500ustar 00000000000000/// Gets the current terminal size pub fn get() -> Option { None } termsize-0.1.9/src/win.rs000064400000000000000000000024131046102023000134270ustar 00000000000000use std::ptr; use winapi::um::{ fileapi::{CreateFileA, OPEN_EXISTING}, handleapi::INVALID_HANDLE_VALUE, wincon::{GetConsoleScreenBufferInfo, CONSOLE_SCREEN_BUFFER_INFO}, winnt::{FILE_SHARE_WRITE, GENERIC_READ, GENERIC_WRITE}, }; use self::super::Size; /// Gets the current terminal size pub fn get() -> Option { // http://rosettacode.org/wiki/Terminal_control/Dimensions#Windows let handle = unsafe { CreateFileA( b"CONOUT$\0".as_ptr() as *const i8, GENERIC_READ | GENERIC_WRITE, FILE_SHARE_WRITE, ptr::null_mut(), OPEN_EXISTING, 0, ptr::null_mut(), ) }; if handle == INVALID_HANDLE_VALUE { return None; } let info = unsafe { // https://msdn.microsoft.com/en-us/library/windows/desktop/ms683171(v=vs.85).aspx let mut info = ::std::mem::MaybeUninit::::uninit(); if GetConsoleScreenBufferInfo(handle, info.as_mut_ptr()) == 0 { None } else { Some(info.assume_init()) } }; info.map(|inf| Size { rows: (inf.srWindow.Bottom - inf.srWindow.Top + 1) as u16, cols: (inf.srWindow.Right - inf.srWindow.Left + 1) as u16, }) }