termsize-0.1.9/.cargo_vcs_info.json 0000644 00000000136 00000000001 0012705 0 ustar {
"git": {
"sha1": "555bc864343114e308ef67a5fa68785c806daef1"
},
"path_in_vcs": ""
} termsize-0.1.9/.github/workflows/ci.yml 0000644 0000000 0000000 00000003240 10461020230 0016207 0 ustar 0000000 0000000
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 check termsize-0.1.9/.gitignore 0000644 0000000 0000000 00000000022 10461020230 0013457 0 ustar 0000000 0000000 target
Cargo.lock
termsize-0.1.9/.travis.yml 0000644 0000000 0000000 00000002504 10461020230 0013607 0 ustar 0000000 0000000 language: 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.md 0000644 0000000 0000000 00000002303 10461020230 0013304 0 ustar 0000000 0000000 # 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.lock 0000644 00000002064 00000000001 0010662 0 ustar # 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.toml 0000644 00000002107 00000000001 0010703 0 ustar # 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.orig 0000644 0000000 0000000 00000001276 10461020230 0014372 0 ustar 0000000 0000000 [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/LICENSE 0000644 0000000 0000000 00000002044 10461020230 0012502 0 ustar 0000000 0000000 Copyright (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.md 0000644 0000000 0000000 00000002166 10461020230 0012761 0 ustar 0000000 0000000 # termsize
[](https://github.com/softprops/termsize/actions/workflows/ci.yml)
[](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-2024 termsize-0.1.9/appveyor.yml 0000644 0000000 0000000 00000001035 10461020230 0014064 0 ustar 0000000 0000000 environment:
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.rs 0000644 0000000 0000000 00000000207 10461020230 0014306 0 ustar 0000000 0000000 pub fn main() {
match termsize::get() {
Some(size) => println!("{:?}", size),
_ => println!("not a term"),
}
}
termsize-0.1.9/rustfmt.toml 0000644 0000000 0000000 00000000556 10461020230 0014104 0 ustar 0000000 0000000 # 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 = true termsize-0.1.9/src/lib.rs 0000644 0000000 0000000 00000001413 10461020230 0013377 0 ustar 0000000 0000000 #![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.rs 0000644 0000000 0000000 00000004301 10461020230 0013426 0 ustar 0000000 0000000 extern 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.rs 0000644 0000000 0000000 00000000124 10461020230 0013750 0 ustar 0000000 0000000 /// Gets the current terminal size
pub fn get() -> Option {
None
}
termsize-0.1.9/src/win.rs 0000644 0000000 0000000 00000002413 10461020230 0013427 0 ustar 0000000 0000000 use 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,
})
}