Function egg_mode_text::characters_remaining [] [src]

pub fn characters_remaining(
    text: &str,
    max: usize,
    http_url_len: i32,
    https_url_len: i32
) -> (usize, bool)

Returns how many characters would remain with the given text, if the given bound were used as a maximum. Also returns an indicator of whether the given text is a valid length to post with that maximum.

This function exists as a sort of convenience method to allow clients to call one uniform method to show a remaining character count on a tweet compose box, and to conditionally enable a "submit" button.

For the http_url_len and https_url_len parameters, call GET help/configuration on the Twitter API (in the egg-mode crate, this is exposed in egg_mode::service::config) and use the short_url_len and short_url_len_https fields on the struct that's returned. If you want to perform these checks offline, twitter-text's sample code and tests assume 23 characters for both sizes. At the time of this writing (2016-11-28), those numbers were also being returned from the service itself.

If you're writing text for a direct message and want to know how many characters are available in that context, see GET help/configuration in the Twitter API (in the egg-mode crate, this is exposed in egg_mode::service::config) and the dm_text_character_limit returned by that endpoint, then call character_count and subtract the result from the configuration value.

Examples

use egg_mode_text::characters_remaining;

 let (count, _) = characters_remaining("This is a test.", 280, 23, 23);
 assert_eq!(count, 280 - 15);

 // URLs get replaced by a t.co URL of the given length
 let (count, _) = characters_remaining("test.com", 280, 23, 23);
 assert_eq!(count, 280 - 23);

 // Multiple URLs get shortened individually
 let (count, _) =
     characters_remaining("Test https://test.com test https://test.com test.com test",
                          280, 23, 23);
 assert_eq!(count, 280 - 86);