diff options
author | Daniel Baumann <daniel.baumann@progress-linux.org> | 2024-04-28 14:29:10 +0000 |
---|---|---|
committer | Daniel Baumann <daniel.baumann@progress-linux.org> | 2024-04-28 14:29:10 +0000 |
commit | 2aa4a82499d4becd2284cdb482213d541b8804dd (patch) | |
tree | b80bf8bf13c3766139fbacc530efd0dd9d54394c /third_party/rust/term_size | |
parent | Initial commit. (diff) | |
download | firefox-upstream.tar.xz firefox-upstream.zip |
Adding upstream version 86.0.1.upstream/86.0.1upstream
Signed-off-by: Daniel Baumann <daniel.baumann@progress-linux.org>
Diffstat (limited to 'third_party/rust/term_size')
-rw-r--r-- | third_party/rust/term_size/.cargo-checksum.json | 1 | ||||
-rw-r--r-- | third_party/rust/term_size/CHANGELOG.md | 48 | ||||
-rw-r--r-- | third_party/rust/term_size/Cargo.toml | 68 | ||||
-rw-r--r-- | third_party/rust/term_size/LICENSE-APACHE | 201 | ||||
-rw-r--r-- | third_party/rust/term_size/LICENSE-MIT | 21 | ||||
-rw-r--r-- | third_party/rust/term_size/README.md | 63 | ||||
-rw-r--r-- | third_party/rust/term_size/appveyor.yml | 12 | ||||
-rw-r--r-- | third_party/rust/term_size/index.html | 1 | ||||
-rw-r--r-- | third_party/rust/term_size/justfile | 36 | ||||
-rw-r--r-- | third_party/rust/term_size/rustfmt.toml | 4 | ||||
-rw-r--r-- | third_party/rust/term_size/src/lib.rs | 237 |
11 files changed, 692 insertions, 0 deletions
diff --git a/third_party/rust/term_size/.cargo-checksum.json b/third_party/rust/term_size/.cargo-checksum.json new file mode 100644 index 0000000000..64cdea354c --- /dev/null +++ b/third_party/rust/term_size/.cargo-checksum.json @@ -0,0 +1 @@ +{"files":{"CHANGELOG.md":"c3ba8be220413f2ed5fce3f49a3888d22e20802f19a237cdaa1171fb13000427","Cargo.toml":"4de1d58c6b6e19f026939fcd1a3bb1ae65abeaeb524f3c9d0f29909a61d08604","LICENSE-APACHE":"a60eea817514531668d7e00765731449fe14d059d3249e0bc93b36de45f759f2","LICENSE-MIT":"6725d1437fc6c77301f2ff0e7d52914cf4f9509213e1078dc77d9356dbe6eac5","README.md":"9cf3574889a4e7f4fe68f532bacd596786057d41eb20fe8e8ed2215d430d8bd3","appveyor.yml":"bd88ad4a942e10ef602540ae592f3e010327dc93aa3d60f07f9f7ff6d3562719","index.html":"d2203968a6fb051d52c55d6e517614c942ea0edbc283e6c3cc8aeb4556eeaad4","justfile":"8255dafcc5dbb0c4df869d68d5cfe0f8b16835669151dacbc0782fbeb3a3e5d4","rustfmt.toml":"8fd2d63119df515fd5f44e530c709b19d66b09fbc2e22a640bf4b64c57e7d6b3","src/lib.rs":"eab371fe8be05d03bb1cd2a5f1ec3e665423501412383d211552174e21da05c7"},"package":"e2b6b55df3198cc93372e85dd2ed817f0e38ce8cc0f22eb32391bfad9c4bf209"}
\ No newline at end of file diff --git a/third_party/rust/term_size/CHANGELOG.md b/third_party/rust/term_size/CHANGELOG.md new file mode 100644 index 0000000000..3a0d3dfad7 --- /dev/null +++ b/third_party/rust/term_size/CHANGELOG.md @@ -0,0 +1,48 @@ +<a name="v0.3.0"></a> +## 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)) + + + +<a name="v0.2.3"></a> +### v0.2.3 (2017-02-21) + +#### Bug Fixes + +* Moves the code into distinct modules and adds dummy functions for unsupported platforms + + +<a name="v0.2.2"></a> +### v0.2.2 (2017-01-29) + +* Updates deps `libc` and `clippy` + +<a name="v0.2.1"></a> +### 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)) + + + + +<a name="v0.2.0">v0.2.0</a> +## 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)) + + diff --git a/third_party/rust/term_size/Cargo.toml b/third_party/rust/term_size/Cargo.toml new file mode 100644 index 0000000000..56201428ab --- /dev/null +++ b/third_party/rust/term_size/Cargo.toml @@ -0,0 +1,68 @@ +[package] +name = "term_size" +version = "0.3.0" +authors = ["Kevin K. <kbknapp@gmail.com>", "Benjamin Sago <ogham@bsago.me>"] +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 OR Apache-2.0" +keywords = ["term", "terminal", "size", "width", "dimension"] + +[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 diff --git a/third_party/rust/term_size/LICENSE-APACHE b/third_party/rust/term_size/LICENSE-APACHE new file mode 100644 index 0000000000..16fe87b06e --- /dev/null +++ b/third_party/rust/term_size/LICENSE-APACHE @@ -0,0 +1,201 @@ + 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. diff --git a/third_party/rust/term_size/LICENSE-MIT b/third_party/rust/term_size/LICENSE-MIT new file mode 100644 index 0000000000..5acedf0412 --- /dev/null +++ b/third_party/rust/term_size/LICENSE-MIT @@ -0,0 +1,21 @@ +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. diff --git a/third_party/rust/term_size/README.md b/third_party/rust/term_size/README.md new file mode 100644 index 0000000000..9f9c6f26aa --- /dev/null +++ b/third_party/rust/term_size/README.md @@ -0,0 +1,63 @@ +term_size +==== + +[![Crates.io](https://img.shields.io/crates/v/term_size.svg)](https://crates.io/crates/term_size) [![Crates.io](https://img.shields.io/crates/d/term_size.svg)](https://crates.io/crates/term_size) [![license](http://img.shields.io/badge/license-MIT-blue.svg)](https://github.com/kbknapp/term_size-rs/blob/master/LICENSE-MIT) [![license](http://img.shields.io/badge/license-Apache2.0-blue.svg)](https://github.com/kbknapp/term_size-rs/blob/master/LICENSE-APACHE) [![Coverage Status](https://coveralls.io/repos/kbknapp/term_size-rs/badge.svg?branch=master&service=github)](https://coveralls.io/github/kbknapp/term_size-rs?branch=master) [![Join the chat at https://gitter.im/kbknapp/term_size-rs](https://badges.gitter.im/Join%20Chat.svg)](https://gitter.im/kbknapp/term_size-rs?utm_source=badge&utm_medium=badge&utm_campaign=pr-badge&utm_content=badge) + +Linux: [![Build Status](https://travis-ci.org/kbknapp/term_size-rs.svg?branch=master)](https://travis-ci.org/kbknapp/term_size-rs) +Windows: [![Build status](https://ci.appveyor.com/api/projects/status/6q0x4h6i0e3ypbm5?svg=true +)](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. diff --git a/third_party/rust/term_size/appveyor.yml b/third_party/rust/term_size/appveyor.yml new file mode 100644 index 0000000000..efef69ec25 --- /dev/null +++ b/third_party/rust/term_size/appveyor.yml @@ -0,0 +1,12 @@ +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 diff --git a/third_party/rust/term_size/index.html b/third_party/rust/term_size/index.html new file mode 100644 index 0000000000..0b03ded533 --- /dev/null +++ b/third_party/rust/term_size/index.html @@ -0,0 +1 @@ +<meta http-equiv="refresh" content="0; url=https://kbknapp.github.io/term_size-rs/doc/term_size/index.html" /> diff --git a/third_party/rust/term_size/justfile b/third_party/rust/term_size/justfile new file mode 100644 index 0000000000..9ea8fc58ff --- /dev/null +++ b/third_party/rust/term_size/justfile @@ -0,0 +1,36 @@ +@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 in alphabetical order:" > CONTRIBUTORS.md + echo "" >> CONTRIBUTORS.md + echo "" >> CONTRIBUTORS.md + githubcontrib --owner kbknapp --repo term_size-rs --sha master --cols 6 --format md --showlogin true --sortBy login >> 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 {} \; diff --git a/third_party/rust/term_size/rustfmt.toml b/third_party/rust/term_size/rustfmt.toml new file mode 100644 index 0000000000..0136d86e31 --- /dev/null +++ b/third_party/rust/term_size/rustfmt.toml @@ -0,0 +1,4 @@ +format_strings = false +chain_overflow_last = false +same_line_if_else = true +fn_single_line = true diff --git a/third_party/rust/term_size/src/lib.rs b/third_party/rust/term_size/src/lib.rs new file mode 100644 index 0000000000..da2b96cbe3 --- /dev/null +++ b/third_party/rust/term_size/src/lib.rs @@ -0,0 +1,237 @@ +// 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 } +} |