term_size-0.3.1/.clog.toml 0100664 0001750 0001750 00000000377 13200213275 0013640 0 ustar 00 0000000 0000000 [clog]
repository = "https://github.com/kbknapp/term_size-rs"
outfile = "CHANGELOG.md"
from-latest-tag = true
[sections]
Performance = ["perf"]
Improvements = ["impr", "im", "imp"]
Documentation = ["docs"]
Deprecations = ["depr"]
Examples = ["examples"]
term_size-0.3.1/.gitignore 0100664 0001750 0001750 00000000274 13200213275 0013725 0 ustar 00 0000000 0000000 # Compiled files
*.o
*.so
*.rlib
*.dll
# Executables
*.exe
# Generated by Cargo
/target/
/clap-test/target/
# Cargo files
Cargo.lock
# Temp files
.*~
# Backup files
*.bak
*.bk
*.orig
term_size-0.3.1/.travis.yml 0100664 0001750 0001750 00000001440 13200213275 0014042 0 ustar 00 0000000 0000000 sudo: false
language: rust
rust:
- nightly
- nightly-2016-09-04
- beta
- stable
matrix:
allow_failures:
- rust: nightly
before_script:
- |
pip install 'travis-cargo<0.2' --user &&
export PATH=$HOME/.local/bin:$PATH
script:
- |
cargo build --verbose &&
cargo test --verbose &&
travis-cargo --only nightly build &&
travis-cargo --only nightly bench
addons:
apt:
packages:
- libcurl4-openssl-dev
- libelf-dev
- libdw-dev
after_success:
- |
travis-cargo --only stable coveralls --no-sudo
env:
global:
- TRAVIS_CARGO_NIGHTLY_FEATURE=travis
- secure: JLBlgHY6OEmhJ8woewNJHmuBokTNUv7/WvLkJGV8xk0t6bXBwSU0jNloXwlH7FiQTc4TccX0PumPDD4MrMgxIAVFPmmmlQOCmdpYP4tqZJ8xo189E5zk8lKF5OyaVYCs5SMmFC3cxCsKjfwGIexNu3ck5Uhwe9jI0tqgkgM3URA=
term_size-0.3.1/CHANGELOG.md 0100664 0001750 0001750 00000002347 13200213275 0013551 0 ustar 00 0000000 0000000
## v0.3.0 (2017-04-09)
#### Features
* allows getting the terminal size of all standard streams, or just particular ones ([c7095c95](https://github.com/kbknapp/term_size-rs/commit/c7095c95d633e0a36ea78434bc83349a9711a187))
### v0.2.3 (2017-02-21)
#### Bug Fixes
* Moves the code into distinct modules and adds dummy functions for unsupported platforms
### v0.2.2 (2017-01-29)
* Updates deps `libc` and `clippy`
### v0.2.1 (2016-09-05)
#### Bug Fixes
* uses libc::winsize instead of homegrown ([216986ec](https://github.com/kbknapp/term_size-rs/commit/216986ecdbe528523953a1cde4cf6c329a0f4fbc), closes [#6](https://github.com/kbknapp/term_size-rs/issues/6))
v0.2.0
## v0.2.0 (2016-09-05)
#### Features
* adds support for Windows ([f181c99](https://github.com/kbknapp/term_size-rs/commit/f181c99c0c306b711952a2a4053df904e851413f))
#### Documentation
* **README.md:**
* fixes documentation link ([ca06bf13](https://github.com/kbknapp/term_size-rs/commit/ca06bf132948559032853addd9aa0af022a126e9))
* minor fixups ([1c269046](https://github.com/kbknapp/term_size-rs/commit/1c2690462b1b1db58d46395c6f1cf098dd769e18))
term_size-0.3.1/CONTRIBUTORS.md 0100664 0001750 0001750 00000002050 13200217772 0014214 0 ustar 00 0000000 0000000 the following is a list of contributors:
[
](https://github.com/kbknapp) |[
](https://github.com/nabijaczleweli) |[
](https://github.com/vorner) |[
](https://github.com/nbaksalyar) |[
](https://github.com/kaegi) |
:---: |:---: |:---: |:---: |:---: |
[kbknapp](https://github.com/kbknapp) |[nabijaczleweli](https://github.com/nabijaczleweli) |[vorner](https://github.com/vorner) |[nbaksalyar](https://github.com/nbaksalyar) |[kaegi](https://github.com/kaegi) |
This list was generated by [mgechev/github-contributors-list](https://github.com/mgechev/github-contributors-list)
term_size-0.3.1/Cargo.toml.orig 0100664 0001750 0001750 00000003246 13200217772 0014634 0 ustar 00 0000000 0000000 [package]
name = "term_size"
version = "0.3.1"
authors = ["Kevin K. ", "Benjamin Sago "]
exclude = []
description = "functions for determining terminal sizes and dimensions"
repository = "https://github.com/kbknapp/term_size-rs.git"
documentation = "https://kbknapp.github.io/term_size-rs"
readme = "README.md"
license = "MIT/Apache-2.0"
keywords = ["term", "terminal", "size", "width", "dimension"]
categories = ["command-line-interface"]
[badges]
travis-ci = {repository = "kbknapp/term_size-rs"}
appveyor = {repository = "kbknapp/term_size-rs"}
[dependencies]
clippy = { version = "~0.0.112", optional = true }
[target.'cfg(not(target_os = "windows"))'.dependencies]
libc = "~0.2.20"
[target.'cfg(target_os = "windows")'.dependencies]
kernel32-sys = "~0.2.2"
winapi = "~0.2.8"
[features]
default = []
lints = ["clippy", "nightly"]
nightly = [] # for building with nightly and unstable features
unstable = [] # for building with unstable features on stable Rust
debug = [] # for building with debug messages
travis = ["lints", "nightly"] # for building with travis-cargo
[profile.release]
opt-level = 3
debug = false
rpath = false
lto = true
debug-assertions = false
# codegen-units ignored with lto=true
[profile.dev]
opt-level = 0
debug = true
rpath = false
lto = false
debug-assertions = true
codegen-units = 4
[profile.test]
opt-level = 1
debug = true
rpath = false
lto = false
debug-assertions = true
codegen-units = 2
[profile.bench]
opt-level = 3
debug = false
rpath = false
lto = true
debug-assertions = false
[profile.doc]
opt-level = 0
debug = true
rpath = false
lto = false
debug-assertions = true
codegen-units = 4
term_size-0.3.1/Cargo.toml 0000644 00000004015 0010053 0 ustar 00 # 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 believe there's an error in this file please file an
# issue against the rust-lang/cargo repository. If you're
# editing this file be aware that the upstream Cargo.toml
# will likely look very different (and much more reasonable)
[package]
name = "term_size"
version = "0.3.1"
authors = ["Kevin K. ", "Benjamin Sago "]
exclude = []
description = "functions for determining terminal sizes and dimensions"
documentation = "https://kbknapp.github.io/term_size-rs"
readme = "README.md"
keywords = ["term", "terminal", "size", "width", "dimension"]
categories = ["command-line-interface"]
license = "MIT/Apache-2.0"
repository = "https://github.com/kbknapp/term_size-rs.git"
[profile.test]
opt-level = 1
lto = false
codegen-units = 2
debug = true
debug-assertions = true
rpath = false
[profile.doc]
opt-level = 0
lto = false
codegen-units = 4
debug = true
debug-assertions = true
rpath = false
[profile.bench]
opt-level = 3
lto = true
debug = false
debug-assertions = false
rpath = false
[profile.dev]
opt-level = 0
lto = false
codegen-units = 4
debug = true
debug-assertions = true
rpath = false
[profile.release]
opt-level = 3
lto = true
debug = false
debug-assertions = false
rpath = false
[dependencies.clippy]
version = "~0.0.112"
optional = true
[features]
debug = []
default = []
lints = ["clippy", "nightly"]
nightly = []
travis = ["lints", "nightly"]
unstable = []
[target."cfg(not(target_os = \"windows\"))".dependencies.libc]
version = "~0.2.20"
[target."cfg(target_os = \"windows\")".dependencies.kernel32-sys]
version = "~0.2.2"
[target."cfg(target_os = \"windows\")".dependencies.winapi]
version = "~0.2.8"
[badges.appveyor]
repository = "kbknapp/term_size-rs"
[badges.travis-ci]
repository = "kbknapp/term_size-rs"
term_size-0.3.1/LICENSE-APACHE 0100664 0001750 0001750 00000025137 13200213275 0013666 0 ustar 00 0000000 0000000 Apache License
Version 2.0, January 2004
http://www.apache.org/licenses/
TERMS AND CONDITIONS FOR USE, REPRODUCTION, AND DISTRIBUTION
1. Definitions.
"License" shall mean the terms and conditions for use, reproduction,
and distribution as defined by Sections 1 through 9 of this document.
"Licensor" shall mean the copyright owner or entity authorized by
the copyright owner that is granting the License.
"Legal Entity" shall mean the union of the acting entity and all
other entities that control, are controlled by, or are under common
control with that entity. For the purposes of this definition,
"control" means (i) the power, direct or indirect, to cause the
direction or management of such entity, whether by contract or
otherwise, or (ii) ownership of fifty percent (50%) or more of the
outstanding shares, or (iii) beneficial ownership of such entity.
"You" (or "Your") shall mean an individual or Legal Entity
exercising permissions granted by this License.
"Source" form shall mean the preferred form for making modifications,
including but not limited to software source code, documentation
source, and configuration files.
"Object" form shall mean any form resulting from mechanical
transformation or translation of a Source form, including but
not limited to compiled object code, generated documentation,
and conversions to other media types.
"Work" shall mean the work of authorship, whether in Source or
Object form, made available under the License, as indicated by a
copyright notice that is included in or attached to the work
(an example is provided in the Appendix below).
"Derivative Works" shall mean any work, whether in Source or Object
form, that is based on (or derived from) the Work and for which the
editorial revisions, annotations, elaborations, or other modifications
represent, as a whole, an original work of authorship. For the purposes
of this License, Derivative Works shall not include works that remain
separable from, or merely link (or bind by name) to the interfaces of,
the Work and Derivative Works thereof.
"Contribution" shall mean any work of authorship, including
the original version of the Work and any modifications or additions
to that Work or Derivative Works thereof, that is intentionally
submitted to Licensor for inclusion in the Work by the copyright owner
or by an individual or Legal Entity authorized to submit on behalf of
the copyright owner. For the purposes of this definition, "submitted"
means any form of electronic, verbal, or written communication sent
to the Licensor or its representatives, including but not limited to
communication on electronic mailing lists, source code control systems,
and issue tracking systems that are managed by, or on behalf of, the
Licensor for the purpose of discussing and improving the Work, but
excluding communication that is conspicuously marked or otherwise
designated in writing by the copyright owner as "Not a Contribution."
"Contributor" shall mean Licensor and any individual or Legal Entity
on behalf of whom a Contribution has been received by Licensor and
subsequently incorporated within the Work.
2. Grant of Copyright License. Subject to the terms and conditions of
this License, each Contributor hereby grants to You a perpetual,
worldwide, non-exclusive, no-charge, royalty-free, irrevocable
copyright license to reproduce, prepare Derivative Works of,
publicly display, publicly perform, sublicense, and distribute the
Work and such Derivative Works in Source or Object form.
3. Grant of Patent License. Subject to the terms and conditions of
this License, each Contributor hereby grants to You a perpetual,
worldwide, non-exclusive, no-charge, royalty-free, irrevocable
(except as stated in this section) patent license to make, have made,
use, offer to sell, sell, import, and otherwise transfer the Work,
where such license applies only to those patent claims licensable
by such Contributor that are necessarily infringed by their
Contribution(s) alone or by combination of their Contribution(s)
with the Work to which such Contribution(s) was submitted. If You
institute patent litigation against any entity (including a
cross-claim or counterclaim in a lawsuit) alleging that the Work
or a Contribution incorporated within the Work constitutes direct
or contributory patent infringement, then any patent licenses
granted to You under this License for that Work shall terminate
as of the date such litigation is filed.
4. Redistribution. You may reproduce and distribute copies of the
Work or Derivative Works thereof in any medium, with or without
modifications, and in Source or Object form, provided that You
meet the following conditions:
(a) You must give any other recipients of the Work or
Derivative Works a copy of this License; and
(b) You must cause any modified files to carry prominent notices
stating that You changed the files; and
(c) You must retain, in the Source form of any Derivative Works
that You distribute, all copyright, patent, trademark, and
attribution notices from the Source form of the Work,
excluding those notices that do not pertain to any part of
the Derivative Works; and
(d) If the Work includes a "NOTICE" text file as part of its
distribution, then any Derivative Works that You distribute must
include a readable copy of the attribution notices contained
within such NOTICE file, excluding those notices that do not
pertain to any part of the Derivative Works, in at least one
of the following places: within a NOTICE text file distributed
as part of the Derivative Works; within the Source form or
documentation, if provided along with the Derivative Works; or,
within a display generated by the Derivative Works, if and
wherever such third-party notices normally appear. The contents
of the NOTICE file are for informational purposes only and
do not modify the License. You may add Your own attribution
notices within Derivative Works that You distribute, alongside
or as an addendum to the NOTICE text from the Work, provided
that such additional attribution notices cannot be construed
as modifying the License.
You may add Your own copyright statement to Your modifications and
may provide additional or different license terms and conditions
for use, reproduction, or distribution of Your modifications, or
for any such Derivative Works as a whole, provided Your use,
reproduction, and distribution of the Work otherwise complies with
the conditions stated in this License.
5. Submission of Contributions. Unless You explicitly state otherwise,
any Contribution intentionally submitted for inclusion in the Work
by You to the Licensor shall be under the terms and conditions of
this License, without any additional terms or conditions.
Notwithstanding the above, nothing herein shall supersede or modify
the terms of any separate license agreement you may have executed
with Licensor regarding such Contributions.
6. Trademarks. This License does not grant permission to use the trade
names, trademarks, service marks, or product names of the Licensor,
except as required for reasonable and customary use in describing the
origin of the Work and reproducing the content of the NOTICE file.
7. Disclaimer of Warranty. Unless required by applicable law or
agreed to in writing, Licensor provides the Work (and each
Contributor provides its Contributions) on an "AS IS" BASIS,
WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or
implied, including, without limitation, any warranties or conditions
of TITLE, NON-INFRINGEMENT, MERCHANTABILITY, or FITNESS FOR A
PARTICULAR PURPOSE. You are solely responsible for determining the
appropriateness of using or redistributing the Work and assume any
risks associated with Your exercise of permissions under this License.
8. Limitation of Liability. In no event and under no legal theory,
whether in tort (including negligence), contract, or otherwise,
unless required by applicable law (such as deliberate and grossly
negligent acts) or agreed to in writing, shall any Contributor be
liable to You for damages, including any direct, indirect, special,
incidental, or consequential damages of any character arising as a
result of this License or out of the use or inability to use the
Work (including but not limited to damages for loss of goodwill,
work stoppage, computer failure or malfunction, or any and all
other commercial damages or losses), even if such Contributor
has been advised of the possibility of such damages.
9. Accepting Warranty or Additional Liability. While redistributing
the Work or Derivative Works thereof, You may choose to offer,
and charge a fee for, acceptance of support, warranty, indemnity,
or other liability obligations and/or rights consistent with this
License. However, in accepting such obligations, You may act only
on Your own behalf and on Your sole responsibility, not on behalf
of any other Contributor, and only if You agree to indemnify,
defend, and hold each Contributor harmless for any liability
incurred by, or claims asserted against, such Contributor by reason
of your accepting any such warranty or additional liability.
END OF TERMS AND CONDITIONS
APPENDIX: How to apply the Apache License to your work.
To apply the Apache License to your work, attach the following
boilerplate notice, with the fields enclosed by brackets "[]"
replaced with your own identifying information. (Don't include
the brackets!) The text should be enclosed in the appropriate
comment syntax for the file format. We also recommend that a
file or class name and description of purpose be included on the
same "printed page" as the copyright notice for easier
identification within third-party archives.
Copyright [yyyy] [name of copyright owner]
Licensed under the Apache License, Version 2.0 (the "License");
you may not use this file except in compliance with the License.
You may obtain a copy of the License at
http://www.apache.org/licenses/LICENSE-2.0
Unless required by applicable law or agreed to in writing, software
distributed under the License is distributed on an "AS IS" BASIS,
WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
See the License for the specific language governing permissions and
limitations under the License.
term_size-0.3.1/LICENSE-MIT 0100664 0001750 0001750 00000002076 13200213275 0013373 0 ustar 00 0000000 0000000 The MIT License (MIT)
Copyright (c) 2015-2016 Kevin B. Knapp
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.
term_size-0.3.1/README.md 0100664 0001750 0001750 00000004710 13200213275 0013213 0 ustar 00 0000000 0000000 term_size
====
[](https://crates.io/crates/term_size) [](https://crates.io/crates/term_size) [](https://github.com/kbknapp/term_size-rs/blob/master/LICENSE-MIT) [](https://github.com/kbknapp/term_size-rs/blob/master/LICENSE-APACHE) [](https://coveralls.io/github/kbknapp/term_size-rs?branch=master) [](https://gitter.im/kbknapp/term_size-rs?utm_source=badge&utm_medium=badge&utm_campaign=pr-badge&utm_content=badge)
Linux: [](https://travis-ci.org/kbknapp/term_size-rs)
Windows: [](https://ci.appveyor.com/project/kbknapp/term_size-rs/branch/master)
A Rust library to enable getting terminal sizes and dimensions
[Documentation](https://kbknapp.github.io/term_size-rs)
## Usage
First, add the following to your `Cargo.toml`:
```toml
[dependencies]
term_size = "0.3"
```
Next, add this to your crate root:
```rust
extern crate term_size;
```
To get the dimensions of your terminal window, simply use the following:
```rust
fn main() {
if let Some((w, h)) = term_size::dimensions() {
println!("Width: {}\nHeight: {}", w, h);
} else {
println!("Unable to get term size :(")
}
}
```
## License
Copyright Benjamin Sago, Kevin Knapp, and `term_size` contributors.
Licensed under either of
* Apache License, Version 2.0, ([LICENSE-APACHE](LICENSE-APACHE) or http://www.apache.org/licenses/LICENSE-2.0)
* MIT license ([LICENSE-MIT](LICENSE-MIT) or http://opensource.org/licenses/MIT)
at your option.
## Contributing
1. Fork it!
2. Create your feature branch: `git checkout -b my-new-feature`
3. Commit your changes: `git commit -am 'Add some feature'`
4. Push to the branch: `git push origin my-new-feature`
5. Submit a pull request :D
Unless you explicitly state otherwise, any contribution intentionally
submitted for inclusion in the work by you, as defined in the
Apache-2.0 license, shall be dual licensed as above, without any
additional terms or conditions.
term_size-0.3.1/appveyor.yml 0100664 0001750 0001750 00000000555 13200213275 0014327 0 ustar 00 0000000 0000000 install:
- ps: Start-FileDownload 'https://static.rust-lang.org/dist/rust-nightly-i686-pc-windows-gnu.exe'
- rust-nightly-i686-pc-windows-gnu.exe /VERYSILENT /NORESTART /DIR="C:\Program Files (x86)\Rust"
- SET PATH=%PATH%;C:\Program Files (x86)\Rust\bin
- rustc -V
- cargo -V
build: false
test_script:
- cargo build --verbose
- cargo test --verbose
term_size-0.3.1/index.html 0100664 0001750 0001750 00000000157 13200213275 0013732 0 ustar 00 0000000 0000000
term_size-0.3.1/justfile 0100664 0001750 0001750 00000002211 13200217772 0013504 0 ustar 00 0000000 0000000 @update-contributors:
echo 'Removing old CONTRIBUTORS.md'
mv CONTRIBUTORS.md CONTRIBUTORS.md.bak
echo 'Downloading a list of new contributors'
echo "the following is a list of contributors:" > CONTRIBUTORS.md
echo "" >> CONTRIBUTORS.md
echo "" >> CONTRIBUTORS.md
githubcontrib --owner kbknapp --repo term_size-rs --sha master --cols 6 --format md --showlogin true --sortBy contributions --sortOrder desc >> CONTRIBUTORS.md
echo "" >> CONTRIBUTORS.md
echo "" >> CONTRIBUTORS.md
echo "This list was generated by [mgechev/github-contributors-list](https://github.com/mgechev/github-contributors-list)" >> CONTRIBUTORS.md
rm CONTRIBUTORS.md.bak
run-test TEST:
cargo test --test {{TEST}}
debug TEST:
cargo test --test {{TEST}} --features debug
run-tests:
cargo test --features "yaml unstable"
@bench: nightly
cargo bench && just remove-nightly
nightly:
rustup override add nightly
remove-nightly:
rustup override remove
@lint: nightly
cargo build --features lints && just remove-nightly
clean:
cargo clean
find . -type f -name "*.orig" -exec rm {} \;
find . -type f -name "*.bk" -exec rm {} \;
find . -type f -name ".*~" -exec rm {} \;
term_size-0.3.1/rustfmt.toml 0100664 0001750 0001750 00000000142 13200213275 0014330 0 ustar 00 0000000 0000000 format_strings = false
chain_overflow_last = false
same_line_if_else = true
fn_single_line = true
term_size-0.3.1/src/lib.rs 0100664 0001750 0001750 00000021360 13200213275 0013637 0 ustar 00 0000000 0000000 // The following was originally taken and adapated from exa source
// repo: https://github.com/ogham/exa
// commit: b9eb364823d0d4f9085eb220233c704a13d0f611
// license: MIT - Copyright (c) 2014 Benjamin Sago
//! System calls for getting the terminal size.
//!
//! Getting the terminal size is performed using an ioctl command that takes
//! the file handle to the terminal -- which in this case, is stdout -- and
//! populates a structure containing the values.
//!
//! The size is needed when the user wants the output formatted into columns:
//! the default grid view, or the hybrid grid-details view.
#[cfg(not(target_os = "windows"))]
extern crate libc;
#[cfg(target_os = "windows")]
extern crate kernel32;
#[cfg(target_os = "windows")]
extern crate winapi;
pub use platform::{dimensions, dimensions_stdout, dimensions_stdin, dimensions_stderr};
#[cfg(any(target_os = "linux",
target_os = "android",
target_os = "macos",
target_os = "ios",
target_os = "bitrig",
target_os = "dragonfly",
target_os = "freebsd",
target_os = "netbsd",
target_os = "openbsd",
target_os = "solaris"))]
mod platform {
use libc::{STDOUT_FILENO, STDIN_FILENO, STDERR_FILENO, c_int, c_ulong, winsize};
use std::mem::zeroed;
// Unfortunately the actual command is not standardised...
#[cfg(any(target_os = "linux", target_os = "android"))]
static TIOCGWINSZ: c_ulong = 0x5413;
#[cfg(any(target_os = "macos",
target_os = "ios",
target_os = "bitrig",
target_os = "dragonfly",
target_os = "freebsd",
target_os = "netbsd",
target_os = "openbsd"))]
static TIOCGWINSZ: c_ulong = 0x40087468;
#[cfg(target_os = "solaris")]
static TIOCGWINSZ: c_ulong = 0x5468;
extern "C" {
fn ioctl(fd: c_int, request: c_ulong, ...) -> c_int;
}
/// Runs the ioctl command. Returns (0, 0) if all of the streams are not to a terminal, or
/// there is an error. (0, 0) is an invalid size to have anyway, which is why
/// it can be used as a nil value.
unsafe fn get_dimensions_any() -> winsize {
let mut window: winsize = zeroed();
let mut result = ioctl(STDOUT_FILENO, TIOCGWINSZ, &mut window);
if result == -1 {
window = zeroed();
result = ioctl(STDIN_FILENO, TIOCGWINSZ, &mut window);
if result == -1 {
window = zeroed();
result = ioctl(STDERR_FILENO, TIOCGWINSZ, &mut window);
if result == -1 {
return zeroed();
}
}
}
window
}
/// Runs the ioctl command. Returns (0, 0) if the output is not to a terminal, or
/// there is an error. (0, 0) is an invalid size to have anyway, which is why
/// it can be used as a nil value.
unsafe fn get_dimensions_out() -> winsize {
let mut window: winsize = zeroed();
let result = ioctl(STDOUT_FILENO, TIOCGWINSZ, &mut window);
if result != -1 {
return window;
}
zeroed()
}
/// Runs the ioctl command. Returns (0, 0) if the input is not to a terminal, or
/// there is an error. (0, 0) is an invalid size to have anyway, which is why
/// it can be used as a nil value.
unsafe fn get_dimensions_in() -> winsize {
let mut window: winsize = zeroed();
let result = ioctl(STDIN_FILENO, TIOCGWINSZ, &mut window);
if result != -1 {
return window;
}
zeroed()
}
/// Runs the ioctl command. Returns (0, 0) if the error is not to a terminal, or
/// there is an error. (0, 0) is an invalid size to have anyway, which is why
/// it can be used as a nil value.
unsafe fn get_dimensions_err() -> winsize {
let mut window: winsize = zeroed();
let result = ioctl(STDERR_FILENO, TIOCGWINSZ, &mut window);
if result != -1 {
return window;
}
zeroed()
}
/// Query the current processes's output (`stdout`), input (`stdin`), and error (`stderr`) in
/// that order, in the attempt to dtermine terminal width. If one of those streams is actually
/// a tty, this function returns its width and height as a number of characters.
///
/// If *all* of the streams are not ttys or return any errors this function will return `None`.
pub fn dimensions() -> Option<(usize, usize)> {
let w = unsafe { get_dimensions_any() };
if w.ws_col == 0 || w.ws_row == 0 {
None
} else {
Some((w.ws_col as usize, w.ws_row as usize))
}
}
/// Query the current processes's output (`stdout`) *only*, in the attempt to dtermine
/// terminal width. If that streams is actually a tty, this function returns its width
/// and height as a number of characters.
///
/// If *all* of the streams are not ttys or return any errors this function will return `None`.
pub fn dimensions_stdout() -> Option<(usize, usize)> {
let w = unsafe { get_dimensions_out() };
if w.ws_col == 0 || w.ws_row == 0 {
None
} else {
Some((w.ws_col as usize, w.ws_row as usize))
}
}
/// Query the current processes's input (`stdin`) *only*, in the attempt to dtermine
/// terminal width. If that streams is actually a tty, this function returns its width
/// and height as a number of characters.
///
/// If *all* of the streams are not ttys or return any errors this function will return `None`.
pub fn dimensions_stdin() -> Option<(usize, usize)> {
let w = unsafe { get_dimensions_in() };
if w.ws_col == 0 || w.ws_row == 0 {
None
} else {
Some((w.ws_col as usize, w.ws_row as usize))
}
}
/// Query the current processes's error output (`stderr`) *only*, in the attempt to dtermine
/// terminal width. If that streams is actually a tty, this function returns its width
/// and height as a number of characters.
///
/// If *all* of the streams are not ttys or return any errors this function will return `None`.
pub fn dimensions_stderr() -> Option<(usize, usize)> {
let w = unsafe { get_dimensions_err() };
if w.ws_col == 0 || w.ws_row == 0 {
None
} else {
Some((w.ws_col as usize, w.ws_row as usize))
}
}
}
#[cfg(target_os = "windows")]
mod platform {
use kernel32::{GetConsoleScreenBufferInfo, GetStdHandle};
use winapi::{CONSOLE_SCREEN_BUFFER_INFO, COORD, SMALL_RECT, STD_OUTPUT_HANDLE};
/// Query the current processes's output, returning its width and height as a
/// number of characters. Returns `None` if the output isn't to a terminal.
pub fn dimensions() -> Option<(usize, usize)> {
let null_coord = COORD { X: 0, Y: 0 };
let null_smallrect = SMALL_RECT {
Left: 0,
Top: 0,
Right: 0,
Bottom: 0,
};
let stdout_h = unsafe { GetStdHandle(STD_OUTPUT_HANDLE) };
let mut console_data = CONSOLE_SCREEN_BUFFER_INFO {
dwSize: null_coord,
dwCursorPosition: null_coord,
wAttributes: 0,
srWindow: null_smallrect,
dwMaximumWindowSize: null_coord,
};
if unsafe { GetConsoleScreenBufferInfo(stdout_h, &mut console_data) } != 0 {
Some(((console_data.srWindow.Right - console_data.srWindow.Left) as usize,
(console_data.srWindow.Bottom - console_data.srWindow.Top) as usize))
} else {
None
}
}
/// Query the current processes's output, returning its width and height as a
/// number of characters. Returns `None` if the output isn't to a terminal.
pub fn dimensions_stdout() -> Option<(usize, usize)> { dimensions() }
/// This isn't implemented for Windows
pub fn dimensions_stdin() -> Option<(usize, usize)> { unimplemented!() }
/// This isn't implemented for Windows
pub fn dimensions_stderr() -> Option<(usize, usize)> { unimplemented!() }
}
// makes project compilable on unsupported platforms
#[cfg(not(any(target_os = "linux",
target_os = "android",
target_os = "macos",
target_os = "ios",
target_os = "bitrig",
target_os = "dragonfly",
target_os = "freebsd",
target_os = "netbsd",
target_os = "openbsd",
target_os = "solaris",
target_os = "windows")))]
mod platform {
pub fn dimensions() -> Option<(usize, usize)> { None }
pub fn dimensions_stdout() -> Option<(usize, usize)> { None }
pub fn dimensions_stdin() -> Option<(usize, usize)> { None }
pub fn dimensions_stderr() -> Option<(usize, usize)> { None }
}