Cork is a simple command-line calculator, mainly targeted towards people who deal with hex numbers

Related tags

Command-line cork
Overview

Cork

version license Rust

Usage

Cork is a simple command-line calculator, mainly targeted towards people who deal with hex numbers. It deals only with integer arithmetic. Expressions may involve mixed bases (limited to decimal, hexadecimal, octal and binary numbers). The global output format may be set to a particular radix - by default it is hex.

History

Cork is something that I wrote over a weekend, when I was getting annoyed at having to punch in 16 digit hex numbers on my calculator. I wanted something on my screen, and naturally on the terminal. But all the calculator programs that I found online (including a REPL of Python and Octave) had a glaring problem - they could work on hex numbers, but the output was always in decimal. So I hit to cargo new ...

Naming

Cork is a rather odd name for a calculator. I wanted something on the lines of kernel calculator, but that's way too long. So kernel became core (technically, that's what it means in English) and calculator, well that can be C. So we have CoreC ... maybe CorC ... ah right, Cork.

Installation

Build from source

If you have cargo installed, then you can build this from source:

git clone https://github.com/RedDocMD/cork
cd cork
cargo build --release

The binary produced will be target/release/cork.

From crates.io

To install from crates.io, run cargo install cork. Then, Cork should be executable with the cork command.

Usage

Cork is a REPL calculator, so you can put in expressions and it displays the answer. A sample run goes like:

Welcome to cork - a calculator for hex-lovers!
Press Ctrl + D to exit.
cork> 0xCAFE
0xcafe
cork> 0xCAFE + 2 * 8 * 0x20
0xccfe
cork> set of dec
cork> ans
52478
cork> 0xCAFE
51966
Exiting ...

Oh, btw, Cork uses rustyline. So you get the whole readline goodness (including a history).

Numbers

Cork accepts four types of numbers:

  • Decimal: These are regular numbers (10, 23245, 4124, etc).
  • Hexadecimal: These are numbers prefixed by 0x (0xaA 0x5acd, 0x101c, etc).
  • Octal: These are numbers prefixed by 0o (0o12, 0o55315, 0o10034, etc).
  • Binary: These are numbers prefixed by 0b (0b1010, 0b101101011001101, 0b1000000011100, etc).

In addition, ans holds the answer of the last computation. It is initialized to 0 on startup.

Set directives

Cork has something called set directives, which basically set some global property. They are of the form

set  

As of now, Cork has the following keys:

Key Possible Values Purpose
of hex, dec, oct, bin Sets the output format

LICENSE

Cork is released under GNU General Public License, v2.

Comments
  • Implements #18 issue/feature, the

    Implements #18 issue/feature, the "to _radix_" command:

    Implements the "to radix" command to quickly and temporary convert a number or the result of an expression to a different radix (for example when working with memory address and segments sizes):

    cork> 0xCAFE to dec
    51966
    cork> 0xCAFE + 2 * 8 * 0x20 to dec
    52478
    cork> 0xCAFE + 2 * 8 * 0x20
    0xccfe
    cork> ans to bin
    0b1100110011111110
    cork> 1234 to hex
    0x4d2
    cork> (0xCAFE + 2 * 8 * 0x20) to oct
    0o146376
    

    cork_to_radix_example

    opened by mgaggero 3
  •  Fixed 0 being displayed empty

    Fixed 0 being displayed empty

    If an expression has value 0, you will only see the base (e.g. "0x", or "0b" / ""). This fixed it by adding a '0' character, if the number was 0

    opened by Zenithsiz 3
  • Adds bitwise ops AND, OR, and XOR

    Adds bitwise ops AND, OR, and XOR

    This pull request adds support for some bitwise operations:

    • AND via "&"
    • OR via "|"
    • XOR via "^"

    What remains unclear to me is how to define operator precedence here. As well, what operations should take precedence over others now that it's possible to mix bitwise operations and "normal" ones. Any feedback or comments you may have would be very appreciated!

    opened by HomoElectromagneticus 3
  • Define a config file to customize cork's behavior

    Define a config file to customize cork's behavior

    Define the format for a file holding the user's configuration preferences and implement the logic to read and parse that file at start-up.

    Preferably, the file should be expected to be found under ${XDG_CONFIG_HOME}/cork/, as per the XDG Base Directory Specification.

    opened by ptosi 2
  • Implement left and right shift operations

    Implement left and right shift operations

    This seems like a useful little tool! While I was playing with it I tried shifting and noticed shift operations were not implemented yet. It looked easy enough to add them so I threw together a quick PR.

    enhancement 
    opened by JCallicoat 2
  • Direct operations with batch file

    Direct operations with batch file

    Hi, it would awesome if were possible to make direct operations on a single line, so it can be used with a batch file, something like: cork 0x4DEE72 + 0x18CF5 >filename.txt Actually it outputs :

    error: Found argument '0x4DEE72' which wasn't expected, or isn't valid in this context
    
    USAGE:
        cork [OPTIONS]
    
    For more information try --help
    

    Cheers

    opened by hellishvictor 1
  • Adds a new flag to work with file scripting

    Adds a new flag to work with file scripting

    This Pull Request aims to solve the issue #12.

    • New flag added (short = -f, long = --file)
    • Refactored the command processing from the interactive, so we wouldn't have code repetition.
    • Created a new module to handle the file processing
    • New function to be called when the flag -f is passed.
    opened by joaoaab 1
  • Support separators in long numbers

    Support separators in long numbers

    For readability of the numbers, it is sometimes desired to add separators to long numbers (for example, writing 0x3000_0000 to avoid confusion with 0x3000000 or 0x300000000); could this be supported by cork? As long as those separators aren't valid operators, it should simply mean stripping arguments from expected separators before converting them to integers.

    As an illustration, consider the following:

    cork> 0x3000_0000 + 0x200_0000
    Failed to parse "0x3000_0000 + 0x200_0000":  --> 1:7
      |
    1 | 0x3000_0000 + 0x200_0000
      |       ^---
      |
      = expected EOI, add, subtract, multiply, divide, rem, lshift, or rshift
    

    Although this is requested for hex numbers, I guess it would also be valid for other bases and characters other than _: for example 10'000'000, 10,000,000? The hex case being the most interesting, of course.

    opened by ptosi 1
  • Add support for one-off operations through `argv`

    Add support for one-off operations through `argv`

    Cork currently ignores command-line arguments; instead, it could be extended to accept an expression, compute and print it, and exit.

    For example:

    $ cork 0x10 + 0x10
    0x20
    $ cork
    Welcome to cork - a calculator for hex-lovers!
    Press Ctrl + D to exit.
    cork> 0x10 + 0x10
    0x20
    cork>
    

    (note that, in the first case, cork returns immediately)

    opened by ptosi 1
  • Allow disabling the welcome message

    Allow disabling the welcome message

    The welcome message is cute:

    $ cork
    Welcome to cork - a calculator for hex-lovers!
    Press Ctrl + D to exit.
    cork> 
    

    but is far from being crucial: there should be a way to disable it, if desired.

    A good way would be through an option in a config file (see #2).

    opened by ptosi 1
  • Accept file to run as script

    Accept file to run as script

    Lines are interpreted the same way if they had been typed in. Evaluated in order, expressions give outputs. Set directives are evaluated, but emit no output.

    • Short arg: "-f"
    • Long arg: "--file"
    opened by RedDocMD 0
  • "Convert to a different radix" command.

    Hi, it would be really useful to have a "to radix" command to quickly and temporary convert a number or the result of an expression to a different radix (for example when working with memory address and segments sizes):

    cork> 0xCAFE to dec
    51966
    cork> 0xCAFE + 2 * 8 * 0x20 to dec
    52478
    cork> 0xCAFE + 2 * 8 * 0x20
    0xccfe
    cork> ans to bin
    0b1100110011111110
    cork> 1234 to hex
    0x4d2
    cork> (0xCAFE + 2 * 8 * 0x20) to oct
    0o146376
    
    opened by mgaggero 0
  • Implement easy-to-use temporary `of` modifier

    Implement easy-to-use temporary `of` modifier

    It's nice to be able to set of to another base but is still a bit verbose, in particular when done for a single operation.

    Could cork be extended to support an operator that temporarily switches of to what makes sense in a "do what I mean" manner?

    For example, the ? operator could result in hex values being printed as decimal and decimal as hex, irrespective of of. Consider

    cork> set of dec
    cork> 1 + 1
    2
    cork> 42?
    0x2a
    
    cork> set of hex
    cork> 0x10 + 0x10
    0x20
    cork> 0x100?
    256
    

    instead of having to manually manage of:

    cork> set of hex
    cork> 42
    0x2a
    cork> set of dec
    
    cork> set of dec
    cork> 0x100
    256
    cork> set of hex
    

    I can't decide whether ?'s behavior should be dependent on of, though ...

    If support for more niche bases is desired (or to disambiguate complex expressions), ? could also take an optional right operand that represents the requested base:

    cork> set of hex
    cork> 0x20 + 3
    0x23
    cork> 0x20 + 3?
    Failed to parse "0x20 + 3?":  --> 1:9
      |
    1 | 0x20 + 3?
      |         ^---
      |
      = ambiguous expression, expected right operand for base
    cork> 0x20 + 3?b
    0b100011
    cork> 0x20 + 3?d
    35
    

    Another approach could also be to overload the case of receiving a single literal of the current of by reflecting it in the alternative base:

    cork> set of hex
    cork> 0x100
    256
    cork> set of dec
    cork> 256
    0x100
    

    which is definitely more informative than the current behavior:

    cork> set of hex
    cork> 0x100
    0x100
    cork> set of dec
    cork> 256
    256
    

    If worth the added complexity, support for other alternative bases than the respective "default ones" (i.e. hex for decimal and decimal for hex), could be implemented through a new set property. For example:

    cork> set of hex
    cork> set alter_of bin
    cork> 0x100
    0b100000000
    
    opened by ptosi 0
Releases(v0.2.6)
Owner
Deep Majumder
To computers and beyond!
Deep Majumder
rsv is a command line tool to deal with small and big CSV, TXT, EXCEL files (especially >10G)

csv, excel toolkit written in Rust rsv is a command line tool to deal with small and big CSV, TXT, EXCEL files (especially >10G). rsv has following fe

Zhuang Dai 39 Jan 30, 2023
A complex numbers, graphing, cli calculator

calc requires gnuplot for graphing history file is stored in ~/.config/.calc_history or C:\\Users\\%USERNAME%\\AppData\\Roaming\\calc.history usage Us

null 6 May 21, 2023
A WIP property-based testing library in Rust, built with generalized targeted-property testing in mind.

Crabcheck A WIP property-based testing library in Rust, built with generalized targeted-property testing in mind. What is property-based testing? TODO

Alperen Keleş 9 Mar 27, 2024
A simple command line based RPN calculator

stak A simple command line based RPN calculator Usage stak can be used in two modes: one-shot from the command line, or in an interactive shell One-sh

null 1 Nov 17, 2021
A simple command-line calculator program writen with Rust.

Calculator.rs An simple command-line calculator program writen with Rust. Features Math functions support > sin(1) = 0.84147098 Variable support > a

BHznJNs 33 Apr 8, 2023
A command line tool for people of transgender experience to replace their deadname within a Git repo.

chowndn (Change Owner from Dead Name) A command line tool for people of transgender experience to replace their dead name within a Git repo. See chown

Christi Miller 23 Dec 6, 2022
Mafa is a command-line tool that helps people interact with online websites in a terminal(tty).

Table of Contents A Small Demo Installation Prerequisite Option 1: Cargo install (recommended) Option 2: Build from source Option 3: Prebuilt binaries

Michael Lee 6 Jul 10, 2023
Mafa is a command-line tool that helps people interact with online websites in a terminal(tty).

A Small Demo Installation Prerequisite Option 1: Cargo install (recommended) Option 2: Build from source Option 3: Prebuilt binaries Mafa is for me? W

Michael Lee 6 Jul 23, 2023
Command Line Scientific Calculator. Free Forever. Made with ❀️ using πŸ¦€

β–ˆβ–ˆβ–ˆβ–ˆβ–ˆβ–ˆβ•—β–ˆβ–ˆβ–ˆβ–ˆβ–ˆβ–ˆβ–ˆβ•— β–ˆβ–ˆβ–ˆβ–ˆβ–ˆβ–ˆβ•— β–ˆβ–ˆβ•”β•β•β•β•β•β–ˆβ–ˆβ•”β•β•β•β•β•β–ˆβ–ˆβ•”β•β•β•β•β• β–ˆβ–ˆβ•‘ β–ˆβ–ˆβ–ˆβ–ˆβ–ˆβ–ˆβ–ˆβ•—β–ˆβ–ˆβ•‘ β–ˆβ–ˆβ•‘ β•šβ•β•β•β•β–ˆβ–ˆβ•‘β–ˆβ–ˆβ•‘ β•šβ–ˆβ–ˆβ–ˆβ–ˆβ–ˆβ–ˆβ•—β–ˆβ–ˆβ–ˆβ–ˆβ–ˆβ–ˆβ–ˆβ•‘β•šβ–ˆβ–ˆβ–ˆβ–ˆβ–ˆβ–ˆβ•— β•šβ•β•β•β•β•β•β•šβ•β•β•β•β•β•β• β•šβ•β•β•β•β•β• -

zahash 17 Oct 28, 2023
CLI tool that make it easier to perform multiple lighthouse runs towards a single target and output the result in a plotable format.

Lighthouse Aggregator CLI tool that make it easier to perform multiple lighthouse runs towards a single target and output the result in a "plotable" f

Polestar 1 Jan 12, 2022
hexyl is a simple hex viewer for the terminal. It uses a colored output to distinguish different categories of bytes

hexyl is a simple hex viewer for the terminal. It uses a colored output to distinguish different categories of bytes (NULL bytes, printable ASCII characters, ASCII whitespace characters, other ASCII characters and non-ASCII).

David Peter 7.3k Dec 29, 2022
My solution for the advent of code 2021, mainly written in Rust

Advent-of-Code-2021 My solution for the advent of code 2021, written in Rust Error Handle NOPE!!! unwrap() everything everywhere Use To run all of the

Nguyen Le Duy 0 Jan 8, 2022
Shellharden is a syntax highlighter and a tool to semi-automate the rewriting of scripts to ShellCheck conformance, mainly focused on quoting

Shellharden is a syntax highlighter and a tool to semi-automate the rewriting of scripts to ShellCheck conformance, mainly focused on quoting

Andreas Nordal 4.3k Dec 28, 2022
Small command-line tool to switch monitor inputs from command line

swmon Small command-line tool to switch monitor inputs from command line Installation git clone https://github.com/cr1901/swmon cargo install --path .

William D. Jones 5 Aug 20, 2022
Parse hex colors to tui::style::Color

Color -> Tui Parse hex colors to tui rgb colors #c3f111 -> Color::Rgb(195,241,17) Note that the indexed colors are NOT HEX #142 -> Color::Indexed(142)

Uttarayan Mondal 1 Nov 8, 2021
A user-friendly re-implementation of existing hex tools in Rust

Hex A project to create alternate (and more user friendly) versions of existing hex tools. The project can be installed as a extension to the github-c

Sohom Datta 6 Sep 27, 2022
convert nostr keys and note-ids between hex and bech32

Key-Convertr People are copy-pasting nostr private keys into webpages to convert between the original hex-encoding and bech32-encoding (specified in N

Rijndael 14 Jan 9, 2023
CLI tool to convert numbers from one base to another

changebase A CLI tool for changing the base of numbers. > changebase -h numeric base converter USAGE: changebase [FLAGS] [OPTIONS] <value> FLAG

null 2 Oct 14, 2022
This repository presents a numbers vizualizer in a polar base. This small project has been entirely made in Rust !

NumbersRepresentation This repository presents a numbers vizualizer in a polar base. This small project has been entirely made in Rust ! This is an id

Lilian 'S3l4h' Schall 3 Apr 12, 2022